Skip to content

Instantly share code, notes, and snippets.

@jsquire
Last active August 7, 2025 17:23
Show Gist options
  • Select an option

  • Save jsquire/f3f9d40529014e153a4bd94d45fb8249 to your computer and use it in GitHub Desktop.

Select an option

Save jsquire/f3f9d40529014e153a4bd94d45fb8249 to your computer and use it in GitHub Desktop.
Numeric Tic-Tac-Toe

Numeric Tic-Tac-Toe

Summary

Numeric Tic-Tac-Toe is a strategic variant of the classic Tic-Tac-Toe game that replaces traditional X's and O's with numbers. This mathematical twist on the timeless game was developed to add a layer of numerical strategy and mental arithmetic to the familiar 3x3 grid format.

The game originated as an educational tool to help students practice basic arithmetic while engaging in strategic thinking. Unlike traditional Tic-Tac-Toe where the objective is simply to get three symbols in a row, Numeric Tic-Tac-Toe introduces mathematical constraints that require players to think several moves ahead while considering numerical relationships.

Rules

The game is played on a standard 3x3 grid with the following rules:

  1. Player Assignment: One player uses odd numbers (1, 3, 5, 7, 9) and the other uses even numbers (2, 4, 6, 8).

  2. Gameplay: Players take turns placing their numbers on empty squares of the grid.

  3. Number Usage: Each number can only be used once during the game.

  4. Winning Condition: Instead of getting three identical symbols in a row, a player wins by creating a line (horizontal, vertical, or diagonal) where the three numbers sum to exactly 15.

  5. Strategic Element: Since players have limited numbers and must reach the target sum of 15, careful planning is required to both create winning opportunities and block opponents.

  6. Draw Condition: If all squares are filled and no player has achieved a sum of 15 in any line, the game is a draw.

Scenario

You have:

  • A standard 3x3 tic-tac-toe board, represented as a single-dimensional array

    • Indexes 0-2 represent the first row of the board
    • Indexes 3-5 represent the second row of the board
    • Indexes 6-8 represent the third row of the board
  • A player type, represented as an enumeration with members for

    • Odd
    • Even
  • A set of tokens for the odd player and for the even player, represented as a hashset

    • The hashsets have built-in operations for Add, Remove, Contains
  • A Move, represented as a custom type that tracks

    • The player associated with the move
    • The board index of the move
    • The token placed on the board

Goals

  1. We want to implement a function that can answer the question "has the game been won?".

  2. We want to implement a function that suggests the best possible move for one of the players, such as a bot player would use.

  • It should accept a player type and find the best move for that player, given the current state of the game
  • If the player has no moves left that can be made, it should not make a suggestion
  • If the only move left for the player would result in an immediate loss, it should suggest the move
  • It should return a Move, if it can make a reasonable suggestion or null if no move can be made
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment