I'm still new to OOP programming and writing compact code so there probably something I'm overlooking, I'm trying to figure out a way to check for vertical wins and diagonal wins in tic-tac-toe, I have horizontal already.
If possible is there a way to incorporate the other two ways into something like what I already have?
#creates the gameboard that stores if spaces are open or not
game_board = [ [0,0,0], [0, 0, 0], [0, 0, 0,] ]
#0 = open space
#1 = O
#2 = X
#check for winners horizontaly
def find_winner():
# y represents the y axis
for y in range(3):
if game_board[y][0] == game_board[y][1] == game_board[y][2] != 0:
#returns if the X or 0 has won
return game_board[y][0]
#returns 0 if no-one has won
return 0
You can add more if
statements like this:
game_board = [ [0, 0, 0],
[0, 0, 0],
[0, 0, 0,] ]
def find_winner():
for y in range(3):
if game_board[y][0] == game_board[y][1] == game_board[y][2] != 0: # Check horizontal
return game_board[y][0]
if : # Check vertical
return game_board[0][y]
if game_board[0][0] == game_board[1][1] == game_board[2][2] != 0 or game_board[0][2] == game_board[1][1] == game_board[2][0] != 0: # Check diagonal
return game_board[1][1]
return 0