For a project of mine, I have to do a tic-tac-toe solver, and I would like to just ask for some help optimizing some code.
Task: Depending on the winner( x or o) , return the appropriate result
How I did It: I checked Horizontal, vertical, and diagonal strategies for the result
What I need help with : I am mostly repeating the same code, but different if statement for each case, and I am just wondering if there is a more universal method to do this.
Current Code: (Diagonal win)
if diagcounter==size and (board[0][0]=="x") :
print("Win Diagonally")
return "X has won"
elif diagcounter==size and (board[0][0]=="o"):
print("Win Diagonally")
Horizontal win:
if vertcounter==size and board[0][x]=="x":
print("Win vertically")
return "X has won"
elif vertcounter==size and board[0][x]=="o":
print("Win vertically")
return "O has won"
As you can see, they are almost the same, but since I need to check the letter at the spot, I don`t know how to optimize it.