コード例 #1
0
ファイル: Play.py プロジェクト: jk370/blackjack-tictactoe
def find_optimal_play(state):
    # Complete 20000 episodes from given state with agent giving first move
    environment = TicTacToe(deepcopy(state))
    agent = play(environment=environment,
                 policy="Q",
                 random_start=False,
                 episodes=20000)

    # Set environment back to given state and let learned agent take best move
    environment = TicTacToe(state)
    agent.set_environment(environment)
    agent.set_epsilon(0)  # Ensures greedy move
    move = agent.find_move()

    # Uncomment to see Q-Values
    '''
    for action in environment.get_available_actions():
        state_val = ''.join(state)
        key = (state_val, action)
        print("Location:", action, "\tValue:", agent.get_Q()[key])
    '''

    # Place move and print board
    environment.place_move(agent.get_icon(), move)
    environment.print_board()
コード例 #2
0
ファイル: unit_test.py プロジェクト: revati-naik/TicTacToe
    def test_func_1(self):
        computer_choice = "X"
        human_choice = "0"
        board_config = np.array([[1, 1, 0], [-1, 1, 1], [-1, 0, 0]])
        print("Running test for win conditions in the board configuration:")
        #Win condition is determined is three move of same kind are in a row, colum or diagonal
        testObject = TicTacToe.TicTacToe(board=board_config)
        testObject.printGrid(computer_choice, human_choice)
        self.assertFalse(testObject.isWinner(user=-1))
        print("Test Output: ", testObject.isWinner(user=-1))

        #Row winning condition
        board_config = np.array([[1, -1, 1], [-1, -1, -1], [0, -1, 0]])
        testObject = TicTacToe.TicTacToe(board=board_config)
        testObject.printGrid(computer_choice, human_choice)
        self.assertTrue(testObject.isWinner(user=-1))
        print("Test Output: ", testObject.isWinner(user=-1))

        #Column winning condition
        board_config = np.array([[1, -1, 0], [1, -1, 0], [1, 1, 0]])
        testObject = TicTacToe.TicTacToe(board=board_config)
        testObject.printGrid(computer_choice, human_choice)
        self.assertTrue(testObject.isWinner(user=1))
        print("Test Output: ", testObject.isWinner(user=1))

        #Diagonal winning condition
        board_config = np.array([[-1, 0, 1], [-1, -1, 0], [0, -1, -1]])
        testObject = TicTacToe.TicTacToe(board=board_config)
        testObject.printGrid(computer_choice, human_choice)
        self.assertTrue(testObject.isWinner(user=-1))
        print("Test Output: ", testObject.isWinner(user=-1))
コード例 #3
0
ファイル: tictactoegame.py プロジェクト: noah415/tic-tac-toe
def draw(screen):
    draw = True
    start = True
    one_player = False
    two_player = False

    start_selector_num = 0
    board_selector = [0, 0]
    cursor = [10, 32]
    p1_game = TicTacToe.TicTacToe()
    p2_game = TicTacToe.TicTacToe()

    draw_controller = Draw_Controller(draw, start, one_player, two_player,
                                      start_selector_num, board_selector,
                                      cursor, p1_game, p2_game)

    while draw_controller.draw:
        draw_controller.selector = screen.getch()
        screen.clear()

        #environment switcher
        environment_switch(draw_controller, screen)

        #this is for the different environment controls
        environment_controller(draw_controller, screen)

        #this is the options control printer
        option_switch(draw_controller, screen)

        #this is the board player turn controller

        screen.refresh()
コード例 #4
0
    def play(self):
        print("Setting up game...\n")
        alpha = -1
        beta = 1

        self.game = TicTacToe()
        self.player2 = AdversarialSearch(self.game.copy(), "O", self.depth)
        window = Tk()
        window.rowconfigure((0, 3), weight=1)
        window.columnconfigure((0, 2), weight=1)
        for i in range(0, 3):
            for j in range(0, 3):
                print(str(i))
                b = Button(window,
                           text="",
                           pady=2,
                           width=5,
                           height=5,
                           command=lambda a=i, b=j, : self.takeTurn(a, b))
                b.grid(row=i, column=j)
                print(self.buttons)
                self.buttons[i][j] = b

        buttonR = Button(window,
                         text="RESET",
                         width=15,
                         height=5,
                         command=self.reset)
        buttonR.grid(row=3, column=0, columnspan=3)
        window.mainloop()
コード例 #5
0
    def play(self):
        print("Setting up game...\n")
        game = TicTacToe()
        alpha = -1
        beta = 1
        player1 = AlphaBetaSearch(game.copy(), "X", alpha, beta)
        player2 = AlphaBetaSearch(game.copy(), "O", alpha, beta)
        while game.isGameOver() == " ":

            print("Player 1 is deciding\n")

            player1.state = game.copy()
            (t, (x1, y1)) = player1.maxValueAB(player1.state, alpha, beta)
            game.setPiece(x1, y1, "X")

            print(game)
            if game.isGameOver() != " ":
                break

            print("Player 2 is deciding\n")

            player2.state = game.copy()
            (t, (x2, y2)) = player2.minValueAB(player2.state, alpha, beta)
            game.setPiece(x2, y2, "O")
            print(game)

        status = game.isGameOver()

        if status == "TIE":
            print("TIE GAME")
        else:
            print(status + " WINS")
コード例 #6
0
 def checaVitoriaVertice(self, vertice, maquinaInicia):
     """Checa se um vértice representa vitória, dependendo do nome do jogo se a maquina iniciou"""
     qntSimbolos = 0
     for caractere in vertice.info:
         if caractere != '0':
             qntSimbolos += 1
     if qntSimbolos % 2 == 0:
         qntSimbolos = 'par'
     else:
         qntSimbolos = 'impar'
     if self.nomeJogo == 'Wild':
         jogo = Wild()
     elif self.nomeJogo == 'Misere':
         jogo = Misere()
     else:
         jogo = TicTacToe()
     jogo.tabuleiro.tabuleiro = vertice.info
     ganhador = jogo.tabuleiroFinalizado()
     if ganhador:
         if self.nomeJogo == 'Misere' and maquinaInicia and qntSimbolos == 'par':
             return 'Vitoria'
         elif self.nomeJogo == 'Misere' and not maquinaInicia and qntSimbolos == 'impar':
             return 'Vitoria'
         elif self.nomeJogo == 'Wild' and maquinaInicia and qntSimbolos == 'impar':
             return 'Vitoria'
         elif self.nomeJogo == 'Wild' and not maquinaInicia and qntSimbolos == 'par':
             return 'Vitoria'
         elif self.nomeJogo == 'TicTacToe' and maquinaInicia and ganhador == 'X':
             return 'Vitoria'
         elif self.nomeJogo == 'TicTacToe' and not maquinaInicia and ganhador == 'O':
             return 'Vitoria'
         else:
             return 'Derrota'
     else:
         return 'Empate'
コード例 #7
0
ファイル: PlayTTT.py プロジェクト: SSJLL/AI-Groupwork
    def play(self):
        # setup board and players
        print("Setting up game...\n")
        game = TicTacToe()

        player1 = AdversarialSearch(game.copy(),"X", self.depth)
        player2 = AdversarialSearch(game.copy(),"O", self.depth)
        while game.isGameOver() == " ":

            # player 1's turn
            print("Player 1 is deciding\n")

            player1.state = game.copy()
            (t,(x1,y1)) = player1.maxValue(player1.state)
            game.setPiece(x1,y1,"X")

            print(game)
            if game.isGameOver() != " ":
                break

            # player 2's turn
            print("Player 2 is deciding\n")

            player2.state = game.copy()
            (t,(x2,y2)) = player2.minValue(player2.state)
            game.setPiece(x2,y2,"O")
            print(game)

        status = game.isGameOver()

        if status == "TIE":
            print("TIE GAME")
        else:
            print(status + " WINS")
コード例 #8
0
def initgame():
	global ttt

	if not ttt:
		ttt = TicTacToe()
	else:
		ttt.reset()
コード例 #9
0
 def playerVsPlayer(self):
     self.currentScreen = self.gameScreen
     self.play = True
     self.playAI = False
     self.clicked[12] = False
     self.buttonClick = [False for _ in range(self.size * self.size)]
     self.game = TicTacToe(self.size, self.numOfPlayers, True, 10, False, False, self)
コード例 #10
0
 def test_checkBoard_full_game_o_win_with_bad_inputs(self):
     TicTac = TicTacToe.TicTacToe()
     TicTac.nextMove([0, 0])  #x [0,0]
     TicTac.nextMove([1, 0])  #o [1,0]
     TicTac.nextMove([1, 0])  # bad input
     TicTac.nextMove([0, 1])  #x [0,1]
     TicTac.nextMove([2, 0])  #o [2,0]
     TicTac.nextMove([1, 1])  #x [1,1]
     TicTac.nextMove([1, 0])  # bad input
     TicTac.nextMove([2, 1])  #o [2,1]
     TicTac.nextMove([1, 2])  #x [1,2]
     TicTac.nextMove(5)  # bad input
     TicTac.nextMove([5, 0])  # bad input
     TicTac.nextMove([2, 2])  #o [2,2]
     self.assertEqual(TicTac.checkBoard('x'),
                      enums.game_state.game_is_not_finished)
     self.assertNotEqual(TicTac.checkBoard('o'), enums.game_state.x_won)
     self.assertNotEqual(TicTac.checkBoard('x'), enums.game_state.x_won)
     self.assertNotEqual(TicTac.checkBoard('x'), enums.game_state.draw)
     self.assertNotEqual(TicTac.checkBoard('o'), enums.game_state.draw)
     self.assertNotEqual(TicTac.checkBoard('x'),
                         enums.game_state.game_is_not_finished)
     self.assertNotEqual(TicTac.checkBoard('o'),
                         enums.game_state.game_is_not_finished)
     self.assertNotEqual(TicTac.checkBoard('x'), enums.game_state.o_won)
     self.assertEqual(TicTac.checkBoard('o'), enums.game_state.o_won)
コード例 #11
0
 def test_vertical(self):
     vert_board = TicTacToe()
     # Testing that cross is able to win the game vertically
     self.assertEqual(TicTacToe.STATES(1),
                      vert_board.place_marker("x", 0, 0))
     self.assertEqual(TicTacToe.STATES(0),
                      vert_board.place_marker("o", 0, 1))
     self.assertEqual(TicTacToe.STATES(1),
                      vert_board.place_marker("x", 1, 0))
     self.assertEqual(TicTacToe.STATES(0),
                      vert_board.place_marker("o", 1, 1))
     self.assertEqual(TicTacToe.STATES(3),
                      vert_board.place_marker("x", 2, 0))
     # Testing that naught is able to win the game vertically
     self.assertEqual(TicTacToe.STATES(1),
                      vert_board.place_marker("x", 0, 0))
     self.assertEqual(TicTacToe.STATES(0),
                      vert_board.place_marker("o", 0, 2))
     self.assertEqual(TicTacToe.STATES(1),
                      vert_board.place_marker("x", 1, 0))
     self.assertEqual(TicTacToe.STATES(0),
                      vert_board.place_marker("o", 1, 2))
     self.assertEqual(TicTacToe.STATES(1),
                      vert_board.place_marker("x", 0, 1))
     self.assertEqual(TicTacToe.STATES(4),
                      vert_board.place_marker("o", 2, 2))
コード例 #12
0
 def test_diagonal_forward(self):
     diag_board = TicTacToe()
     # Testing that cross is able to win the game diagonally
     self.assertEqual(TicTacToe.STATES(1),
                      diag_board.place_marker("x", 0, 2))
     self.assertEqual(TicTacToe.STATES(0),
                      diag_board.place_marker("o", 1, 0))
     self.assertEqual(TicTacToe.STATES(1),
                      diag_board.place_marker("x", 1, 1))
     self.assertEqual(TicTacToe.STATES(0),
                      diag_board.place_marker("o", 0, 1))
     self.assertEqual(TicTacToe.STATES(3),
                      diag_board.place_marker("x", 2, 0))
     # Testing that naught is able to win the game diagonally
     self.assertEqual(TicTacToe.STATES(1),
                      diag_board.place_marker("x", 1, 0))
     self.assertEqual(TicTacToe.STATES(0),
                      diag_board.place_marker("o", 0, 2))
     self.assertEqual(TicTacToe.STATES(1),
                      diag_board.place_marker("x", 0, 1))
     self.assertEqual(TicTacToe.STATES(0),
                      diag_board.place_marker("o", 1, 1))
     self.assertEqual(TicTacToe.STATES(1),
                      diag_board.place_marker("x", 2, 2))
     self.assertEqual(TicTacToe.STATES(4),
                      diag_board.place_marker("o", 2, 0))
コード例 #13
0
def main():
    output_string = "\nHello!\nThis is a game of Tic Tac Toe\nPlayer 1 uses \"X\" and Player 2 uses \"O\"\nEnjoy the game!\n\n"

    for char in output_string:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(.05)

    game = TicTacToe()
    player1_turn = True
    player_char = ""

    while not game.check_status():
        if player1_turn:
            print("Player 1")
            player_char = "X"
        else:
            print("Player 2")
            player_char = "O"

        player1_turn = not player1_turn

        game.player_turn(player_char)
        print("\n\n")
        game.print_game()
        print("\n\n")

    if (player1_turn):
        print("Player 2 win!!")
    else:
        print("Player 1 win!!")
コード例 #14
0
 def playerVsAI(self):
     self.currentScreen = self.gameScreen
     self.play = True
     self.playAI = True
     self.clicked[12] = False
     self.buttonClick = [False for _ in range(self.size * self.size)]
     self.game = TicTacToe(self.size, 2, self.playerIsFirst, 10, False, True, self)
コード例 #15
0
	def __init__(self, address, port, data_size):
		self.data_size = data_size
		self._createTcpIpSocket()
		self._bindSocketToThePort(address, port)
		self.numberGame = NumberGame(0,100)
		self.tttGame = TicTacToe(0)
		self.state = [State.NO_GAME]
コード例 #16
0
 def test_has_board_attr(self):
     self.test_has_TicTacToe_constructor()
     b = TicTacToe.TicTacToe()
     self.assertTrue(
         hasattr(b, 'board'),
         msg=
         'You should have an attribute named board in the TicTacToe class.')
コード例 #17
0
 def test_reset(self):
     reset_board = TicTacToe()
     # Creating draw to fill board
     self.assertEqual(TicTacToe.STATES(1),
                      reset_board.place_marker("x", 0, 0))
     self.assertEqual(TicTacToe.STATES(0),
                      reset_board.place_marker("o", 1, 1))
     self.assertEqual(TicTacToe.STATES(1),
                      reset_board.place_marker("x", 0, 2))
     self.assertEqual(TicTacToe.STATES(0),
                      reset_board.place_marker("o", 0, 1))
     self.assertEqual(TicTacToe.STATES(1),
                      reset_board.place_marker("x", 2, 1))
     self.assertEqual(TicTacToe.STATES(0),
                      reset_board.place_marker("o", 1, 2))
     self.assertEqual(TicTacToe.STATES(1),
                      reset_board.place_marker("x", 1, 0))
     self.assertEqual(TicTacToe.STATES(0),
                      reset_board.place_marker("o", 2, 0))
     self.assertEqual(TicTacToe.STATES(2),
                      reset_board.place_marker("x", 2, 2))
     # Checking variables and game state after draw
     self.assertEqual([["-", "-", "-"], ["-", "-", "-"], ["-", "-", "-"]],
                      reset_board.board)
     self.assertEqual(0, reset_board.tiles_filled)
     self.assertEqual(0, reset_board.curr_state)
     self.assertEqual(TicTacToe.STATES(1),
                      reset_board.place_marker("x", 0, 0))
コード例 #18
0
def train(cold=True, name='data.csv', n=10000):
    """Train the computer and store memory into training data file. If cold,
    computer starts with no prior training data. If warm, computer starts
    with whatever memory is in training data"""
    
    ttt = TicTacToe.TicTacToe()
    
    if cold:
        
        print('Stochastic\n')
        wins1 = ttt.CvC(iterations=int(0.2*n), rand=True)
        print('Learning\n')
        wins2 = ttt.CvC(iterations=int(0.8*n), rand=False)
        ttt.comp.store_dta(name)
        
        wins = wins1.append(wins2, ignore_index=True)
        x = wins.groupby(wins.index//100).sum()
        plt.plot(x['win'])
        plt.show()
    
    else:
        
        print('Loading...')
        ttt.comp.load_dta(name)
        
        wins = ttt.CvC(iterations=n, rand=False)
        ttt.comp.store_dta(name)
        
        x = wins.groupby(wins.index//100).sum()
        plt.plot(x['win'][:-1])
        plt.show()
コード例 #19
0
 def test_nextMove_changes_to_board(self):
     TicTac = TicTacToe.TicTacToe()
     TicTac.nextMove([0, 0])
     TicTac.nextMove([0, 1])
     board = [[' ' for x in range(3)] for x in range(3)]
     board[0][0] = "x"
     board[0][1] = "o"
     self.assertEqual(TicTac.getBoard(), board)
コード例 #20
0
    def test_check_status3(self):
        game = TicTacToe()
        
        game.puzzle = [[" ", " ", " "],
                       [" ", " ", " "],
                       [" ", " ", " "]]

        self.assertFalse(game.check_status())
コード例 #21
0
    def test_check_status5(self):
        game = TicTacToe()
        
        game.puzzle = [["X", "O", "O"],
                       [" ", "O", " "],
                       [" ", "O", " "]]

        self.assertTrue(game.check_status())
コード例 #22
0
 def test_get_player_char_no_params(self):
     self.test_get_player_char_exists()
     t = TicTacToe.TicTacToe()
     self.assertTrue(
         len(signature(t.get_player_char).parameters) == 0,
         msg=
         'get_player_char method in TicTacToe should have only self as a paramter '
     )
コード例 #23
0
 def test_nextMove_move_if_off_the_board(self):
     TicTac = TicTacToe.TicTacToe()
     self.assertEqual(TicTac.nextMove([50, 50]),
                      enums.error.different_position)
     self.assertEqual(TicTac.nextMove([-2, 1]),
                      enums.error.different_position)
     self.assertEqual(TicTac.nextMove([-5, -5]),
                      enums.error.different_position)
コード例 #24
0
    def test_check_status2(self):
        game = TicTacToe()
        
        game.puzzle = [["O", "X", "O"],
                       ["X", "X", "O"],
                       ["O", "O", "X"]]

        self.assertFalse(game.check_status())
コード例 #25
0
def initgame():
    global ttt

    if not ttt:
        ttt = TicTacToe()
        ttt.playerTurn = 1  # Setting this manually.
    else:
        ttt.reset()
コード例 #26
0
ファイル: PlayTTT.py プロジェクト: SSJLL/AI-Groupwork
 def reset(self):
     self.game = TicTacToe()
     alpha = -1
     beta = 1
     self.player2 = AdversarialSearch(self.game.copy(),"O", self.depth)
     for i in range(0,3):
         for j in range(0,3):
             self.buttons[i][j]["text"] = ""
コード例 #27
0
 def test_has_TicTacToe_constructor(self):
     self.test_has_TicTacToe_class()
     b = TicTacToe.TicTacToe()
     self.assertTrue(
         hasattr(b, '__init__'),
         msg=
         'Could not find constructor ("__init__") method in TicTacToe class'
     )
コード例 #28
0
 def test_get_player_spot_5then6(self):
     self.test_get_player_spot_no_params()
     expected = 6
     t = TicTacToe.TicTacToe()
     t.board.setO(5)
     toenter = '99\n-3\n22\n5\n6'
     with capture_io(toenter):
         actual = t.get_player_spot()
     self.assertEqual(expected, actual)
コード例 #29
0
    def test_checkrows1(self):
        game = TicTacToe()
        
        for i in range(3):
            game.puzzle[0][i] = "X"

        self.assertTrue(game.check_row())
        self.assertFalse(game.check_col())
        self.assertFalse(game.check_diag())
コード例 #30
0
 def test_get_player_spot_5then6then7(self):
     self.test_get_player_spot_no_params()
     expected = 7
     t = TicTacToe.TicTacToe()
     t.board.setO(5)
     t.board.setX(6)
     toenter = '5\n6\n7'
     with capture_io(toenter):
         actual = t.get_player_spot()
     self.assertEqual(expected, actual)