A terminal-based Reversi game in C where you play against an AI opponent. Supports configurable board sizes from 4x4 up to 26x26.
Reversi is a two-player strategy game played on a square board. Black moves first.
- Objective: End the game with more pieces of your colour than your opponent.
- Placing a piece: You place a piece on an empty square. If your piece sandwiches one or more opponent pieces in a straight line (horizontal, vertical, or diagonal), those opponent pieces are flipped to your colour.
- End of game: The game ends when the board is full or neither player has a valid move.
- Winner: The player with the most pieces wins. A tie occurs if both have the same count.
reversi-game-algorithm/
├── src/
│ ├── main.c # Game loop, turn management, gameOver, determineWinner
│ ├── board.c # initializeBoard, printBoard, positionInBounds
│ ├── moves.c # checkLegalInDirection, isLegalMove, makeMoveFull,
│ │ # findAvailableMoves, countFlips
│ └── ai.c # makeMove (AI scoring), computerMove
├── include/
│ └── reversi.h # Shared function prototypes
├── tests/
│ ├── runner.py # Test runner (feeds stdin, compares stdout to expected)
│ ├── inputs/ # game-1 through game-4 input files
│ └── outputs/ # Expected output for each test
├── Makefile
├── .gitignore
└── README.md
The computer uses a heuristic-based AI with three components:
-
Positional weight matrix (8x8 boards): A fixed 8x8 grid assigns scores to each square. Corners are worth 100, edges 10, corner-adjacent squares are penalized (-20 to -30) because they often give the opponent a corner.
-
Game-phase weighting: The AI balances position vs. flips differently by game phase:
- Early game (< 20 pieces): 80% position, 20% flips
- Mid game (20–50 pieces): 50% position, 50% flips
- Late game (50+ pieces): 20% position, 80% flips
-
One-move lookahead (8x8 only, early/mid game): For each candidate move, the AI simulates it on a temporary board. If it exposes a corner to the opponent, the move is penalized by 150 points.
-
Non-8x8 boards: Use a simplified heuristic: corners = 100, edges = 10, corner-adjacent = -30.
- gcc (or any C17-compatible compiler), e.g.
clangorgcc - make
- Python 3 (only for running tests)
git clone <repository-url>
cd reversi-game-algorithm
make
./reversi- Board dimension: Enter the board size (e.g.
4for 4x4,8for standard 8x8). - Computer colour: Choose whether the computer plays Black (B) or White (W).
- Moves: On your turn, enter your move as two lowercase letters: row and column (e.g.
cd= row c, column d). - Invalid move: If you enter an invalid move, the game ends and the computer wins.
Board display:
U= unoccupiedB= blackW= white
Rows and columns are labelled from a to the last letter for the given size.
make testThe test runner:
- Runs the game in non-interactive mode with input from
tests/inputs/game-N. - Compares the program output to
tests/outputs/game-N. - Prints a unified diff on failure and exits with status 1 if any test fails.
Enter the board dimension: 4
Computer plays (B/W) : W
abcd
a UUUU
b UWBU
c UBWU
d UUUU
Enter move for colour B (RowCol): ba
abcd
a UUUU
b BBBU
c UBWU
d UUUU
Computer places W at aa.
abcd
a WUUU
b BWBU
c UBWU
d UUUU
Enter move for colour B (RowCol): ab
...
| Target | Description |
|---|---|
make / make all |
Build the reversi executable |
make clean |
Remove the executable |
make test |
Run all four test cases (game-1 through game-4) |
MIT License
Copyright (c) 2025
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.