About
reversi-core is a simple reversi library for C.
It's not the fastest library but fast enough for the monte carlo search.
It's not feature rich but sufficient for demo apps of libraries.
Requirements
- A 64-bit processor
- Meson for building
Documentation
Most of the functions are here.
reversi-core: RevBoard Struct Reference
Rest of them are here.
reversi-core: include/reversi.h File Reference
Building
Build Whole Project
meson setup build
meson compile -C build
meson test -C build
Build Library Only
meson setup build -Dexamples=false -Dtests=false
meson compile -C build
Build as Subproject
You don't need to clone the git repo if you build your project with meson.
Save the following text as subprojects/reversi.wrap
.
[wrap-git]
url = https://github.com/matyalatte/reversi-core.git
revision = head
depth=1
[provide]
reversi = reversi_dep
Then, you can use reversi-core in your meson project.
reversi_dep = dependency('reversi', fallback : ['reversi', 'reversi_dep'])
executable('your_exe_name', ['your_code.cpp'], dependencies : [reversi_dep])
meson setup build -Dreversi:examples=false -Dreversi:tests=false
meson compile -C build
Examples
Initialization
int main() {
if (board == NULL) {
printf("Failed to create a reversi board.\n");
return 1;
}
revFreeBoard(board);
}
void revInitGenRandom(uint32_t seed)
Initialize a random number generator.
Class for a reversi board.
Bitboard to Array
You can get xy coordinates from bitboards.
int *black_disks = revGetBitboardAsArray(board,
DISK_BLACK);
for (
int i = 0; i < revCountDisks(board,
DISK_BLACK); i++) {
int pos = black_disks[i];
int x = pos % 8;
int y = pos / 8;
printf("disk%d: %d, %d", i, x, y);
}
free(black_disks);
int *mobility = revGetMobilityAsArray(board);
for (int i = 0; i < revGetMobilityCount(board); i++) {
int pos = mobility[i];
int x = pos % 8;
int y = pos / 8;
printf("move%d: %d, %d", i, x, y);
}
free(mobility);
@ DISK_BLACK
Black disk.
Definition: reversi.h:118
Get a Disk
You can get a disk from a board.
} else {
}
RevDiskType
Color of disk.
Definition: reversi.h:117
@ DISK_WHITE
White disk.
Definition: reversi.h:119
Check a Move
You can check if a move is legal or not.
if (revIsLegalMoveXY(board, 4, 2)) {
} else {
}
Execute a Move
int pos = flipped_array[i];
int x = pos % 8;
int y = pos / 8;
printf("flipped%d: %d, %d", i, x, y);
}
free(flipped_array);
int revCountOnes(RevBitboard b)
Counts number of positive bits in a bitboard.
uint64_t RevBitboard
Type for a bitboard.
Definition: reversi.h:46
int * revBitboardToArray(RevBitboard b)
Converts a bitboard to an array of positions.
Generate a Move
You can use revGenMove*()
to generate a move.
int move = revGenMoveRandom(board);
printf("move: %d, %d\n", move % 8, move / 8);
revMove(board, move);
move = revGenMoveMonteCarlo(board, 20000);
printf("move: %d, %d\n", move % 8, move / 8);
revMove(board, move);
CLI App
Command-line app to play reversi.
#include <stdio.h>
#include <time.h>
int inputInt(const char* msg) {
int x = -1;
while ((x < 0) | (x >= 8)) {
printf("%s", msg);
scanf("%d", &x);
fflush(stdin);
}
return x;
}
int main() {
if (board == NULL) {
printf("Failed to create a reversi board.\n");
return 1;
}
while (revHasLegalMoves(board)) {
int move;
printf("Your turn.\n");
revPrintBoardWithMobility(board);
int x = -1;
int y = -1;
while ((x == -1) | !revIsLegalMoveXY(board, x, y)) {
x = inputInt("x? ");
y = inputInt("y? ");
}
} else {
printf("CPU's turn.\n");
revPrintBoardWithMobility(board);
move = revGenMoveMonteCarlo(board, 20000);
}
printf("move: %d, %d\n\n", move % 8, move / 8);
revMove(board, move);
if (!revHasLegalMoves(board)) {
revChangePlayer(board);
}
}
printf("You win!\n");
printf("You lose.\n");
} else {
printf("Draw.\n");
}
revFreeBoard(board);
return 0;
}
int revXYToPos(int x, int y)
Returns x + y * 8.
GUI App
There is a GUI demo for reversi-core.
matyalatte/libui-reversi-demo: Demo app to play reversi using libui-ng and reversi-core