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)...

  • 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 =...

  • 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,...

  • 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...

  • Programming Exercise 8.3 | Instructions Write a GUI-based program that allows the user to convert temperature...

    Programming Exercise 8.3 | Instructions Write a GUI-based program that allows the user to convert temperature values between degrees Fahrenheit and degrees Celsius. The interface should have labeled entry fields for these two values. breezypythongui.py temperatureconvert... + 1 2 File: temperatureconverter.py 3 Project 8.3 4 Temperature conversion between Fahrenheit and Celsius. 5 Illustrates the use of numeric data fields. 6 • These components should be arranged in a grid where the labels occupy the first row and the corresponding fields...

  • You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people....

    You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...

  • I need help finishing my C++ coding assignment! My teacher has provided a template but I...

    I need help finishing my C++ coding assignment! My teacher has provided a template but I need help finishing it! Details are provided below and instructions are provided in the //YOU: comments of the template... My teacher has assigned a game that is similar to battleship but it is single player with an optional multiplayer AI... In the single player game you place down a destroyer, sub, battleship, and aircraft carrier onto the grid.... Then you attack the ships you...

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
ADVERTISEMENT