blob: 8eeb7584a439eb8afb28a4599ead8f9e4c51d583 (
plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
#include "zobrist.h"
// ===================================================================
// Globals
// ===================================================================
static uint64_t *zobrist[15];
// ===================================================================
// Helpers
// ===================================================================
// ===================================================================
// Exported method implementations
// ===================================================================
int
zobrist_init(void) {
for (int k=0; k<15; k++) {
if (zobrist[k] != NULL) return EXIT_FAILURE;
}
for (int j=0; j<15; j++) {
zobrist[j] = malloc(sizeof(uint64_t)*board_size*board_size*(2*3+1));
for (int k=0; k<board_size*board_size*(2*3+1); k++) {
XORSHIFT64;
zobrist[j][k] = RANDOM64;
}
}
return EXIT_SUCCESS;
}
void
zobrist_free(void) {
for (int k=0; k<15; k++) {
if (zobrist[k] != NULL) {
free(zobrist[k]);
zobrist[k] = NULL;
}
}
}
uint64_t
zobrist_compute(void) {
uint64_t hash = 0;
for (uint8_t l=0; l<board_size*board_size; l++) {
colour_stack_t c = colours[l];
const uint8_t count = COUNT_AT(l);
enum STONE_VARIANT s = STONE_AT(l);
for (uint8_t h=0; h<count; h++, c >>= 1)
hash ^= zobrist[h][l*(2*3+1)+(c&1)*3+s];
}
return hash;
}
uint64_t
zobrist_apply(const action_t action, uint64_t hash) {
const enum A_TYPE type = GET_TYPE(action);
const int8_t loc = GET_LOC(action);
if (type == A_PLACE) {
hash ^= zobrist[0][loc*(2*3+1)
+current_colour*3
+GET_DATA0(action)];
} else {
const uint8_t gaps = GET_DATA0(action) & 0x7F,
crush = GET_DATA0(action) & 0x80,
num = GET_DATA1(action) & 0x0F,
dir = GET_DATA1(action) >> 4;
const int8_t delta = move_deltas[dir];
// TODO: adapt this
int8_t steps = 1;
uint8_t gap_bit = 1, total = 1;
for (int8_t d = 1; d < num; d++, total++, gap_bit <<= 1) {
if (gaps & gap_bit) {
colours[loc] <<= total;
colours[loc] |= colours[loc+steps*delta] & ((1 << total) - 1);
colours[loc+steps*delta] >>= total;
celldat[loc] += total*NUM_INC;
celldat[loc+steps*delta] -= total*NUM_INC;
total = 0;
steps++;
}
}
colours[loc] <<= total;
colours[loc] |= colours[loc+steps*delta] & ((1 << total) - 1);
colours[loc+steps*delta] >>= total;
celldat[loc] += total*NUM_INC;
// celldat[loc] &= CLR_STONE; is not necessary, as STONE_FLAT == 0
celldat[loc] |= STONE_AT(loc+steps*delta);
celldat[loc+steps*delta] -= total*NUM_INC;
celldat[loc+steps*delta] &= CLR_STONE;
if (crush) {
celldat[loc+steps*delta] |= STONE_STANDING;
} else {
celldat[loc+steps*delta] |= STONE_FLAT; // should be optimised out
}
}
return hash;
}
|