0

How to simplify this Tic Tac Toe program ?

number = 3 matrix = [] for i in range(number): string = input().split() matrix.append(string) if (matrix[0][0] == "O" and matrix[0][1] == "O" and matrix[0][2] == "O") or (matrix[1][0] == "O" and matrix[1][1] == "O" and matrix[1][2] == "O") or (matrix[2][0] == "O" and matrix[2][1] == "O" and matrix[2][2]) == "O": print("Abhinav Wins") elif (matrix[0][0] == "X" and matrix[0][1] == "X" and matrix[0][2] == "X") or (matrix[1][0] == "X" and matrix[1][1] == "X" and matrix[1][2] == "X") or (matrix[2][0] == "X" and matrix[2][1] == "X" and matrix[2][2]) == "X": print("Anjali Wins") elif (matrix[0][0] == "O" and matrix[1][0] == "O" and matrix[2][0] == "O") or (matrix[0][1] == "O" and matrix[1][1] == "O" and matrix[2][1] == "O") or (matrix[0][2] == "O" and matrix[1][2] == "O" and matrix[2][2] == "O"): print("Abhinav Wins") elif (matrix[0][0] == "X" and matrix[1][0] == "X" and matrix[2][0] == "X") or (matrix[0][1] == "X" and matrix[1][1] == "X" and matrix[2][1] == "X") or (matrix[0][2] == "X" and matrix[1][2] == "X" and matrix[2][2] == "X"): print("Anjali Wins") elif (matrix[0][0] == "O" and matrix[1][1] == "O" and matrix[2][2] == "O") or (matrix[0][2] == "O" and matrix[1][1] and matrix[2][0]) == "O": print("Abhinav Wins") elif (matrix[0][0] == "X" and matrix[1][1] == "X" and matrix[2][2] == "X") or (matrix[0][2] == "X" and matrix[1][1] and matrix[2][0]) == "X": print("Anjali Wins") else: print("Tie")

16th Oct 2022, 3:37 PM
PRAVEEN
PRAVEEN - avatar
3 Antworten
+ 1
Yeah there are a lot you can consider to make this more efficient. first, you should have a function to check if the game is finished and has a winning move. a winning game is three repeated character in a row, column or diagonal. so you can loop over each of those and validate if they are all identical I can't add much more right now but I hope this helps!
16th Oct 2022, 6:06 PM
Apollo-Roboto
Apollo-Roboto - avatar
0
No code is linked here
16th Oct 2022, 3:57 PM
Apollo-Roboto
Apollo-Roboto - avatar
0
sry.. now pasted code
16th Oct 2022, 4:06 PM
PRAVEEN
PRAVEEN - avatar