Question: I'm sure the code below is the correct code. here is the link to the previous task before this and there will be next task
I'm sure the code below is the correct code. here is the link to the previous task before this and there will be next task following this same project. This project is focused on topic " special class and inheritance in python".
https://www.chegg.com/homework-help/questions-and-answers/dices-ver01-dices01py-2-dice-double-nothing-dice-game-user-inputs-bet-1-pot-pot-100-beginn-q68584913
""" Dices consists of reusable components that are used to build different games using one or several dices. Dices class encapsulates attributes commonly used in dice games (pot and bet) and functions roll and check. Checking is done according to the rules of each game.
Dices ver0.3(dices_0_3.py) is a tkinter program, double-or-nothing-dice-game where user inputs bet that is between 1 - pot. Pot is 100 in the beginning and after the roll user's bet is first checked, win or lose is calculated and added to the pot. The result, pot, and faces are displayed and if the pot is bigger than 0 the game continues and user can enter new bet.
The game ends when the pot is gone. User can not be in dept (pot is always bigger or equal to 0) and if user ends game before the pot is gone the pot is not saved to next game (casino can not be in dept).
Dices is a user interface that hides the actual Game where all game logic is separated. User is playing for one pot only, there is one and the same game all the time. """ import random import tkinter as tk from tkinter import messagebox import tkinter.ttk as ttk #basic Tk widgets are overriden
class Dices(tk.Tk) : ''' encapsulates a dice game and provides user interface to call functions roll and check. Checking is done according to the rules of each game. ''' def __init__(self, pot=100, bet=1, number=2, *args, **kwargs ) : ''' calls TK initializer defines and initializes attributes ''' super().__init__(*args, **kwargs) #call Tk class initializer self.title('Double-or-nothing') self.geometry('360x160') self.resizable(width = False, height = False) self.pot = pot self.bet = bet self.number = number self.__faces = [0]*number self.__createLayout()
def __createLayout(self): '''create layout widgets and place them into grid''' self.__v_bet = tk.IntVar() self.__v_bet.set(1) self.__v_roll = tk.StringVar(value='Roll the dices') self.__result = tk.StringVar() self.__result.set(f'The pot starts from {self.pot}') self.__error = tk.StringVar() self.__error.set('') self.columnconfigure(0, weight = 1) self.__l_bet = ttk.Label( self, text = 'Place your bet', justify = tk.CENTER) self.__l_bet.grid(sticky = tk.N + tk.S) self.__s_bet = ttk.Entry( self, textvariable = self.__v_bet, justify = tk.CENTER) self.__s_bet.grid(sticky = tk.N + tk.S) self.b = ttk.Button(self, textvariable = self.__v_roll, command=self.__rollandcheck) self.b.grid(sticky = tk.N + tk.S) ttk.Label( self, textvariable=self.__result, justify = tk.CENTER ).grid(sticky = tk.N + tk.S) ttk.Label( self, textvariable=self.__error, justify = tk.CENTER ).grid(sticky = tk.N + tk.S) def __rollandcheck(self): '''called when button is pressed, calls roll and then check and shows result''' try: self.__error.set('') self.bet = self.__v_bet.get() self.roll() self.__result.set(self.check()) if self.pot = 0 ''' self.__pot = pot if self.__pot = 1 and bet 0: self.__number = number self.__faces = [0]*number def roll(self) : ''' fills faces list with new random numbers [1,6] ''' for i in range(self.number) : self.__faces[i] = random.randint(1, 6) def check(self): ''' double-or-nothing - if the two dices faces are: * double 1 or 6 user wins the bet ten fold (multiplied with 10) * double 2, 3, 4, or 5 user wins the bet doubled (multiplied with 2) * no double and sum equals 6 user loses the bet * any other combination (no double, sum not 6) and user loses the doubled bet Winnings (updated bet) are added into pot and lost bet is subtracted from the pot. ''' text = f'{self.__faces[0]} and {self.__faces[1]} - You ' if self.number != 2 : raise ValueError('Wrong game, double-or-nothing is two dice game') else : if self.__faces[0] == self.__faces[1] : if self.__faces[0] == 1 or self.__faces[0] == 6 : text += f'won {self.bet} x 10!' self.pot += self.bet * 10 else : text += f'won {self.bet} x 2!' self.pot += self.bet * 2 elif sum(self.__faces) == 6 : text += f'lost your {self.bet} - bet!' else : text += f'lost {self.bet} x 2!' self.pot -= self.bet * 2 text += f' Your pot is now {self.pot}' return text if __name__ == '__main__' : Dices().mainloop()
Write a new version of Dices (dices_1_1.py). Add into version 1.0 (created in Lab 8) a new mixin class Tenner that defines an abstract method tenner and let Dices class to inherit it. Dices inherits both Tk from tkinter library and Tenner. If user selects to play 'tenner the bet is multiplied with 10 prior checking the result of roll. If 'tenner is selected and the sum of the two dices is 10 user wins the already tenned bet tenfold. eg if user bet 1 and 'tenner is selected the bet value in checking is 10. If the sum of dices is 10 user wins 1*10*10 = 100 which is then added to the pot. Inherited method tenner can be implemented in Dices class using e.g. radiobutton, checkbutton, button. User must be able to select and deselect the tenner option for each roll. As a whole tenner functionality is implemented in the double-or-nothing-game with a combination of an attribute istenner (False = not tennered) and method tenner, that checks if the sum is 10 before calling check. Write a new version of Dices (dices_1_1.py). Add into version 1.0 (created in Lab 8) a new mixin class Tenner that defines an abstract method tenner and let Dices class to inherit it. Dices inherits both Tk from tkinter library and Tenner. If user selects to play 'tenner the bet is multiplied with 10 prior checking the result of roll. If 'tenner is selected and the sum of the two dices is 10 user wins the already tenned bet tenfold. eg if user bet 1 and 'tenner is selected the bet value in checking is 10. If the sum of dices is 10 user wins 1*10*10 = 100 which is then added to the pot. Inherited method tenner can be implemented in Dices class using e.g. radiobutton, checkbutton, button. User must be able to select and deselect the tenner option for each roll. As a whole tenner functionality is implemented in the double-or-nothing-game with a combination of an attribute istenner (False = not tennered) and method tenner, that checks if the sum is 10 before calling check
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
