Skip to content
Advertisement

Python – TicTacToe

In this question I have to create a TicTacToe game in python which will run in terminal. This is a 2player game and if any player wins by the rules of TicTacToe, Winner = player 1/2 has to be printed.

I’m getting a winner statement when I have three 0’s or 1’s diagonally but I’m not getting winner in other two cases (horizontally and vertically)

Please help find the error in my code

import numpy as np

def create_board():
    return (np.array([[0, 0, 0],
                      [0, 0, 0],
                      [0, 0, 0]]))


def coordinates(board, player):
    i, j, cn = (-1, -1, 0)
    while (i > 3 or i < 0 or j < 0 or j > 3) or (board[i][j] != 0):
        if cn > 0:
            print("Wrong Input! Try Again")
        print("Player {}'s turn".format(player))
        i = int(input("x-coordinates: "))
        j = int(input("y-coordinates: "))
        i = i - 1
        j = j - 1
        cn = cn + 1
    board[i][j] = player
    return board


def row_win(board, player):
    for x in range(len(board)):
        win = True

        for y in range(len(board)):
            if board[x, y] != player:
                win = False
                continue
    return win


def col_win(board, player):
    for x in range(len(board)):
        win = True

        for y in range(len(board)):
            if board[y][x] != player:
                win = False
                continue

    return win


def diag_win(board, player):
    win = True
    y = 0
    for x in range(len(board)):
        if board[x][x] != player:
            win = False
    if win:
        return win
    win = True
    if win:
        for x in range(len(board)):
            y = len(board) - 1 - x
            if board[x][y] != player:
                win = False
    return win


def evaluate(board):
    winner = 0

    for player in [1, 2]:
        if (row_win(board, player) or
                col_win(board, player) or
                diag_win(board, player)):
            winner = player

    if np.all(board != 0) and winner == 0:
        winner = -1
    return winner


def play_game():
    board, winner, counter = create_board(), 0, 1
    print(board)
    while winner == 0:
        for player in [1, 2]:
            board = coordinates(board, player)
            print("Board after " + str(counter) + " move")
            print(board)
            counter += 1
            winner = evaluate(board)
            if winner != 0:
                break
    return winner

print("Winner is: " + str(play_game()))

Advertisement

Answer

Let’s say this is the board:

x x x 
o o x
x o o

Your row_win checks then first row, win remains True. It then proceeds to check the second row, win is set to False. After the last row the function ends up reporting that the player has not won, even though it has.

To fix this change your row_win and col_win to something like this:

def row_win(board, player):
    for row in board:
        if all([cell == player for cell in row]):
            return True
    return False

If all the cells of a row are equal to the player, then the player has won. If none of the rows have all cells equal to the player, then the player has not won.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement