Why am I unable to run my code for a TicTacToe game using numpy arrays in Jupyter notebook?

13 Views Asked by At

I am running my code on Jupyter. My goal is to learn basics of Python while making a Tic Tac Toe game using an n-dimensional numpy array whose order is arbitrary. I debugged my code over and over again. Finally I have a code that doesn't show any error but it does not seem to show any output either. I am not able to understand why the program is not running. I am still a beginner so it would be helpful if you could give me some tips on how to improve my code and logic. Thank you for your time.

My code:

 import numpy as np


 class TicTac:
 def __init__(self, row, column):
    self.row = row
    self.column = column
    self.board = np.full((self.row, self.column), " ", dtype=str)
    self.game_over = False
    self.winner = None
    self.mark = "X"

def check(self, arr): #function to check whether all the elements of an array are the same
    if np.all(arr == self.mark):
        return True
    else:
        return False

def game_play(self):
    x, y = map(int, input("Enter mark location").split(" ")) #input from user
    print(self.board)
    self.board[x][y] = self.mark
    if (
        self.check(self.board[x, :])
        or self.check(self.board[:, y])
        or self.check(np.diag(self.board))
        or self.check(np.diag(np.fliplr(self.board))) #Condition for the game to get over
    ):
        self.winner == self.mark
        self.game_over is True
        print("{} is the winner".format(self.mark))
    elif all(" " not in row for row in self.board): #Condition for a draw
        self.game_over is True
        print("Its a draw")

    if self.mark == "X": #If the game doesn't finish, the mark variable is updated
        self.mark == "O"
    else:
        self.mark == "X"
    return


order = int(input("Enter the order of the gameboard"))
A = TicTac(order, order)
while A.game_over is False:
    A.game_play()

The basic logic of the code: I create a game board which is essentially a n-dimensional string array filled with spaces.I have a boolean variable called game_over. When the variable is false, my game_play function is called in a while loop. The game_play function would get the input from the user. The game finishes once the check function returns True or there is no space in the array by setting the game_over variable as True. Therefore, the game_play function cannot be called again because the game is over.

I am not able to produce any output or error for the code above in Jupyter.

0

There are 0 best solutions below