class ClassicTicTacToe:
def __init__(self):
'''
Initializes an empty Numerical Tic Tac Toe board.
Inputs: none
Returns: None
'''
self.board = [] # list of lists, where each internal list represents a row
self.size = 3 # number of columns and rows of board
# populate the empty squares in board with empty string
for i in range(self.size):
row = []
for j in range(self.size):
row.append(' ')
self.board.append(row)
#print(self.board)
def drawBoard(self):
l1=' 0 1 2'
l2=' 0 %s | %s | %s' % (self.board[0][0],self.board[0][1],self.board[0][2])
l3=' '+'-'*11
l4=' 1 %s | %s | %s' % (self.board[1][0],self.board[1][1],self.board[1][2])
l5=l3
l6=' 2 %s | %s | %s' % (self.board[2][0],self.board[2][1],self.board[2][2])
print(l1,l2,l3,l4,l5,l6,sep='\n')
'''
Displays the current state of the board, formatted with column and row
indicies shown.
Inputs: none
Returns: None
'''
def squareIsEmpty(self, row, col):
if self.board[row][col]==' ':
return True
else:
return False
'''
Checks if a given square is empty, or if it already contains a number
greater than 0.
Inputs:
row (int) - row index of square to check
col (int) - column index of square to check
Returns: True if square is empty; False otherwise
'''
# mark is a string for the users input of x or o
def update(self, row, col, mark):
if self.squareIsEmpty(row,col)==True:
self.board[row][col]=mark.lower()
return True
if self.squareIsEmpty(row,col)==False:
return False
'''
Assigns the integer, num, to the board at the provided row and column,
but only if that square is empty.
Inputs:
row (int) - row index of square to update
col (int) - column index of square to update
num (string) - entry to place in square
Returns: True if attempted update was successful; False otherwise
'''
def boardFull(self):
'''
Checks if the board has any remaining empty squares.
Inputs: none
Returns: True if the board has no empty squares (full); False otherwise
'''
nonempty_list=[]
# TO DO: delete pass and complete method
for i in self.board:
# alternatively this could be range(len(self.board))
for j in range(0,3):
if i[j]!=' ':
nonempty_list.append(i[j])
if len(nonempty_list)<self.size**2:
return False
return True
def isWinner(self):
for i in range(len(self.board)):
if self.board[i][0]+self.board[i][1]+self.board[i][2]==(3*'x') or (3*'o'):
#return self.board[i][0]+self.board[i][1]+self.board[i][2]
return True
if self.board[0][i]+self.board[1][i]+self.board[2][i]==(3*'x') or (3*'o'):
#return self.board[0][i]+self.board[1][i]+self.board[2][i]
return True
if self.board[0][0]+self.board[2][2]+self.board[1][1]==(3*'x') or (3*'o'):
#return self.board[0][0]+self.board[2][2]+self.board[1][1]
return True
if self.board[0][2]+self.board[2][0]+self.board[1][1]==(3*'x') or (3*'o'):
#return self.board[0][2]+self.board[2][0]+self.board[1][1]
return True
return False
def isNum(self):
if self.choice=='num':
return False
else:
return True
'''
Checks whether the current player has just made a winning move. In order
to win, the player must have just completed a line (of 3 squares) that
adds up to 15. That line can be horizontal, vertical, or diagonal.
Inputs: none
Returns: True if current player has won with their most recent move;
False otherwise
'''
if __name__ == "__main__":
new_game=ClassicTicTacToe()
new_game.update(1,1,'x')
new_game.update(2,0,'x')
new_game.update(0,0,'x')
new_game.drawBoard()
print(new_game.isWinner())
Creating a version of classic tic Tac Toe with X's and O's. The method isWin checks if we have any win conditions (3 in a row of either x or o) and if we have any it returns true if not it returns false. I've set up a scenario in which it should return False yet it returns True can anyone find why? I'm not sure what the issue is