Beispiel #1
0
def test_isDraw():
    x = ttt.TicTacToe(brd=["O", "X", "X", "X", "O", "X", "X", "O", "O"])
    assert (x.isDraw(9) == False)
    x = ttt.TicTacToe(brd=["X", "X", "O", "O", "O", "X", "X", "O", "O"])
    assert (x.isDraw(9) == True)
    assert (x.isDraw(9, brd=["O", "O", "X", "X", "X", "O", "O", "X",
                             "O"]) == True)
Beispiel #2
0
def test_getNextMoveNo():
    x = ttt.TicTacToe()
    assert (x.getNextMoveNo() == 1)
    assert (x.getNextMoveNo(
        brd=["O", "-", "X", "X", "-", "X", "-", "O", "O"]) == 7)
    x = ttt.TicTacToe(brd=["X", "X", "O", "O", "O", "X", "X", "O", "O"])
    assert (x.getNextMoveNo() == None)
Beispiel #3
0
def test_isWin():
    x = ttt.TicTacToe()
    x.setBoard(["O", "-", "X", "X", "-", "X", "-", "O", "O"])
    x.play(5, "X")
    assert (x.isWin() == True)
    x = ttt.TicTacToe()
    x.setBoard(["O", "-", "X", "X", "-", "X", "-", "O", "O"])
    x.play(2, "X")
    assert (x.isWin() == False)
    assert (x.isWin(brd=["O", "-", "X", "X", "X", "X", "-", "O", "O"]) == True)
Beispiel #4
0
def test_rankMove():
    x = ttt.TicTacToe(brd=["O", "-", "X", "X", "-", "X", "-", "O", "O"])
    assert (x.rankMove("X", 5) == 10)
    assert (x.rankMove("X", 2) == -10)
    assert (x.rankMove("X", 7) == -10)
    x = ttt.TicTacToe(brd=["X", "O", "-", "O", "O", "X", "X", "X", "-"])
    assert (x.rankMove("O", 3) == -10)
    assert (x.rankMove("X",
                       5,
                       brd=["O", "-", "X", "X", "-", "X", "-", "O",
                            "O"]) == 10)
Beispiel #5
0
def test_init():
    x = ttt.TicTacToe()
    assert (x.board == ["-", "-", "-", "-", "-", "-", "-", "-", "-"])
    assert (x.player1 == "computer")
    assert (x.player2 == "human")
    assert (x.player1ID == "X")
    assert (x.player2ID == "O")
    y = ttt.TicTacToe(brd=["O", "-", "X", "X", "-", "X", "-", "O", "O"])
    assert (y.board[0] == "O")
    assert (y.board[1] == "-")
    assert (y.board[8] == "O")
    assert (y.board[3] == "X")
    assert (y.board[4] == "-")
Beispiel #6
0
def test_analyseMovesFor():
    x = ttt.TicTacToe(brd=["O", "-", "X", "X", "-", "X", "-", "O", "O"])
    assert (x.analyseMovesFor("X") == [(1, -10), (4, 10), (6, -10)])
    assert (x.analyseMovesFor(
        "O", brd=["O", "-", "X", "X", "-", "X", "-", "-", "O"]) == [(1, -10),
                                                                    (4, 10),
                                                                    (6, -10),
                                                                    (7, -10)])
    x = ttt.TicTacToe(brd=["X", "-", "X", "-", "O", "-", "-", "-", "O"])
    assert (x.analyseMovesFor(
        "O", brd=["X", "-", "X", "-", "O", "-", "-", "-", "O"]) == [(1, 8),
                                                                    (3, -10),
                                                                    (5, -10),
                                                                    (6, -10),
                                                                    (7, -10)])
Beispiel #7
0
def test_available():
    x = ttt.TicTacToe()
    x.setBoard(["O", "-", "X", "X", "-", "X", "-", "O", "O"])
    assert (x.available() == [1, 4, 6])
    assert (x.available(brd=["O", "-", "X", "X", "-", "X", "-", "O", "O"]) == [
        1, 4, 6
    ])
Beispiel #8
0
def play():
    """Game start function. User enters player names, is created
    object of class TicTacToe, the game starts."""
    name_1 = input("Введите имя первого игрока (X): ")
    name_2 = input("Введите имя второго игрока (O): ")
    game = T.TicTacToe(name_1, name_2)
    main_game(game)
Beispiel #9
0
def test_play():
    x = ttt.TicTacToe()
    x.setBoard(["O", "-", "X", "X", "-", "X", "-", "O", "O"])
    x.play(2, "X")
    assert (x.board[1] == "X")
    with pytest.raises(ttt.PosTakenError):
        x.play(3, "O")
Beispiel #10
0
def test_setboard():
    x = ttt.TicTacToe()
    x.setBoard(["O", "-", "X", "X", "-", "X", "-", "O", "O"])
    assert (x.board[0] == "O")
    assert (x.board[1] == "-")
    assert (x.board[8] == "O")
    assert (x.board[3] == "X")
    assert (x.board[4] == "-")
Beispiel #11
0
def test_isAvailable():
    x = ttt.TicTacToe(brd=["O", "-", "X", "X", "-", "X", "-", "O", "O"])
    assert (x.isAvailable(3) == False)
    assert (x.isAvailable(5) == True)
    assert (x.isAvailable(2, brd=["O", "X", "X", "X", "O", "X", "X", "-",
                                  "O"]) == False)
    assert (x.isAvailable(8, brd=["O", "X", "X", "X", "O", "X", "X", "-",
                                  "O"]) == True)
Beispiel #12
0
 def testInitBoard(self):
     board = tic_tac_toe.TicTacToe()
     self.assertListEqual(
         [
             ' ', ' ', ' ',
             ' ', ' ', ' ',
             ' ', ' ', ' '
         ],
         board.state)
Beispiel #13
0
def test_incTerminalState():
    x = ttt.TicTacToe(brd=["O", "-", "X", "X", "-", "X", "-", "O", "O"])
    brds = x.genAllPosBrds("X")
    assert (x.incTerminalState(brds, "X", 7) == True)
    brds = [["O", "X", "X", "X", "-", "X", "-", "O", "O"],
            ["O", "-", "X", "X", "-", "X", "X", "O", "O"]]
    assert (x.incTerminalState(brds, "X", 7) == False)
    brds = [["O", "O", "X", "X", "X", "O", "O", "X", "O"]]
    assert (x.incTerminalState(brds, "X", 9) == True)
Beispiel #14
0
def test_genAllPosBrds():
    x = ttt.TicTacToe(brd=["O", "-", "X", "X", "-", "X", "-", "O", "O"])
    assert (x.genAllPosBrds("X") == [[
        "O", "X", "X", "X", "-", "X", "-", "O", "O"
    ], ["O", "-", "X", "X", "X", "X", "-", "O",
        "O"], ["O", "-", "X", "X", "-", "X", "X", "O", "O"]])
    assert (x.genAllPosBrds(
        "X", brd=["O", "-", "X", "X", "O", "X", "-", "O",
                  "-"]) == [["O", "X", "X", "X", "O", "X", "-", "O", "-"],
                            ["O", "-", "X", "X", "O", "X", "X", "O", "-"],
                            ["O", "-", "X", "X", "O", "X", "-", "O", "X"]])
Beispiel #15
0
    def testSetPosition(self):
        board = tic_tac_toe.TicTacToe()
        board.SetPosition('X', 1, 2)

        self.assertListEqual(
            [
                ' ', ' ', ' ',
                ' ', ' ', 'X',
                ' ', ' ', ' '
            ],
            board.state)
Beispiel #16
0
def play(a_i=None):
    """Game start function. If the game takes place between two human players,
    names are simply entered, where users can decide for themselves who they want
    play. The object is created and the game starts. If the game is between a human
    and a computer, then before we accidentally we choose who will play for the cross,
    depending on the result, we transfer the names of the players (the player's name
    and "Computer") to the object in the correct order. And we start the game"""
    if a_i is None:
        name_1 = input("Введите имя первого игрока (X): ")
        name_2 = input("Введите имя второго игрока (O): ")
        game = T.TicTacToe(name_1, name_2)
    else:
        if not randint(0, 1):
            name_1 = input("Введите имя игрока: ")
            game = T.TicTacToe(name_1, "Компьютер")
            print(f"Случайно выбрано, что за Х играет {name_1}")
        else:
            name_2 = input("Введите имя игрока: ")
            game = T.TicTacToe("Компьютер", name_2)
            print("Случайно выбрано, что за Х играет Компьютер")
    main_game(game, a_i)
Beispiel #17
0
    def testGameOverStalemate(self):
        board = tic_tac_toe.TicTacToe()
        
        board.SetPosition('X', 0, 0)
        board.SetPosition('O', 0, 1)
        board.SetPosition('X', 0, 2)
        board.SetPosition('O', 1, 0)
        board.SetPosition('X', 1, 1)
        board.SetPosition('O', 1, 2)
        board.SetPosition('X', 2, 0)
        board.SetPosition('O', 2, 1)
        board.SetPosition('X', 2, 2)

        self.assertTrue(board.GameOver())
Beispiel #18
0
    def testIfTwoWinners(self):
        board = tic_tac_toe.TicTacToe()
        
        board.SetPosition('X', 0, 0)
        board.SetPosition('X', 0, 1)
        board.SetPosition('X', 0, 2)
        board.SetPosition('O', 1, 0)
        board.SetPosition('O', 1, 1)
        board.SetPosition('O', 1, 2)
        board.SetPosition(' ', 2, 0)
        board.SetPosition(' ', 2, 1)
        board.SetPosition(' ', 2, 2)

        self.assertEqual(board.Winner(), None)
Beispiel #19
0
def play_game():
    '''Logic for building tic-tac-toe instance and playing rounds'''

    player_1 = input('Player 1, what\'s your name? ')
    player_2 = input('Player 2, what\'s your name? ')
    size = input(
        'How many rows/columns in your board? Minimum size is 3, maximum size is 8: '
    )

    game = tic_tac_toe.TicTacToe(player_1, player_2, int(size))

    outcome = game.play()
    while outcome != 'ended':
        outcome = game.play()
Beispiel #20
0
    def testRenderEmptyBoard(self):
        board = tic_tac_toe.TicTacToe()

        self.assertMultiLineEqual(
            "   |   |   \n"
            "   |   |   \n"
            "   |   |   \n"
            "---+---+---\n"
            "   |   |   \n"
            "   |   |   \n"
            "   |   |   \n"
            "---+---+---\n"
            "   |   |   \n"
            "   |   |   \n"
            "   |   |   \n",
            board.RenderBoard())
Beispiel #21
0
    def testRenderBoard(self):
        board = tic_tac_toe.TicTacToe()
        
        board.SetPosition('X', 1, 0)

        self.assertMultiLineEqual(
            "   |   |   \n"
            "   |   |   \n"
            "   |   |   \n"
            "---+---+---\n"
            "   |   |   \n"
            " X |   |   \n"
            "   |   |   \n"
            "---+---+---\n"
            "   |   |   \n"
            "   |   |   \n"
            "   |   |   \n",
            board.RenderBoard())
Beispiel #22
0
def run_suite():
    """Run informal tests on code."""
    suite = test.TestSuite()

    # testing the sum_along_direction() method
    test_board2 = np.array([[row + col for col in range(5)]
                            for row in range(6)])
    game1 = tic_tac_toe.TicTacToe(6, 5, test_board2)
    print(game1)
    suite.run_test(game1.sum_along_direction((4, 2), (-1, 0), 3), 15, "Test #3.1")
    suite.run_test(game1.sum_along_direction((4, 2), (0, -1), 3), 15, "Test #3.2")
    suite.run_test(game1.sum_along_direction((4, 2), (0, 1), 3), 21, "Test #3.3")
    suite.run_test(game1.sum_along_direction((4, 2), (0, -1), 4), 0, "Test #3.4")
    suite.run_test(game1.sum_along_direction((4, 2), (1, 0), 3), 0, "Test #3.5")
    suite.run_test(game1.sum_along_direction((4, 2), (1, 0), 2), 13, "Test #3.6")
    suite.run_test(game1.sum_along_direction((4, 2), (-1, 1), 3), 18, "Test #3.7")
    suite.run_test(game1.sum_along_direction((4, 2), (-1, 1), 4), 0, "Test #3.8")
    suite.run_test(game1.sum_along_direction((4, 2), (1, -1), 3), 0, "Test #3.9")
    suite.run_test(game1.sum_along_direction((5, 0), (0, 1), 5), 35, "Test #3.10")
    suite.run_test(game1.sum_along_direction((5, 0), (-1, -1), 3), 0, "Test #3.11")
    suite.run_test(game1.sum_along_direction((5, -1), (0, 1), 3), 0, "Test #3.12")
    suite.run_test(game1.sum_along_direction((-1, -1), (1, 1), 3), 0, "Test #3.13")

    # testing the is_over() and is_going() methods
    suite.run_test(game1.is_going(), True, "Test 4.1")
    test_board3 = np.array([[0, 0, 1, 0, -1, 0, 1],
                            [0, 0, 0, 0, -1, 0, 1],
                            [1, 1, 1, -1, -1, 0, -1],
                            [0, 0, 0, 0, 0, -1, 0],
                            [0, -1, 0, 0, -1, 1, -1]])
    game1.change_board(test_board3)
    print(game1)
    suite.run_test(game1.is_over((2, 0), board.PLAYER1, 3), board.PLAYER1, "Test #4.2")
    suite.run_test(game1.is_going(), False, "Test 4.3")
    game1.change_board(test_board3)
    suite.run_test(game1.is_over((2, 1), board.PLAYER1, 3), board.PLAYER1, "Test #4.4")
    game1.change_board(test_board3)
    suite.run_test(game1.is_over((2, 2), board.PLAYER1, 3), board.PLAYER1, "Test #4.5")
    suite.run_test(game1.is_going(), False, "Test 4.6")

    game1.change_board(test_board3)
    suite.run_test(game1.is_over((2, 0), board.PLAYER1, 4), board.GOING, "Test #4.7")
    suite.run_test(game1.is_going(), True, "Test 4.8")

    game1.change_board(test_board3)
    suite.run_test(game1.is_over((0, 4), board.PLAYER2, 3), board.PLAYER2, "Test #4.9")
    game1.change_board(test_board3)
    suite.run_test(game1.is_over((1, 4), board.PLAYER2, 3), board.PLAYER2, "Test #4.10")
    game1.change_board(test_board3)
    suite.run_test(game1.is_over((3, 5), board.PLAYER2, 3), board.PLAYER2, "Test #4.11")
    game1.change_board(test_board3)
    suite.run_test(game1.is_over((4, 6), board.PLAYER2, 3), board.PLAYER2, "Test #4.12")
    suite.run_test(game1.is_going(), False, "Test 4.13")

    game1.change_board(test_board3)
    suite.run_test(game1.is_over((4, 6), board.PLAYER2, 4), board.GOING, "Test #4.14")
    suite.run_test(game1.is_going(), True, "Test 4.15")

    game1.change_board(test_board3)
    suite.run_test(game1.is_over((3, 0), board.PLAYER2, 4), board.GOING, "Test #4.16")
    game1.change_board(test_board3)
    suite.run_test(game1.is_over((-1, -1), board.PLAYER2, 4), board.GOING, "Test #4.17")
    # suite.run()

    test_board4 = np.array([[1, -1, 1, -1, 1, -1, 1],
                            [-1, 1, 1, 1, -1, 1, -1],
                            [1, 1, -1, 1, -1, 1, -1],
                            [1, 1, -1, -1, -1, 1, -1],
                            [1, -1, -1, -1, -1, 1, -1],
                            [1, -1, 1, -1, -1, 1, -1]])
    game1.change_board(test_board4)
    print(game1)
    suite.run_test(game1.is_over((1, 1), board.PLAYER1, 4), board.TIE, "Test #4.18")
    game1.change_board(test_board4)
    suite.run_test(game1.is_over((2, 3), board.PLAYER1, 4), board.TIE, "Test #4.19")
    game1.change_board(test_board4)
    suite.run_test(game1.is_over((2, 2), board.PLAYER2, 4), board.TIE, "Test #4.20")
    suite.run_test(game1.is_going(), False, "Test 4.21")

    # reporting the results of the test
    suite.report_results()
Beispiel #23
0
import reinforcement_learning_model as avm
import tic_tac_toe as ttt

import matplotlib.pyplot as plt

if __name__ == "__main__":

    ttt = ttt.TicTacToe()
    rl_agent_0 = avm.ActionValueModel(epsilon=0.5)
    rl_agent_X = avm.ActionValueModel(epsilon=0.01)

    actions_X = [
        (0, 'X'),
        (1, 'X'),
        (2, 'X'),
        (3, 'X'),
        (4, 'X'),
        (5, 'X'),
        (6, 'X'),
        (7, 'X'),
        (8, 'X'),
    ]

    actions_0 = [
        (0, '0'),
        (1, '0'),
        (2, '0'),
        (3, '0'),
        (4, '0'),
        (5, '0'),
        (6, '0'),
Beispiel #24
0
 def testSetAndGetPosition(self):
     board = tic_tac_toe.TicTacToe()
     self.assertEqual(' ', board.GetPosition(1, 2))
     board.SetPosition('X', 1, 2)
     self.assertEqual('X', board.GetPosition(1, 2))
Beispiel #25
0
 def testWinnerEmptyBoardIsNone(self):
     board = tic_tac_toe.TicTacToe()
     
     self.assertEqual(None, board.Winner())
Beispiel #26
0
 def testGameOverEmptyBoard(self):
     board = tic_tac_toe.TicTacToe()
     
     self.assertFalse(board.GameOver())