Skip to content

Tic Tac Toe's computer never tries to win or block #18

Description

@pushpam2404

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:

  1. Win — if the computer has two in a line and the third is free, take it
  2. Block — if the player has two in a line and the third is free, take it
  3. 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

  • The computer takes an immediate win when one is available
  • It blocks the player's immediate win when it cannot win itself
  • Winning is preferred over blocking
  • It prefers the centre when no win or block exists
  • It never picks an occupied square
  • Tests cover: available win, available block, win beats block, centre preference
  • python3 -m pytest passes

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.

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

difficulty: intermediateRequires reading existing codedomain: os-devxOpen Source & Developer ExperienceenhancementNew feature or requesthacktoberfestCounts toward Hacktoberfest 2026status: claimedAssigned to a contributor — do not start work on this

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions