コード例 #1
0
def play():
  game = TicTacToeGame()

  while not game.isgameover:
    game.play()
    game.print()
    game.is_over()

  game.print_result()
コード例 #2
0
def test_handle_game_end():
    game = TicTacToeGame()
    game.place(8)

    with patch("builtins.input", return_value="y"):
        handle_game_end(game)

    assert game.board[8] == "-"

    with pytest.raises(TicTacToeException):
        with patch("builtins.input", return_value="n"):
            handle_game_end(game)
コード例 #3
0
def play():
  game = TicTacToeGame()
  termino = False
  while not termino:
    game.play() 
    termino = game.is_over()
    game.print()

  game.print_result()
コード例 #4
0
    def train(self):
        episodes_trained = 0
        from tic_tac_toe import TicTacToeGame, TicTacToeBoard

        while episodes_trained < self.n_episodes:

            new_game = TicTacToeGame(
                TicTacToeBoard.create_empty_board(game_config.BOARD_SIZE))
            self.run_episode(new_game)

            episodes_trained += 1

        return self.dqn
コード例 #5
0
def test_place_already_taken():
    game = TicTacToeGame()
    game.place(3)
    game.place(3)

    with patch("sys.stdout", new=io.StringIO()) as stdout:
        game.place(3)
    assert stdout.getvalue() == "Position already taken, please try again:\n"
コード例 #6
0
def test_random_ai_move():
    game = TicTacToeGame()
    with patch("ai_moves.randint") as mock_random:
        mock_random.return_value = 5
        game.random_ai_move()

    with patch("sys.stdout", new=io.StringIO()) as stdout:
        game.place(5)
    assert stdout.getvalue() == "Position already taken, please try again:\n"
コード例 #7
0
def test_game_restart():
    game = TicTacToeGame()

    game.place(8)
    game.restart()

    assert game.steps == 0
    for key in game.board.keys():
        assert game.board[key] == "-"
コード例 #8
0
def test_draw():
    game = TicTacToeGame()

    with patch("ai_moves.randint") as mock_random:
        game.place(5)
        mock_random.return_value = 3
        game.ai_move()

        game.place(7)
        mock_random.return_value = 9
        game.ai_move()

        game.place(6)
        mock_random.return_value = 4
        game.ai_move()

        game.place(2)
        mock_random.return_value = 8
        game.ai_move()

    with patch("sys.stdout", new=io.StringIO()) as stdout:
        game.place(1)

    assert stdout.getvalue().split("\n")[-2] == "Its a TIE!"
コード例 #9
0
def test_is_board_full():
    game = TicTacToeGame()
    assert game.is_board_full() == False

    game.steps = 9
    assert game.is_board_full() == True
コード例 #10
0
def test_place_valid():
    game = TicTacToeGame()
    game.place(3)
    assert game.board[3] == "X"
コード例 #11
0
def test_check_position():
    game = TicTacToeGame()

    with pytest.raises(TicTacToeException):
        game.check_position(0)

    with pytest.raises(TicTacToeException):
        game.check_position(10)

    with pytest.raises(TicTacToeException):
        game.check_position(564)

    with pytest.raises(TicTacToeException):
        game.check_position("SA")

    with pytest.raises(TicTacToeException):
        game.check_position("=")
コード例 #12
0
def test_ai_win__board_not_full():
    game = TicTacToeGame()

    with patch("ai_moves.randint") as mock_random:
        game.place(7)
        mock_random.return_value = 1
        game.ai_move()

        game.place(8)
        mock_random.return_value = 2
        game.ai_move()

        game.place(5)
        mock_random.return_value = 3

        with patch("sys.stdout", new=io.StringIO()) as stdout:
            game.ai_move()

    assert stdout.getvalue().split("\n")[-2] == "Sorry you lost, keep trying!"
コード例 #13
0
def test_board_empty():
    game = TicTacToeGame()

    for key in game.board.keys():
        assert game.board[key] == "-"
コード例 #14
0
def play():

    game = TicTacToeGame()

    while not game.is_game_over:
        game.play()
コード例 #15
0
def test_player_win__board_not_full():
    game = TicTacToeGame()

    with patch("ai_moves.randint") as mock_random:
        game.place(7)
        mock_random.return_value = 1
        game.ai_move()

        game.place(8)
        mock_random.return_value = 2
        game.ai_move()

    with patch("sys.stdout", new=io.StringIO()) as stdout:
        game.place(9)

    assert stdout.getvalue().split("\n")[-2] == "Congratulations! You win!"
コード例 #16
0
def test_game_diag_win():
    game = TicTacToeGame()
    game.place(7)
    game.place(5)
    game.place(3)

    assert game._check_diag_win(game.board, "X") == True
    assert game.check_winner(game.board) == "Player"
    game.restart()

    game.place(9)
    game.place(5)
    game.place(1)

    assert game._check_diag_win(game.board, "X") == True
    assert game.check_winner(game.board) == "Player"
コード例 #17
0
async def ttt(ctx, user: typing.Union[discord.User, str]):

    lb = leaderb()

    # Instantiate the Game unless a Move is being Played
    if not isinstance(user, str):

        if not tttGames.get(ctx.author.id) and not tttGames.get(user.id):
            tttGames[ctx.author.id] = TicTacToeGame(ctx.author.id, user.id)
            tttGames[user.id] = tttGames[ctx.author.id]
            print(tttGames)
        else:
            error1 = discord.Embed(
                title="You or the player you invited are already in a game!")
            await ctx.channel.send(embed=error1)
            return

        start = discord.Embed(
            title="Tic-Tac-Toe Game Started!",
            description=
            "Enter $ttt \'Location\' To Make A Move\nExample: $ttt a1",
            color=15158332)
        await ctx.send(embed=start)
        await ctx.channel.send(tttGames[ctx.author.id].initBoard())
        await ctx.send(f"{ctx.author.mention}, Make your move!")

    else:
        move = user
        if (move == 'help'):
            help = discord.Embed(
                title="Tic-Tac-Toe Commands!",
                description=
                "Use command '$ttt @user' to start the game\nThe game is played on a 3x3 board, use A-C and 1-3 to select a column and row\nUse command '$ttt [col][row]' to make a move, for example '$ttt a1'"
            )
            await ctx.send(embed=help)
            return

        if tttGames.get(ctx.author.id):
            if ctx.author.id == tttGames[ctx.author.id].user:
                if tttGames[ctx.author.id].userTurn == True:
                    await ctx.send(tttGames[ctx.author.id].makeMove(move))
                    if tttGames[ctx.author.id].checkWin == True:
                        winner = tttGames[ctx.author.id].user
                        loser = tttGames[ctx.author.id].opponent
                        winnerName = await client.fetch_user(int(winner))
                        loserName = await client.fetch_user(int(loser))
                        print(winnerName)
                        print(loserName)
                        lb.updateLeaderboard(winner, loser, str(winnerName),
                                             str(loserName))
                else:
                    await ctx.send(f"{ctx.author.mention}, it's not your turn!"
                                   )

            elif ctx.author.id == tttGames[ctx.author.id].opponent:
                if tttGames[ctx.author.id].userTurn == False:
                    await ctx.send(tttGames[ctx.author.id].makeMove(move))
                    if tttGames[ctx.author.id].checkWin == True:
                        winner = tttGames[ctx.author.id].opponent
                        loser = tttGames[ctx.author.id].user
                        winnerName = await client.fetch_user(int(winner))
                        loserName = await client.fetch_user(int(loser))
                        print(winnerName)
                        print(loserName)
                        lb.updateLeaderboard(winner, loser, str(winnerName),
                                             str(loserName))
                else:
                    await ctx.send(f"{ctx.author.mention}, it's not your turn!"
                                   )

            if tttGames[ctx.author.id].checkWin == True or tttGames[
                    ctx.author.id].checkTie == True:
                await ctx.send(embed=goodbyeMessage())
                userId = tttGames[ctx.author.id].user
                opId = tttGames[ctx.author.id].opponent
                del tttGames[userId]
                del tttGames[opId]
                print(tttGames)
        else:
            error2 = discord.Embed(
                title="Start a Tic Tac Toe game ($ttt @user) to make a move!")
            await ctx.send(embed=error2)
    return