Question

Need help with Intro to Comp Sci 2 (Python) problem:

Write a GUI class called Craps that implements the die game craps. This question asks you to write an event handler that deals with a single round of the game. To understand how to write the handler, you need to remember how the game works. It begins with the player throwing a pair of standard, six-sided dice. If the player rolls a 7 or 11 in the first round, the player wins. If the player rolls a 2, 3, or 12 in the first round, the player loses. For any other roll the player must roll again to determine whether he/she won. During subsequent rounds, the player wins if he/she rolls point, that is, rolls the same total as he/she did during the first round. During subsequent rounds, the player loses if he/she rolls a 7 In the template file find has all the methods completed except for the event handler play. The following is a description of the methods that are already written. None of these methods should be modified as you complete this question: a. init The constructor creates the main window, sets the title, calls the new game() method, and calls the make widgets0 method. b. new game: This method resets the first roll variable in the object. When the first roll is set to 0 it means that the player has just begun a new game. Once the player

This is what is provided:
(Copy/Paste version):


from tkinter import Tk, Label, Entry, Button
from random import *

class Craps(Tk):

#set up the main window
def __init__(self, parent=None):
Tk.__init__(self, parent)
self.title('Play Craps')
self.new_game()
self.make_widgets()

#when a new game is started, the firstRoll will start at 0
def new_game(self):
self.firstRoll = 0
  
#create and place the widgets in the window
def make_widgets(self):
Label(self, text="Die 1").grid(row=0, column=0, columnspan=1)
Label(self, text="Die 2").grid(row=0, column=1, columnspan=1)

self.die1Ent = Entry(self) #entry field for die 1
self.die2Ent = Entry(self) #entry field for die 2
self.die1Ent.grid(row=1, column=0, columnspan=1)
self.die2Ent.grid(row=1, column=1, columnspan=1)

Label(self, text="First roll").grid(row=2, column=0, columnspan=1)
Label(self, text="Result").grid(row=2, column=1, columnspan=1)

self.firstRollEnt = Entry(self) #entry field for the first roll
self.resultEnt = Entry(self) #entry field the result of the current roll
self.firstRollEnt.grid(row=3, column=0, columnspan=1)
self.resultEnt.grid(row=3, column=1, columnspan=1)

Button(self, text="Roll the dice!", command=self.play).grid(row=4, column=0)

#complete the implementation of play()
def play(self):
#replace pass with your solution

  

Craps().mainloop()

-------------------------------------------------------------------------------------------------------------------

(Screenshot version):

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Craps(Tk):
    'a GUI that plays craps'

    def __init__(self, parent=None):
        'constructor'
        Tk.__init__(self, parent)
        self.title("Play craps")
        self.new_game()
        self.make_widgets()

    def new_game(self):
        'reset the game'
        self.firstroll = 0
        
    def make_widgets(self):
        'define the Craps widgets'
        Label(self, text="Die 1").grid(row=0, column=0)
        Label(self, text="Die 2").grid(row = 0, column = 1)
        self.ent1 = Entry(self, justify = CENTER)
        self.ent1.grid(row = 1, column = 0)
        self.ent2 = Entry(self, justify = CENTER)
        self.ent2.grid(row = 1, column = 1)
        Label(self, text = "First roll").grid(row = 2, column = 0)
        self.first = Entry(self, justify = CENTER)
        self.first.grid(row = 3, column = 0)
        Label(self, text = "Result").grid(row = 2, column = 1)
        self.result = Entry(self, justify = CENTER)
        self.result.grid(row = 3, column = 1)
        Button(self, text="Roll the dice!", command = self.play).grid(row = 4, column = 0, columnspan = 2)

   
    def play(self):
        'the event handler'
        self.result.delete(0,END)
        roll1, roll2 = randrange(1,7), randrange(1,7)
        self.ent1.delete(0,END)
        self.ent2.delete(0,END)
        self.ent1.insert(END, roll1)
        self.ent2.insert(END, roll2)
        if self.firstroll == 0:
            self.first.delete(0,END)
            self.firstroll = roll1 + roll2
            self.first.insert(END, self.firstroll)
            if self.firstroll == 7:
                self.result.insert(END, 'You won! Play Again?')
                self.new_game()
            elif self.firstroll == 2 or self.firstroll == 3 or self.firstroll == 12:
                self.result.insert(END, 'You lost. Play Again?')
                self.new_game()
            else:
                self.result.insert(END, 'Roll Again')
    
        else:
            total = roll1 + roll2
            if total == 7:
                self.result.insert(END, 'You lost! Play Again?')
                self.new_game()
            elif total == self.firstroll:
                self.result.insert(END, 'You won! Play Again?')
                self.new_game()
            else:
                self.result.insert(END, 'Roll Again')
Add a comment
Know the answer?
Add Answer to:
Need help with Intro to Comp Sci 2 (Python) problem: This is what is provided: (Copy/Paste...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help with Python code. Right now I'm creating a code in Python to create a GUI....

    Help with Python code. Right now I'm creating a code in Python to create a GUI. Here's my code: #All modules are built into Python so there is no need for any installation import tkinter from tkinter import Label from tkinter import Entry def calculatewages(): hours=float(nhours.get()) nsal=float(nwage.get()) wage=nsal*hours labelresult=Label(myGUI,text="Weekly Pay: $ %.2f" % wage).grid(row=7,column=2) return Tk = tkinter.Tk()    myGUI=Tk myGUI.geometry('400x200+100+200') myGUI.title('Pay Calculator') nwage=float() nhours=float() label1=Label(myGUI,text='Enter the number of hours worked for the week').grid(row=1, column=0) label2=Label(myGUI,text='Enter the pay rate').grid(row=2, column=0)...

  • Hi I am using python and I keep getting this error with my code. Traceback (most...

    Hi I am using python and I keep getting this error with my code. Traceback (most recent call last): ['1'] Traceback (most recent call last): File "/Users/isabellawelch/Python ish/shuffle.py", line 86, in <module> emp = Employee(emp_data[0], emp_data[1], emp_data[2], emp_data[3], emp_data[4]) IndexError: list index out of range What does it mean and how do i fix it?? from tkinter import Tk, Label, Button, Entry, END employees = [] class Employee: def __init__(self, empno, name, addr, hwage, hworked): self.empno = empno self.name =...

  • The Gui has all the right buttons, but from there i get lost. I need to...

    The Gui has all the right buttons, but from there i get lost. I need to know whats wrong with my assignment can someone please help. The code I have so far is listed below, could you please show me the errors in my code. PYTHON Create the GUI(Graphical User Interface). Use tkinter to produce a form that looks much like the following. It should have these widgets. Temperature Converter GUI Enter a temperature (Entry box)                 Convert to Fahrenheit...

  • Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk()...

    Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk() self.main.title("mine sweeper") self.define_widgets() self.mines = random.sample( [(i,j) for i in range(25) for j in range(50) ],self.CustomizeNumberOfMines()) print(self.mines) self.main.mainloop() self.CustomizeNumberOfMines() def define_widgets(self): """ Define a canvas object, populate it with squares and possible texts """ self.canvas = tk.Canvas(self.main, width = 1002, height=502, bg="#f0f0f0") self.canvas.grid(row=0, column=0) self.boxes =...

  • Python 3 Fix the code so when i click on submit botton the values reset Code:...

    Python 3 Fix the code so when i click on submit botton the values reset Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox from datetime import datetime window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Working product").grid(row=4, sticky="W", pady=20, padx=20) #Working product label tk.Label(window, text="Failed...

  • I need help figuring out how to code this in C++ in Visual Studio. Problem Statement:...

    I need help figuring out how to code this in C++ in Visual Studio. Problem Statement: Open the rule document for the game LCR Rules Dice Game. Develop the code, use commenting to describe your code and practice debugging if you run into errors. Submit source code. Use the document to write rules for the user on how to play the game in a text file. Then, using the information from the resource articles, create a program that will read...

  • I need to complete the code by implementing the min function and the alpha betta pruning...

    I need to complete the code by implementing the min function and the alpha betta pruning in order to complete the tic tac toe game using pything. code: # -*- coding: utf-8 -*- """ Created on: @author: """ import random from collections import namedtuple GameState = namedtuple('GameState', 'to_move, utility, board, moves') infinity = float('inf') game_result = { 1:"Player 1 Wins", -1:"Player 2 Wins", 0:"It is a Tie" } class Game: """To create a game, subclass this class and implement actions,...

  • WITHOUT FUNCTIONS. Could anyone help me solve this problem not using the function ? Thanks a...

    WITHOUT FUNCTIONS. Could anyone help me solve this problem not using the function ? Thanks a lot. Pig Dice Game Pig is a simple two player dice game, played with one die. The first player to reach or surpass 50 is the winner. Each player takes a turn rolling the dice. They add to the pot with each roll, having to decide to roll again and increase the pot, or cash out. The risk being they could lose the amount...

  • Please, I need help with program c++. This is a chutes and ladders program. The code...

    Please, I need help with program c++. This is a chutes and ladders program. The code must be a novel code to the specifications of the problem statement. Thank you very much. Assignment Overview This program will implement a variation of the game “chutes and ladders” or “snakes and ladders:” https://en.wikipedia.org/wiki/Snakes_and_Ladders#Gameplay. Just like in the original game, landing on certain squares will jump the player ahead or behind. In this case, you are trying to reach to bottom of the...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT