def test_pick_move(self): strategy.computer_flag = model.O strategy.player_flag = model.X self.assertIn(strategy.pick_move(), [model.TOP_MID, model.MID_LEFT, model.MID_RIGHT, model.BOT_MID]) strategy.computer_flag, strategy.player_flag = strategy.player_flag, strategy.computer_flag model.update_square(model.O, (1, 2)) self.assertEqual(strategy.pick_move(), (1, 0))
def game_control(): """No return. Simple loop to accept user input and validate it.""" global spaces commands = {"quit": game_quit, "exit": game_quit} while True: command = raw_input(">") try: model.update_square(strategy.player_flag, spaces[int(command) - 1]) return except ValueError: try: commands[command]() return except IndexError: print("Unrecognized command. Try again.") except IndexError: print("Value out of range. Please use the number pad.") except model.DoubleMoveError: print("You can't play on a space that's filled. Pick again.")
def game_loop(): """No return. Primary game loop.""" global game_running game_running = True model.clear_board() start_player = random.choice(("You", "The computer")) print("{} will go first.".format(start_player)) player_turn = True if start_player == "You" else False strategy.computer_flag = model.O if start_player == "You" else model.X strategy.player_flag = model.X if start_player == "You" else model.O while game_running: print( """[{0}][{1}][{2}] [{3}][{4}][{5}] [{6}][{7}][{8}]""".format( *current_board() ) ) if " " not in current_board(): print("Draw game.") break if player_turn: game_control() if model.did_player_win(strategy.player_flag): print("You won.") game_quit() else: player_turn = not player_turn else: model.update_square(strategy.computer_flag, strategy.pick_move()) if model.did_player_win(strategy.computer_flag): print("The computer won.") game_quit() else: player_turn = not player_turn clear()
def test_update_square(self): model.update_square(model.O, (1, 2)) self.assertEqual(model.board[1][2], model.O) self.assertRaises(model.DoubleMoveError, model.update_square, model.X, (1, 1))
def setUp(self): model.clear_board() model.update_square(model.X, (0, 0)) model.update_square(model.O, (1, 1)) model.update_square(model.X, (2, 2))
def test_failed_update_square(self): model.update_square(model.O, model.CENTER) self.assertRaises(model.DoubleMoveError, model.update_square, model.X, model.CENTER)
def test_update_square(self): model.update_square(model.X, model.TOP_RIGHT) self.assertEqual(model.board[0][2], model.X) model.update_square(model.O, model.CENTER) self.assertEqual(model.board[1][1], model.O)