Skip to content

raiyan37/reversi-game-algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Reversi (Othello) Game

A terminal-based Reversi game in C where you play against an AI opponent. Supports configurable board sizes from 4x4 up to 26x26.

How It Works

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.

Project Structure

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

AI Strategy

The computer uses a heuristic-based AI with three components:

  1. 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.

  2. 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
  3. 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.

  4. Non-8x8 boards: Use a simplified heuristic: corners = 100, edges = 10, corner-adjacent = -30.

Prerequisites

  • gcc (or any C17-compatible compiler), e.g. clang or gcc
  • make
  • Python 3 (only for running tests)

Getting Started

git clone <repository-url>
cd reversi-game-algorithm
make
./reversi

Gameplay

  1. Board dimension: Enter the board size (e.g. 4 for 4x4, 8 for standard 8x8).
  2. Computer colour: Choose whether the computer plays Black (B) or White (W).
  3. Moves: On your turn, enter your move as two lowercase letters: row and column (e.g. cd = row c, column d).
  4. Invalid move: If you enter an invalid move, the game ends and the computer wins.

Board display:

  • U = unoccupied
  • B = black
  • W = white

Rows and columns are labelled from a to the last letter for the given size.

Running Tests

make test

The test runner:

  1. Runs the game in non-interactive mode with input from tests/inputs/game-N.
  2. Compares the program output to tests/outputs/game-N.
  3. Prints a unified diff on failure and exits with status 1 if any test fails.

Sample Session

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
...

Makefile Targets

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)

License

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.

About

A terminal-based Reversi game in C where you play against an AI opponent. Supports configurable board sizes from 4x4 up to 26x26.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors