Python-Projects icon indicating copy to clipboard operation
Python-Projects copied to clipboard

Update TicTacToe.py

Open Sanjana-Desh opened this issue 5 months ago • 0 comments

Problem Summary: Game Crashing when board is full in certain scenarios like:

X | O | X

O | X | X

O | X | O

The compMove() function is responsible for finding a move for the computer. It looks for winning moves, blocking moves, and selects from available corners, center, or edges.

When the board is full, there are no available moves left. In that case, the compMove() function reached the end without returning anything, which means it returned None by default.

In the main() function, after calling compMove(), the program immediately tried to insert the letter "O" at the returned position using insertLetter("O", move). But if move was None, this resulted in insertLetter("O", None), which caused a crash. This is because Python lists do not allow None as a valid index — it must be an integer.

How I Solved It:

I modified the compMove() function so that it always returns a value. If no valid moves are available (i.e., the board is full), the function now explicitly returns None at the end. This makes it clear that the computer has no moves left.

I updated the main() function to check whether the returned move is None before trying to use it. If move is None, the program prints "Tie game" and breaks out of the game loop, ending the game safely without crashing. Otherwise, it proceeds to insert the computer's move normally.

Sanjana-Desh avatar Jul 31 '25 14:07 Sanjana-Desh