def game_round_keep_phase(self, roll): self.print_dice(roll) if not GameLogic.calculate_score(roll): print('****************************************\n' '** Zilch!!! Round over **\n' '****************************************') print(f"You banked 0 points in round {self.round_}") print(f"Total score is {self.banker.balance} points") self.banker.clear_shelf() self.start_new_round() return kept_dice = None user_input = None while not user_input: print("Enter dice to keep, or (q)uit:") user_input = input("> ") if user_input == "q": self.user_quit() return parsed_input = [ int(number) for number in user_input if number.isnumeric() ] if not GameLogic.validate_keepers(roll, parsed_input): print('Cheater!!! Or possibly made a typo...') self.game_round_keep_phase(roll) return kept_dice = GameLogic.get_scorers(roll) # TODO, we should not pass the roll, but instead pass the kept dice # once that function is built self.dice_remaining -= len(kept_dice) self.game_round_gambling_phase(roll)
def _enter_dice(self): """simulate user entering which dice to keep. Defaults to all scoring dice""" roll = GameLogic.get_scorers(self.last_roll) roll_string = "" for value in roll: roll_string += str(value) if GameLogic.calculate_score(roll) < 200: self.report("> " + roll_string[0]) return roll_string[0] five_count = 0 two_count = 0 three_count = 0 for value in roll_string: if value == "5": five_count += 1 elif value == "2": two_count += 1 elif value == "3": three_count += 1 if two_count <= 4 and two_count != len(roll): roll_string.replace("2", "", two_count) if three_count <= 3 and three_count != len(roll): roll_string.replace("3", "", three_count) if five_count <= 2: if five_count == len(roll_string): roll_string.replace("5", "", 1) else: roll_string.replace("5", "", five_count) if roll_string == "": roll_string += str(roll[0]) self.report("> " + roll_string) return roll_string
def _mock_input(self, *args): prompt = args[0] if prompt.startswith("Wanna play?"): return "y" elif prompt.startswith("Enter dice to keep (no spaces), or (q)uit:"): scorers = GameLogic.get_scorers(self.roll) keepers = "".join([str(ch) for ch in scorers]) return keepers elif prompt.startswith("(r)oll again, (b)ank your points or (q)uit "): return "b" else: raise ValueError(f"Unrecognized prompt {prompt}")
def _enter_dice(self): """simulate user entering which dice to keep. Defaults to all scoring dice""" roll = GameLogic.get_scorers(self.last_roll) roll_string = "" for value in roll: roll_string += str(value) self.report("> " + roll_string) return roll_string
def _enter_dice(self): """simulate user entering which dice to keep. Defaults to all scoring dice""" roll = GameLogic.get_scorers(self.last_roll) roll_string = "" # if we all dice score, please keep them all # if we are intending on banking, lets keep all scoring dice if len(roll) == len( self.last_roll) or self._roll_bank_or_quit() == "b": # self.real_print("\nINTENDING TO BANK:",self.dice_remaining) for value in roll: roll_string += str(value) self.report("> " + roll_string) return roll_string # lets go for highest average of points per die highest_score_per_die = 0 highest_scoring_dice = 0 highest_scoring_len = 0 # check each combination of dice, to determine the 'best' value per die, and keep that set roll = list(roll) roll.sort() for i in range(len(roll)): for j in range(len(roll)): if len(roll[i:j + 1]): test_dice = roll[i:j + 1] test_score = GameLogic.calculate_score( roll[i:j + 1]) / len(test_dice) if test_score > highest_score_per_die: highest_score_per_die = test_score highest_scoring_dice = test_dice highest_scoring_len = len(test_dice) elif test_score == highest_score_per_die: if (highest_score_per_die >= 100): if len(test_dice) > highest_scoring_len: highest_score_per_die = test_score highest_scoring_dice = test_dice highest_scoring_len = len(test_dice) for value in highest_scoring_dice: roll_string += str(value) self.report("> " + roll_string) return roll_string
def test_get_back_one(test_input, expected): actual = GameLogic.get_scorers(test_input) assert sorted(actual) == sorted(expected)
def test_get_scorers(test_input, expected): actual = GameLogic.get_scorers(test_input) assert actual == expected
def test_get_scorers(test_input, expected): actual = GameLogic.get_scorers(test_input) assert actual == tuple(sorted(expected))
def _mock_input(self, *args, **kwargs): """Capture STDINPUT prompts and return corresponding answer Raises: ValueError: If unknown prompt message is present Returns: Corresponding answer to handle game logic """ prompt = args[0] if prompt.startswith("Wanna play?"): return "y" elif prompt.startswith("Enter dice to keep (no spaces), or (q)uit:"): self.scorers = GameLogic.get_scorers(self.roll) all_dice_scored = GameLogic.calculate_score(self.roll)[1] if all_dice_scored: self.scorers = self.roll # If the entire roll (5 or 6 dice) is scored under 300 points, select only 1 dice elif GameLogic.calculate_score(self.scorers)[0] < 300 and len( self.roll) > 4: if 1 in self.scorers: self.scorers = (1, ) elif 5 in self.scorers: self.scorers = (5, ) keepers = "".join([str(ch) for ch in self.scorers]) return keepers elif prompt.startswith("(r)oll again, (b)ank your points or (q)uit "): self.remaining_dice = len(self.roll) - len(self.scorers) # Reset when all dice are scored if self.remaining_dice == 0: self.remaining_dice = 6 combinations = ( { 'dice_left': 1, 'min_points': 250 }, { 'dice_left': 2, 'min_points': 300 }, { 'dice_left': 3, 'min_points': 400 }, { 'dice_left': 4, 'min_points': 700 }, { 'dice_left': 5, 'min_points': 1500 }, ) # Keep rolling if the points are below the minimum for a particular number of dice left for combination in combinations: if self.remaining_dice == combination['dice_left']: if self.current_points > combination['min_points']: self.current_points = 0 return 'b' return 'r' else: raise ValueError(f"Unrecognized prompt {prompt}")
def keep_scorers(self, roll): return GameLogic.get_scorers(roll)