Context
computer_move() picks a random free square. It will happily ignore a winning
move, and let you win with three in a row it could have blocked. It is not fun
after the second game.
What to change
File: arcade/games/tictactoe.py
Function: computer_move()
Implement the classic three-rule strategy, in this order:
- Win — if the computer has two in a line and the third is free, take it
- Block — if the player has two in a line and the third is free, take it
- Otherwise — prefer the centre, then a corner, then any free square
The WINNING_LINES constant at the top of the file is what you need for rules 1
and 2. Write a helper that finds a completing square for a given mark, and use
it twice — once for COMPUTER, once for PLAYER.
Keep it beatable. This is not the minimax issue (that is separate). A player
who thinks ahead should still be able to win sometimes.
Acceptance criteria
How to verify locally
python3 -m pytest tests/test_tictactoe.py -v
python3 -m arcade # play several games — it should feel like an opponent
Difficulty
Intermediate. The logic is a loop over WINNING_LINES; getting the rule order
right is what takes the thought.
New here? Comment /claim on this issue and a bot will assign it to you. Then read CONTRIBUTING.md.
Stuck at any point? Comment here saying what you have tried, or come to a PR Debug Clinic (Oct 12 or Oct 21). Getting stuck is the normal experience of learning this, not a sign you are doing it wrong.
Context
computer_move()picks a random free square. It will happily ignore a winningmove, and let you win with three in a row it could have blocked. It is not fun
after the second game.
What to change
File:
arcade/games/tictactoe.pyFunction:
computer_move()Implement the classic three-rule strategy, in this order:
The
WINNING_LINESconstant at the top of the file is what you need for rules 1and 2. Write a helper that finds a completing square for a given mark, and use
it twice — once for
COMPUTER, once forPLAYER.Keep it beatable. This is not the minimax issue (that is separate). A player
who thinks ahead should still be able to win sometimes.
Acceptance criteria
python3 -m pytestpassesHow to verify locally
python3 -m pytest tests/test_tictactoe.py -v python3 -m arcade # play several games — it should feel like an opponentDifficulty
Intermediate. The logic is a loop over
WINNING_LINES; getting the rule orderright is what takes the thought.
New here? Comment
/claimon this issue and a bot will assign it to you. Then read CONTRIBUTING.md.Stuck at any point? Comment here saying what you have tried, or come to a PR Debug Clinic (Oct 12 or Oct 21). Getting stuck is the normal experience of learning this, not a sign you are doing it wrong.