Skip to content

Update TicTacToe.py #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Sanjana-Desh
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant