-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
85 lines (73 loc) · 2.09 KB
/
game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#pragma ide diagnostic ignored "cert-msc50-cpp"
#pragma ide diagnostic ignored "hicpp-signed-bitwise"
#pragma clang diagnostic ignored "-Wshift-op-parentheses"
// includes
#include "game.h"
#include <stdio.h>
#ifdef production
#include <emscripten/emscripten.h>
#endif
int NUM_COLUMNS = 7;
int NUM_ROWS = 6;
uint64_t bitboard[2];
int heights[7] = { 0, 7, 14, 21, 28, 35, 42 };
uint8_t moves = 0b01111111;
int counter = 0;
const char red_circle[5] = { (uint8_t) 0xF0, (uint8_t) 0x9F, (uint8_t) 0x94, (uint8_t) 0xB4, '\0' };
const char blue_circle[5] = { (uint8_t) 0xF0, (uint8_t) 0x9F, (uint8_t) 0x94, (uint8_t) 0xB5, '\0' };
void printBoard() {
for (int i = NUM_ROWS - 1; i >= 0; i--) {
for (int j = 0; j < NUM_COLUMNS; j++) {
if (GET_BITBOARD_BIT(bitboard[0], i, j)) {
printf("| %s ", red_circle);
} else if (GET_BITBOARD_BIT(bitboard[1], i, j)) {
printf("| %s ", blue_circle);
} else {
printf("| ");
}
}
printf("|%*s\n", i + 1, " ");
}
printf("------------------------------------\n");
for (int i = 0; i < NUM_COLUMNS; i++) {
printf("| %d ", i);
}
printf("|\n");
}
bool isWin(uint64_t player_bitboard) {
uint32_t directions[] = { 1, 7, 6, 8 };
uint64_t bb;
foreach(direction, directions, uint32_t) {
bb = player_bitboard & (player_bitboard >> *direction);
if ((bb & (bb >> (2 * *direction))) != 0) {
return true;
}
}
return false;
}
bool isDraw() {
return moves == 0;
}
uint8_t getMoves() {
for (int col = 0; col < NUM_COLUMNS; col++) {
if (TOP_ROW & BIT_MASK_64(heights[col])) {
CLEAR_BIT_8(moves, col);
} else {
SET_BIT_8(moves, col);
}
}
return moves;
}
void makeMove(unsigned int col) {
TOGGLE_BIT_64(CURRENT_BITBOARD, heights[col]++);
getMoves();
counter++;
}
void unmakeMove(unsigned int col) {
TOGGLE_BIT_64(PREVIOUS_BITBOARD, --heights[col]);
getMoves();
counter--;
}
//#ifdef production
//EMSCRIPTEN_KEEPALIVE
//#endif