This project implements the Reversi game (also known as Othello). It also includes a game-playing agent using the minimax algorithm. Users are able to play against either another human or a CPU player (i.e., the game-playing agent).
This game is hosted on my personal website. Click it open and play! 😊
For each of the Black (first) and White (second) players, users can pick whether it will be a human or a CPU player. All the four combinations are possible: human versus human, human versus CPU, CPU versus human, and CPU versus CPU (although the last combination is not very useful). The entire game history is recorded, so moves made by human players may be undone for an unlimited number of times, while moves made by CPUs cannot be undone. Moreover, users may stop in the middle of a game and start a new game.
CPU players are driven by the minimax algorithm with alpha-beta pruning. By changing the search depth and comprehensiveness of the heuristic function, the game-playing agent has three difficulty levels: easy, normal, and hard. The main code of this agent is written in Rust and then compiled into WebAssembly, resulting in faster response and smaller memory footprints compared to a JavaScript implementation.
This project is made up of two parts:
-
A Cargo project containing a Rust implementation of the game-playing agent. It lies in the
reversi-agentfolder.reversi-agent/src/lib.rs: This file exports theplay()function for which a JavaScript binding is generated.reversi-agent/src/utils.rs: Utility functions like counting scores and computing disc flips as a result of a move.
To compile this Cargo project, execute
wasm-pack buildunder thereversi-agentdirectory. Output files will be under two directories,pkgandtarget, where thepkgdirectory contains the JavaScript/TypeScript binding that will be used by the Node project. -
A Node project which implements the user interface and the human player capability. The majority of its code lies in
src.src/favicon.ico: Webpage favicon, which is a screenshot of the user interface.src/index.html: The main user interface.src/main.ts: Logic of the user interface and the human player.src/style.css: Stylesheet of the chessboard, discs, and controls.
To compile this Node project, execute
npm run buildunder the project's root directory. Output files will be under thedistdirectory, which constitute a webpage that is ready to be hosted by a server. Note that simply clicking-open thedist/index.htmlfile will not work because of the browser's CORS restriction on WebAssembly files. An easy way to override this restriction is to host the webpage files on a local server by runningnpm run serveunder the project's root directory. Users will then be able to visit the webpage at a localhost address.
Heuristic functions used by the minimax algorithm are based on the paper An Analysis of Heuristics in Othello.
The following GIF is a screen recording of a CPU-versus-CPU game. As you can see, the response time of CPU players is barely noticeable.
