-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.h
151 lines (131 loc) · 4.02 KB
/
board.h
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef board_cuda_h
#define board_cuda_h
#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <iostream>
#define GET_INDEX(y_max, x, y) (y_max * x + y)
typedef struct {
uint64_t bitboards[2];
uint32_t parent;
} Board;
__host__ __device__ inline bool is_valid(const Board *board) {
return board->bitboards[0] & 1 != 0;
}
__host__ __device__ void set_valid(Board *board, bool valid) {
if (valid == true) {
board->bitboards[0] = board->bitboards[0] | 1;
} else {
board->bitboards[0] = board->bitboards[0] & 0;
}
}
inline bool boards_equal(const Board *b1, const Board *b2) {
return memcmp(b1, b2, sizeof(Board)) == 0;
}
int compare_boards(const void *v1, const void *v2) {
Board *b1 = (Board*) v1;
Board *b2 = (Board*) v2;
if (!is_valid(b1) && !is_valid(b2)) {
return 0;
} else if (!is_valid(b1) && is_valid(b2)) {
return 1;
} else if (is_valid(b1) && !is_valid(b2)) {
return -1;
} else {
return memcmp(b1, b2, sizeof(Board));
}
}
__host__ __device__ inline bool get_location(Board *board, int y_max, int x, int y) {
//TODO this will probs break for bigger boards
int index = GET_INDEX(y_max, x, y);
int boardIndex;
double offset;
if (index < 63) { // board 1
boardIndex = 0;
offset = pow(2.0, index + 1);
} else { // board 2
boardIndex = 1;
offset = pow(2.0, index - 63);
}
return (board->bitboards[boardIndex] & (uint64_t) offset) != 0;
}
__host__ __device__ inline void set_location(Board *board, int y_max, int x, int y) {
//TODO this will probs break for bigger boards
int index = GET_INDEX(y_max, x, y);
int boardIndex;
uint64_t offset;
if (index < 63) { // board 1
boardIndex = 0;
offset = (uint64_t) pow(2.0, index + 1);
} else { // board 2
boardIndex = 1;
offset = (uint64_t) pow(2.0, index - 63);
}
board->bitboards[boardIndex] = board->bitboards[boardIndex] | offset;
}
__host__ __device__ void print_board(Board *board, int x_max, int y_max) {
printf("size: (%d, %d)\n", x_max, y_max);
printf(" ");
for (int y = 0 ; y < y_max ; y++) {
printf("%d ", y);
}
printf("\n");
for (int x = 0 ; x < x_max ; x++) {
printf("%d ", x);
for (int y = 0 ; y < y_max ; y++) {
if (get_location(board, y_max, x, y) == true) {
printf(" X ");
} else {
printf(" . ");
}
}
printf("\n");
}
}
__device__ inline void copy_left(Board *board, int x_max, int y_max) {
int middle = y_max / 2;
int leftCount = 0;
int rightCount = 0;
if (y_max % 2 == 0) {
for (int x = 0 ; x < x_max ; x++) {
for (int y = 0 ; y < middle ; y++) {
if (get_location(board, y_max, x, y) == true) {
leftCount++;
}
}
for (int y = middle ; y < y_max ; y++) {
if (get_location(board, y_max, x, y) == true) {
rightCount++;
}
}
}
} else {
for (int x = 0 ; x < x_max ; x++) {
for (int y = 0 ; y < middle ; y++) {
if (get_location(board, y_max, x, y) == true) {
leftCount++;
}
}
for (int y = middle + 1 ; y < y_max ; y++) {
if (get_location(board, y_max, x, y) == true) {
rightCount++;
}
}
}
}
if (leftCount < rightCount) {
Board *tmpBoard = new Board;
memcpy(tmpBoard, board, sizeof(Board));
memset(board, 0, sizeof(Board));
set_valid(board, true);
for (int x = 0 ; x < x_max ; x++) {
for (int y = 0 ; y < y_max ; y++) {
if (get_location(tmpBoard, y_max, x, y) == true) {
set_location(board, y_max, x, y_max - y - 1);
}
}
}
delete tmpBoard;
}
}
#endif