def test_guess_with_correct_color_and_position_returns_black_peg(self): # Black key peg is placed for each code peg from the guess which is correct in both color and position solution = [Peg("Blue"), Peg("Blue"), Peg("Pink"), Peg("Pink")] game = Game(solution) result, hint = game.make_guess([Peg("Green"), Peg("Green"), Peg("Green"), Peg("Pink")]) self.assertEqual(hint, [None, None, None, Peg("Black")], msg=f'Answer with correct color and position should return black peg')
def test_correct_answer_fourth_try_wins(self): solution = [Peg("Green"), Peg("Blue"), Peg("Pink"), Peg("Yellow")] game = Game(solution) incorrect_guess = [Peg("Green"), Peg("Green"), Peg("Green"), Peg("Green")] for i in range(0, 3): result = game.make_guess(incorrect_guess) result, _ = game.make_guess(solution) self.assertEqual("Win!!!", result)
def test_length(self): game = Game(self.settings) game.settings['codeLength'] = 8 self.assertEqual(len(self.game.generate_code()), 8) self.settings['codeLength'] = 0 self.assertEqual(len(self.game.generate_code()), 0) game.settings['codeLength'] = 800 self.assertEqual(len(game.generate_code()), 800)
class CodeGenerator(unittest.TestCase): # def __init__(self, settings): settings = { 'codeLength': 4, 'colors': 6, 'maxTurns': 4, } game = Game(settings) def test_length(self): game = Game(self.settings) game.settings['codeLength'] = 8 self.assertEqual(len(self.game.generate_code()), 8) self.settings['codeLength'] = 0 self.assertEqual(len(self.game.generate_code()), 0) game.settings['codeLength'] = 800 self.assertEqual(len(game.generate_code()), 800) def test_colors(self): game = Game(self.settings) game.settings['colors'] = 1 game.settings['codeLength'] = 5 self.assertEqual(game.generate_code(), [1, 1, 1, 1, 1]) game.colors = 5 code = [6, 6, 6, 6, 6] for x in code: self.assertTrue(not x <= game.settings['colors']) self.assertTrue(x > 0)
def main(): """Parse command-line arguments and run a game """ parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0])) parser.add_argument('-n', action=require(lambda i: i % 2 == 0 and i > 0), default=8, type=int, help='number of rounds (must be an even number)', metavar='ROUNDS', dest='rounds') parser.add_argument('-c', action=require(lambda i: i > 0), default=4, type=int, help='length of the secret code', metavar='COLUMNS', dest='cols') parser.add_argument('-r', action=require(lambda i: i > 0), default=8, type=int, help='number of turns per round', metavar='ROWS', dest='rows') parser.add_argument('--noninteractive', action='store_const', const=NPC(), default=NPC(), help='machine vs. machine', dest='player') args = parser.parse_args() return Game(args.rounds, args.rows, args.cols, args.player).play()
def test_random_solver_is_random(self): game = Game() solver = RandomSolver(game) guesses = [] for i in range(0, 50): guesses.append(solver.generate_guess()) guesses = [str(g) for guess_list in guesses for g in guess_list] guess_map = {item: idx for idx, item in enumerate(set(guesses))} guesses = [guess_map.get(g) for g in guesses] t_stat, p_val = chisquare(guesses) self.assertGreater(p_val, 0.05, msg=f"p-value should should be < .05, pval={p_val}")
def test_colors(self): game = Game(self.settings) game.settings['colors'] = 1 game.settings['codeLength'] = 5 self.assertEqual(game.generate_code(), [1, 1, 1, 1, 1]) game.colors = 5 code = [6, 6, 6, 6, 6] for x in code: self.assertTrue(not x <= game.settings['colors']) self.assertTrue(x > 0)
def test_guess_with_duplicate_colors_gives_hint(self): solution = [Peg("Blue"), Peg("Blue"), Peg("Pink"), Peg("Blue")] game = Game(solution) result, hint = game.make_guess([Peg("Blue"), Peg("Blue"), Peg("Blue"), Peg("Yellow")]) self.assertEqual(hint, [Peg("Black"), Peg("Black"), Peg("White"), None])
def test_correct_answer_wins(self): guess = [Peg("Green"), Peg("Green"), Peg("Green"), Peg("Green")] result, _ = Game(guess).make_guess(guess) self.assertEqual("Win!!!", result)
def test_no_hint_when_guess_colors_exceed_solution(self): solution = [Peg("Blue"), Peg("Blue"), Peg("Pink"), Peg("Pink")] game = Game(solution) result, hint = game.make_guess([Peg("Blue"), Peg("Blue"), Peg("Blue"), Peg("Pink")]) correct_hint = [Peg("Black"), Peg("Black"), None, Peg("Black")] self.assertEqual(hint, correct_hint, msg=f"Answer with more than correct color matches should not return white peg")
def test_guess_with_correct_color_returns_white_peg(self): solution = [Peg("Blue"), Peg("Blue"), Peg("Pink"), Peg("Pink")] game = Game(solution) result, hint = game.make_guess([Peg("Green"), Peg("Green"), Peg("Blue"), Peg("Green")]) self.assertEqual(hint, [None, None, Peg("White"), None], msg=f'Answer with correct color should return white peg: {hint}')
def test_incorrect_answer_returns_hint(self): result, hint = Game().make_guess([Peg("Blue"), Peg("Blue"), Peg("Blue"), Peg("Pink")]) self.assertIsNotNone(hint)
def test_fifteen_incorrect_guesses_is_loss(self): game = Game([Peg("Green"), Peg("Blue"), Peg("Pink"), Peg("Yellow")]) incorrect_guess = [Peg("Green"), Peg("Green"), Peg("Green"), Peg("Green")] for i in range(0, 15): result, _ = game.make_guess(incorrect_guess) self.assertEqual("Loss, too many guesses", result)
def test_ten_incorrect_guesses_is_loss(self): game_two = Game([Peg("Green"), Peg("Blue"), Peg("Pink"), Peg("Yellow")]) for i in range(0, 10): result, _ = game_two.make_guess([Peg("Green"), Peg("Green"), Peg("Green"), Peg("Green")]) self.assertEqual("Loss, too many guesses", result)
def test_generate_random_answer(self): game_two = Game() # game without solution guess = [Peg("Green"), Peg("Green"), Peg("Blue"), Peg("Blue")] result, _ = game_two.make_guess(guess) self.assertNotEqual("Win!!!", result)
def test_incorrect_answer_does_not_win(self): game_one = Game([Peg("Green"), Peg("Green"), Peg("Blue"), Peg("Blue")]) result, _ = game_one.make_guess([Peg("Green"), Peg("Green"), Peg("Green"), Peg("Green")]) self.assertNotEqual("Win!!!", result)