def test_clear_shelf(): banker = Banker() banker.shelf(100) banker.bank() banker.shelf(50) banker.clear_shelf() assert banker.balance == 100 assert banker.shelved == 0
def test_shelf(): banker = Banker() banker.shelf(100) assert banker.shelved == 100 assert banker.balance == 0
def test_bank_over_ten_thousand(): round_bank = Banker(9000) round_bank.shelf(2000) round_bank.bank() assert round_bank.balance == 0
def test_bank_instance_add_from_shelf(): round_bank = Banker(0) round_bank.shelf(300) round_bank.shelf(750) round_bank.bank() assert round_bank.balance == 1050
def test_deposit(): banker = Banker() banker.shelf(100) banker.bank() assert banker.shelved == 0 assert banker.balance == 100
def play(self): """ * To play the game, this method will keep calling itself untill the game ends, by quitting the game * This method starts the rounds, roll the dice, calculate it's score and so on * Uses all the methods above """ if Game.round == 1 and Game.new_game.shelved == 0: Game.result = Game.welcome() if Game.result == "y" or Game.result == "yes": if not Game.new_game.shelved: print(f"Starting round {Game.round}") print(f"Rolling {Game.num_dice} dice...") roll = self.roller(Game.num_dice) self.print_roll(roll) GameLogic.how_many = 0 if not (GameLogic.calculate_score(roll)): self.zilch(len(roll)) GameLogic.how_many = 0 sys.exit() enter = input("Enter dice to keep (no spaces), or (q)uit: ") # print('yyy') if enter.lower() == "q" or enter.lower() == "quit": self.to_quit() else: roll_list = self.check(roll, enter) if roll_list: roll_tuple = tuple(roll_list) Game.num_dice -= len(roll_tuple) GameLogic.how_many = 0 score = GameLogic.calculate_score(roll_tuple) if len(roll_tuple) == 6 and GameLogic.how_many == 6: # print('hot dice:',GameLogic.how_many) Game.status = False if not score: self.zilch(len(roll_tuple)) Game.new_game.shelved = Banker.shelf(Game.new_game, score) print( f"You have {Game.new_game.shelved} unbanked points and {Game.num_dice} dice remaining" ) enter_again = input( "(r)oll again, (b)ank your points or (q)uit ") if enter_again.lower() == "r" or enter_again.lower( ) == "roll": if not Game.num_dice: Game.num_dice = 6 self.play() if enter_again.lower() == "b" or enter_again.lower( ) == "bank": print( f"You banked {Game.new_game.shelved} points in round {Game.round}" ) Game.round += 1 Game.num_dice = 6 Game.banked_score = Banker.bank(Game.new_game) print(f"Total score is {Game.new_game.balance} points") self.play() if enter_again.lower() == "q" or enter_again.lower( ) == "quit": self.to_quit()
def play(self): nope_player = Banker() print('Welcome to Game of Greed') res = input('Wanna play?') if res == 'n': print('OK. Maybe another time') elif res == 'y': # we have to path [new round - current round] #initializing Variable round = 1 remaining_dice = 6 new_round = True #looping until reach break ('q') while True: # run this code if we are in a new round if new_round == True: remaining_dice = 6 dice_to_keep, rolled = self.new_round( remaining_dice, round) round += 1 #check the zilch if Game.zilch(round, rolled, nope_player.balance): round += 1 else: # handeling the Integers -> If Not Integers handel execep where the inpu is string ['q' - 'b'] try: dice_to_keep = int(dice_to_keep) user_aside = Game.convert_input_to_tuple(dice_to_keep) #checking cheat cheat = Game.cheatting(rolled, user_aside) while cheat: print('Cheater!!! Or possibly made a typo...') print(','.join([str(i) for i in rolled])) dice_to_keep = input( 'Enter dice to keep (no spaces), or (q)uit: ') user_aside = Game.convert_input_to_tuple( dice_to_keep) cheat = Game.cheatting(rolled, user_aside) nope_player.shelf( GameLogic.calculate_score(user_aside)) # decrese The Remainig Dice if GameLogic.calculate_score(user_aside) == 1500: remaining_dice = 6 print( f"You have {nope_player.shelved} unbanked points and 0 dice remaining" ) else: remaining_dice -= len(str(dice_to_keep)) print( f"You have {nope_player.shelved} unbanked points and {remaining_dice} dice remaining" ) dice_to_keep = input( '(r)oll again, (b)ank your points or (q)uit ') # To Prevent handel the new_round msg when back to hande ['b' or 'q'] if dice_to_keep == 'b' or dice_to_keep == 'q' or dice_to_keep == 'r': new_round = False except ValueError: if dice_to_keep == 'b': current_round = round - 1 print( f'You banked {nope_player.shelved} points in round {current_round}' ) nope_player.bank() print( f'Total score is {nope_player.balance} points') new_round = True remaining_dice = 6 if dice_to_keep == 'r': print(f'Rolling {remaining_dice} dice...') rolled = self.roller(remaining_dice) if len(str(rolled)) == 1: rolled = () + (rolled, ) print(','.join([str(i) for i in rolled])) # print(round,rolled,nope_player.balance,"top") if Game.zilch(round - 1, rolled, nope_player.balance): new_round = True else: dice_to_keep = input( 'Enter dice to keep (no spaces), or (q)uit: ' ) if dice_to_keep == 'q': Game.quit_game(nope_player.balance) break
class Game: """Class for Game of Greed application """ def __init__(self, roller=None, num_rounds=20): self._roller = roller or GameLogic.roll_dice self.banker = Banker() self.num_rounds = num_rounds self.round_num = 0 def play(self): """ Entry point for playing (or/not) a game """ print("Welcome to Game of Greed") prompt = "Wanna play?" self.choice(prompt.strip(), self.start_game, self.decline_game) def choice(self, prompt, accept, decline): response = input(prompt) if response == "y" or response == "yes": accept() else: decline() def decline_game(self): print("OK. Maybe another time") def start_game(self): self.round_num = 1 while self.round_num <= self.num_rounds: self.start_round(self.round_num) self.round_num += 1 print(f"Total score is {self.banker.balance} points") self.quit_game() def quit_game(self): print(f"Thanks for playing. You earned {self.banker.balance} points") sys.exit() def start_round(self, round, num_dice=6): print(f"Starting round {round}") round_score = 0 while True: roll = self.roll_dice(num_dice) if self.got_zilch(roll): break keepers = self.handle_keepers(roll) roll_again_response = input( "(r)oll again, (b)ank your points or (q)uit ") if roll_again_response == "q": self.quit_game() return elif roll_again_response == "b": round_score = self.banker.bank() break else: num_dice -= len(keepers) if num_dice == 0: num_dice = 6 print(f"You banked {str(round_score)} points in round {round}") def handle_keepers(self, roll): while True: keeper_string = input( "Enter dice to keep (no spaces), or (q)uit: ") if keeper_string.startswith("q"): self.quit_game() keepers = self.gather_keepers(roll, keeper_string) roll_score = self.calculate_score(keepers) if roll_score == 0: print("Must keep at least one scoring dice") else: break self.banker.shelf(roll_score) print( f"You have {self.banker.shelved} unbanked points and {len(roll) - len(keepers)} dice remaining" ) return keepers def roll_dice(self, num): print(f"Rolling {num} dice...") roll = self._roller(num) print(",".join([str(i) for i in roll])) return roll def got_zilch(self, roll): initial_score = self.calculate_score(roll) if initial_score == 0: print("Zilch!!! Round over") self.banker.clear_shelf() return True return False def calculate_score(self, roll): return GameLogic.calculate_score(roll) def keep_scorers(self, roll): return GameLogic.get_scorers(roll) def gather_keepers(self, roll, keeper_string): keepers = [int(ch) for ch in keeper_string] while not GameLogic.validate_keepers(roll, keepers): print("Cheater!!! Or possibly made a typo...") print(",".join([str(i) for i in roll])) keeper_string = input( "Enter dice to keep (no spaces), or (q)uit: ") if keeper_string.startswith("q"): self.quit_game() keepers = [int(ch) for ch in keeper_string] return keepers
def play(self): nope_player = Banker() print('Welcome to Game of Greed') res = input('Wanna play?') if res == 'n': print('OK. Maybe another time') elif res == 'y': remaining_dice = 6 round = 1 user_input = ' ' action_status = True EndOfGame = True while EndOfGame: if action_status: # Starting New Round remaining_dice = 6 Game.new_round(remaining_dice, round) roll = self.rolling(remaining_dice) while Game.zilch(round, roll, nope_player.balance, remaining_dice): round += 1 Game.new_round(remaining_dice, round) roll = self.rolling(remaining_dice) Game.zilch(round, roll, nope_player.balance, remaining_dice) user_choice = Game.user_input() if user_choice == 'q': Game.quit_game(nope_player.balance) break if user_choice.isdigit(): user_aside = Game.convert_input_to_tuple(int(user_choice)) cheat = Game.cheatting(roll, user_aside) while cheat: print('Cheater!!! Or possibly made a typo...') print(','.join([str(i) for i in roll])) user_choice = Game.user_input() if user_choice.isdigit(): user_aside = Game.convert_input_to_tuple( int(user_choice)) cheat = Game.cheatting(roll, user_aside) nope_player.shelf(GameLogic.calculate_score(user_aside)) Game.unbankmsg(nope_player.shelved, remaining_dice - len(user_choice)) if nope_player.shelved == 1500: remaining_dice = 6 user_choice = '' action = Game.action() while True: if action == 'b': print( f'You banked {nope_player.shelved} points in round {round}' ) nope_player.bank() print(f'Total score is {nope_player.balance} points') round += 1 action_status = True break elif action == 'r': action_status = False print( f'Rolling {remaining_dice-len(user_choice)} dice...' ) roll = self.rolling(remaining_dice - len(user_choice)) while Game.zilch(round, roll, nope_player.balance, remaining_dice): round += 1 remaining_dice = 6 Game.new_round(remaining_dice, round) roll = self.rolling(remaining_dice) Game.zilch(round, roll, nope_player.balance, remaining_dice) remaining_dice = remaining_dice - len(user_choice) break if action == 'q': Game.quit_game(nope_player.balance) action_status = False EndOfGame = False break else: print('Enter Yes (yn) or No (n)')
def test_shelf(): banker = Banker() banker.shelf(100) assert banker.shelved == 100 assert banker.total == 0
class Game: """Class for Game of Greed application """ def __init__(self, num_rounds=20): self.banker = Banker() self.logic = GameLogic() self.num_rounds = num_rounds self._roller = GameLogic.roll_dice self.round_num = 0 def play(self, roller=None): """Entry point for playing (or declining) a game Args: roller (function, optional): Allows passing in a custom dice roller function. Defaults to None. """ self.round_num = 0 self._roller = roller or GameLogic.roll_dice print("Welcome to Game of Greed") print("(y)es to play or (n)o to decline") response = input("> ") if response == "y" or response == "yes": self.start_game() else: self.decline_game() def decline_game(self): print("OK. Maybe another time") def new_roll(self, remaining_die): print(f"Rolling {remaining_die} dice...") die_roll = self._roller(remaining_die) string_roll = [str(num) for num in die_roll] return (die_roll, string_roll) def check_validity(self, die_roll, string_roll): cheated = True while cheated: cheated = False print(f'*** {" ".join(string_roll)} ***') round_points = self.logic.calculate_score(die_roll) if round_points == 0: return 'Zilch' print("Enter dice to keep, or (q)uit:") kept_die = input("> ") saved_die = list(kept_die) if kept_die == "q": print( f"Thanks for playing. You earned {self.banker.total} points" ) sys.exit() good_input = True for value in saved_die: try: int(value) except: saved_die.remove(value) user_selection = tuple(int(char) for char in saved_die) no_cheats = self.logic.validate_keepers(die_roll, user_selection) if not no_cheats: cheated = True print('Cheater!!! Or possibly made a typo...') return user_selection def round_decisions(self, remaining_die): print( f"You have {self.banker.shelved} unbanked points and {remaining_die} dice remaining" ) print("(r)oll again, (b)ank your points or (q)uit:") round_choice = input("> ") if round_choice == "q": print(f"Thanks for playing. You earned {self.banker.total} points") sys.exit() elif round_choice == "b": round_points = self.banker.bank() remaining_die = 6 print( f"You banked {round_points} points in round {self.round_num}") elif round_choice == "r": if remaining_die == 0: remaining_die = 6 self.full_roll(remaining_die) def full_roll(self, remaining_die): (die_roll, string_roll) = self.new_roll(remaining_die) user_selection = self.check_validity(die_roll, string_roll) if user_selection == 'Zilch': self.banker.clear_shelf() print( '****************************************\n** Zilch!!! Round over **\n****************************************' ) print(f"You banked 0 points in round {self.round_num}") else: remaining_die -= len(user_selection) self.banker.shelf(self.logic.calculate_score(user_selection)) self.round_decisions(remaining_die) def start_game(self): remaining_die = 6 while self.round_num < self.num_rounds: self.round_num += 1 print(f"Starting round {self.round_num}") self.full_roll(remaining_die) print(f"Total score is {self.banker.total} points") print(f"Thanks for playing. You earned {self.banker.total} points") sys.exit()
class Game: """Class for Game of Greed application """ def __init__(self, num_rounds=20): self.banker = Banker() self.num_rounds = num_rounds def play(self, roller=None): """Entry point for playing (or declining) a game Args: roller (function, optional): Allows passing in a custom dice roller function. Defaults to None. """ self.round_num = 0 self._roller = roller or GameLogic.roll_dice print("Welcome to Game of Greed") print("(y)es to play or (n)o to decline") response = input("> ") if response == "y" or response == "yes": self.start_game() else: self.decline_game() def decline_game(self): print("OK. Maybe another time") def quit_game(self, score=0): print(f"Thanks for playing. You earned {self.banker.balance} points") sys.exit() def start_game(self): self.round_num += 1 self.start_round(self.round_num) def start_round(self, round_num, number=6): print(f"Starting round {round_num}") current_round_score = 0 while True: roll = self.rolling_dice(number) if self.farkle(roll): self.start_round # break selection = self.handle_selection(roll) print("(r)oll again, (b)ank your points or (q)uit:") response = input("> ") if response == "q": self.quit_game() return elif response == "b": current_round_score = self.banker.bank() print( f"You banked {str(current_round_score)} points in round {round_num}" ) print(f'Total score is {self.banker.balance} points') self.start_game() # break else: number -= len(selection) if number == 0: number = 6 def handle_selection(self, roll): while True: print("Enter dice to keep, or (q)uit:") response = input("> ") if response == 'q': self.quit_game() saved_dice = self.saved_dice(roll, response) tup = tuple([int(i) for i in response]) score = GameLogic.calculate_score(tup) break self.banker.shelf(score) print( f'You have {self.banker.shelved} unbanked points and {len(roll) - len(saved_dice)} dice remaining' ) return saved_dice def rolling_dice(self, number): print(f"Rolling {number} dice...") roll = self._roller(number) var = ' ' for i in range(0, len(roll)): var += f'{roll[i]} ' print(f"***{var}***") return roll def check_cheater(self, response, roll): tup = tuple([int(i) for i in response]) for x in tup: if x not in roll: return False return True def saved_dice(self, roll, response): tup = tuple([int(i) for i in response]) check = self.check_cheater(response, roll) if check == 0: print("Cheater!!! Or possibly made a typo...") var = ' ' for i in range(0, len(roll)): var += f'{roll[i]} ' if response == "q": sys.exit() print(f"***{var}***") tup = tuple([int(i) for i in response]) # print("Enter dice to keep, or (q)uit:") # response = input("> ") return tup def farkle(self, roll): score = GameLogic.calculate_score(roll) if score == 0: print(f"""**************************************** ** Zilch!!! Round over ** ****************************************""") self.banker.clear_shelf()