def playIncompleteGameAIPlaysSecond(self):
     ai_game = Game(Player("X", self.mock_cli_input, self.ui),
                    AIMinimax("O", Rules()), self.ui, Validations(),
                    Rules(), Board())
     ai_game._board.make_move(1, self.game._player1._symbol)
     ai_game._board.make_move(3, self.game._player2._symbol)
     self.db.add_game_to_database(ai_game)
Example #2
0
 def setUp(self):
     self.mock_cli_input = MockCLIInput()
     self.output = MockCLIOutput()
     self.player1 = Player("X", MockCLIInput(), self.output)
     self.player2 = Player("O", MockCLIInput(), self.output)
     self.rules = Rules()
     self.board = Board()
     self.game = Game(Player("X", self.mock_cli_input, self.output),
                      Player("O", MockCLIInput(), self.output), self.output,
                      Validations(), self.rules, self.board)
     self.engine = create_engine(TEST_DB_ADDRESS)
     self.db = Database(self.engine)
Example #3
0
 def create_player_o_object(self, saved_game, cli_input, ui):
     session = self.create_session()
     player_o = session.query(PlayerO.is_ai).filter(PlayerO.saved_game_id == saved_game.id).first()
     if player_o[0]:
         return AIMinimax("O", Rules())
     else:
         return Player("O", cli_input, ui)
Example #4
0
 def setUp(self):
     self.mock_cli_input = MockCLIInput()
     self.mock_cli_output = MockCLIOutput()
     self.ui = UIWrapper(self.mock_cli_output)
     self.rules = Rules()
     self.game = Game(Player("X", self.mock_cli_input, self.ui), Player("O", MockCLIInput(), self.ui), self.ui, Validations(), self.rules, Board())
     self.engine = create_engine(TEST_DB_ADDRESS)
     self.db = Database(self.engine)
Example #5
0
    def recreate_game_object(self, saved_game):
        cli_input = CLIInput()
        ui = UIWrapper(CLIOutput())
        board_obj = self.create_board_object(saved_game)
        player_x_obj = self.create_player_x_object(saved_game, cli_input, ui)
        player_o_obj = self.create_player_o_object(saved_game, cli_input, ui)

        game_obj = Game(player_x_obj, player_o_obj, ui, Validations(), Rules(), board_obj, saved_game.id)
        return game_obj
 def setUp(self):
     self.engine = create_engine(TEST_DB_ADDRESS)
     self.db = Database(self.engine)
     self.mock_cli_input = MockCLIInput()
     self.mock_cli_output = MockCLIOutput()
     self.ui = UIWrapper(self.mock_cli_output)
     self.game = Game(Player("X", self.mock_cli_input, self.ui),
                      Player("O", self.mock_cli_input, self.ui), self.ui,
                      Validations(), Rules(), Board())
     self.setup_game = SetupGame(self.mock_cli_input, self.mock_cli_output,
                                 self.engine)
     meta = MetaData(bind=self.engine)
     self.game_table = Table("saved_games",
                             meta,
                             autoload=True,
                             autoload_with=self.engine)
 def setUp(self):
     self.ai = AIMinimax("O", Rules())
     self.board = Board()
Example #8
0
class RulesTest(unittest.TestCase):
    def setUp(self):
        self.mock_cli_input = MockCLIInput()
        self.output = MockCLIOutput()
        self.player1 = Player("X", MockCLIInput(), self.output)
        self.player2 = Player("O", MockCLIInput(), self.output)
        self.rules = Rules()
        self.board = Board()
        self.game = Game(Player("X", self.mock_cli_input, self.output),
                         Player("O", MockCLIInput(), self.output), self.output,
                         Validations(), self.rules, self.board)
        self.engine = create_engine(TEST_DB_ADDRESS)
        self.db = Database(self.engine)

    def x_winning(self):
        x_winning = Board()
        x_winning.make_move(1, "X")
        x_winning.make_move(2, "O")
        x_winning.make_move(3, "X")
        x_winning.make_move(4, "O")
        x_winning.make_move(5, "X")
        x_winning.make_move(7, "O")
        x_winning.make_move(9, "X")
        return x_winning

    def o_winning(self):
        o_winning = Board()
        o_winning.make_move(1, "X")
        o_winning.make_move(3, "O")
        o_winning.make_move(2, "X")
        o_winning.make_move(5, "O")
        o_winning.make_move(6, "X")
        o_winning.make_move(7, "O")
        return o_winning

    def player1_win(self):
        self.game._board.make_move(1, self.game._player1._symbol)
        self.game._board.make_move(3, self.game._player2._symbol)
        self.game._board.make_move(5, self.game._player1._symbol)
        self.game._board.make_move(6, self.game._player2._symbol)
        self.game._board.make_move(9, self.game._player1._symbol)

    def player2_win(self):
        self.game._board.make_move(2, self.game._player1._symbol)
        self.game._board.make_move(3, self.game._player2._symbol)
        self.game._board.make_move(6, self.game._player1._symbol)
        self.game._board.make_move(5, self.game._player2._symbol)
        self.game._board.make_move(9, self.game._player1._symbol)
        self.game._board.make_move(7, self.game._player2._symbol)

    def draw_game(self):
        self.game._board.make_move(5, self.game._player1._symbol)
        self.game._board.make_move(1, self.game._player2._symbol)
        self.game._board.make_move(3, self.game._player1._symbol)
        self.game._board.make_move(7, self.game._player2._symbol)
        self.game._board.make_move(4, self.game._player1._symbol)
        self.game._board.make_move(8, self.game._player2._symbol)
        self.game._board.make_move(9, self.game._player1._symbol)
        self.game._board.make_move(6, self.game._player2._symbol)
        self.game._board.make_move(2, self.game._player1._symbol)

    def testRuleIsFullReturnsTrueWhenTheBoardIsFull(self):
        self.draw_game()

        result = self.rules.is_full(self.board)
        expected_result = True
        self.assertTrue(result,
                        msg='\nRetrieved:\n{0} \nExpected:\n{1}'.format(
                            result, expected_result))

    def testRulesAIMinimaxReturnsTrueWhenAskedIfPlayerXWinsTheGame(self):
        board_x_winning = self.x_winning()

        result = self.rules.winning_symbol_check("X", board_x_winning)
        expected_result = True
        self.assertTrue(result,
                        msg='\nRetrieved:\n{0} \nExpected:\n{1}'.format(
                            result, expected_result))

    def testRulesAIMinimaxReturnsTrueWhenAskedIfPlayerOWinsTheGame(self):
        board_o_winning = self.o_winning()

        result = self.rules.winning_symbol_check("O", board_o_winning)
        expected_result = True
        self.assertTrue(result,
                        msg='\nRetrieved:\n{0} \nExpected:\n{1}'.format(
                            result, expected_result))

    def testRulesAIMinimaxReturnsFalseWhenAskedIfOpponentPlayerOWinsTheGame(
            self):
        board_x_winning = self.x_winning()

        result = self.rules.winning_symbol_check("O", board_x_winning)
        expected_result = False
        self.assertFalse(result,
                         msg='\nRetrieved:\n{0} \nExpected:\n{1}'.format(
                             result, expected_result))

    def testRulesAIMinimaxReturnsFalseWhenAskedIfOpponentPlayerXWinsTheGame(
            self):
        board_o_winning = self.o_winning()

        result = self.rules.winning_symbol_check("X", board_o_winning)
        expected_result = False
        self.assertFalse(result,
                         msg='\nRetrieved:\n{0} \nExpected:\n{1}'.format(
                             result, expected_result))

    def testRulesIsWonReturnsTrueWhenGameIsWon(self):
        self.player1_win()

        result = self.rules.is_winner(self.game._board)
        expected_result = True
        self.assertTrue(result,
                        msg='\nRetrieved:\n{0} \nExpected:\n{1}'.format(
                            result, expected_result))

    def testRulesIsWonReturnsFalseWhenGameIsNotWon(self):
        self.draw_game()

        result = self.rules.is_winner(self.game._board)
        expected_result = False
        self.assertFalse(result,
                         msg='\nRetrieved:\n{0} \nExpected:\n{1}'.format(
                             result, expected_result))

    def testRulesIsDrawReturnsTrueWhenGameIsADraw(self):
        self.draw_game()

        result = self.rules.is_full(self.game._board)
        expected_result = True
        self.assertTrue(result,
                        msg='\nRetrieved:\n{0} \nExpected:\n{1}'.format(
                            result, expected_result))

    def testRulesIsDrawReturnsFalseWhenGameIsNotADraw(self):
        self.player1_win()

        result = self.rules.is_full(self.game._board)
        expected_result = False
        self.assertFalse(result,
                         msg='\nRetrieved:\n{0} \nExpected:\n{1}'.format(
                             result, expected_result))

    def testRulesKnowsWhenGameEndsWhenThereIsAWinOrADraw(self):
        self.player1_win()

        result = self.rules.game_over(self.game._board)
        expected_result = True
        self.assertTrue(result,
                        msg='\nRetrieved:\n{0} \nExpected:\n{1}'.format(
                            result, expected_result))

    def testRulesReturnsWinningPlayersSymbolXWhenPlayer1HasWon(self):
        self.player1_win()

        result = self.rules.winner(self.game._board)
        expected_result = self.game._player1._symbol
        self.assertEqual(result,
                         expected_result,
                         msg='\nRetrieved:\n{0} \nExpected:\n{1}'.format(
                             result, expected_result))

    def testRulesReturnsWinningPlayersSymbolOWhenPlayer2HasWon(self):
        self.player2_win()

        result = self.rules.winner(self.game._board)
        expected_result = self.game._player2._symbol
        self.assertEqual(result,
                         expected_result,
                         msg='\nRetrieved:\n{0} \nExpected:\n{1}'.format(
                             result, expected_result))