-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
63 lines (54 loc) · 1.61 KB
/
Copy pathboard.cpp
File metadata and controls
63 lines (54 loc) · 1.61 KB
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
#include "board.h"
#include <fstream>
#include <vector>
// We cash the neighbors of a tile to save time while scoring a board
// These represent the 8 neighbors of a tile or grid position
// up, down, left, right and the four diagonals
int row_inc[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int col_inc[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
// neighbor cache
int **neighbors;
// for any tile get it's neighbors
// start with the 8 grid neighbors and then filter out any tiles
// that are out of the grid
int get_neighbor(int tile, int neighbor) {
int row = (tile / GRID_SIZE) + row_inc[neighbor];
int col = (tile % GRID_SIZE) + col_inc[neighbor];
if (row < 0 || row >= GRID_SIZE || col < 0 || col >= GRID_SIZE) return -1;
return (row * GRID_SIZE) + col;
}
// build the neighbor cache
void build_neighbors() {
neighbors = new int *[BOARD_SIZE];
for (int i = 0; i < BOARD_SIZE; i++) {
neighbors[i] = new int[9];
std::fill_n(neighbors[i], 9, -1);
int num_neighbors = 0;
for (int j = 0; j < 8; j++) {
int neighbor = get_neighbor(i, j);
if (neighbor != -1) {
neighbors[i][num_neighbors] = get_neighbor(i, j);
num_neighbors++;
}
}
}
}
void print_neighbors() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < 9; j++) cout << neighbors[i][j] << ' ';
cout << endl;
}
}
vector<Board> input_boards;
bool read_boards(char *board_file) {
string board;
fstream board_stream(board_file);
if (!board_stream) {
cerr << "no dictionary file" << endl;
return false;
}
while (getline(board_stream, board)) {
input_boards.push_back(board);
}
return true;
}