Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.