Skip to content
Advertisement

Python – TicTacToe

In this question I have to create a TicTacToe game in python which will run in terminal. This is a 2player game and if any player wins by the rules of TicTacToe, Winner = player 1/2 has to be printed.

I’m getting a winner statement when I have three 0’s or 1’s diagonally but I’m not getting winner in other two cases (horizontally and vertically)

Please help find the error in my code

JavaScript

Advertisement

Answer

Let’s say this is the board:

JavaScript

Your row_win checks then first row, win remains True. It then proceeds to check the second row, win is set to False. After the last row the function ends up reporting that the player has not won, even though it has.

To fix this change your row_win and col_win to something like this:

JavaScript

If all the cells of a row are equal to the player, then the player has won. If none of the rows have all cells equal to the player, then the player has not won.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement