Skip to content

Missleading TestCase [Projects -> Reinforcement Learning for Tic-Tac-Toe: From Minimax to DQN -> Step 017] #594

Description

@SAHAYAASHIKLIBU

As per the description of the problem. The user need to print "illegal move, try again". Still, the test case pass only if not make a print statement.

Image Image

Correct solution as per problem description

import numpy as np

def play_interactive_game():
    """Play a full game with two humans entering moves via stdin and return the final status."""

    board = np.zeros((3, 3), dtype=int)
    display = np.full((3, 3), ".", dtype=str)
    player = 1

    def print_board():
        for row in display:
            print(" ".join(row))

    def win(player):
        return (
            np.any(np.all(board == player, axis=0)) or  # columns
            np.any(np.all(board == player, axis=1)) or  # rows
            np.all(np.diag(board) == player) or         # main diagonal
            np.all(np.diag(np.fliplr(board)) == player) # other diagonal
        )

    while True:
        print_board()

        row, col = map(int, input().split())

        # Illegal move
        if board[row, col] != 0:
            print("illegal move, try again")

        board[row, col] = player
        display[row, col] = "X" if player == 1 else "O"

        if win(player):
            print_board()
            return "X_win" if player == 1 else "O_win"

        if np.all(board != 0):
            print_board()
            return "draw"

        player *= -1

The solution that actually pass

`import numpy as np

def play_interactive_game():
    """Play a full game with two humans entering moves via stdin and return the final status."""

    board = np.zeros((3, 3), dtype=int)
    display = np.full((3, 3), ".", dtype=str)
    player = 1

    def print_board():
        for row in display:
            print(" ".join(row))

    def win(player):
        return (
            np.any(np.all(board == player, axis=0)) or  # columns
            np.any(np.all(board == player, axis=1)) or  # rows
            np.all(np.diag(board) == player) or         # main diagonal
            np.all(np.diag(np.fliplr(board)) == player) # other diagonal
        )

    while True:
        print_board()

        row, col = map(int, input().split())

        # Illegal move
        if board[row, col] != 0:
            continue

        board[row, col] = player
        display[row, col] = "X" if player == 1 else "O"

        if win(player):
            print_board()
            return "X_win" if player == 1 else "O_win"

        if np.all(board != 0):
            print_board()
            return "draw"

        player *= -1`

Actual change

   ```# Illegal move
    if board[row, col] != 0:
        continue```
     if board[row, col] != 0:
         print("illegal move, try again")```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions