-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.h
194 lines (157 loc) · 4.07 KB
/
Map.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef MAP_H
#define MAP_H
#include <string>
#include <vector>
#include <stdint.h>
#include "position.h"
enum Direction
{
NORTH,
SOUTH,
WEST,
EAST,
DIR_MIN = NORTH,
DIR_MAX = EAST
};
const char *dirToString(Direction dir);
enum Player
{
SELF,
ENEMY
};
const char *playerToString(Player p);
extern int width, height; // width and height never change, so why bother passing them around
inline
int index(position p)
{
return p.x * height + p.y;
}
void zobrist_init();
typedef uint64_t HASH_TYPE;
const int TRANSPOSITION_TABLE_SIZE = 1 * 1024 * 1024;
class TranspositionTable
{
public:
struct Entry
{
int heuristic;
};
const Entry *get(HASH_TYPE hash);
void set(HASH_TYPE, const Entry &entry);
private:
std::pair<HASH_TYPE, Entry> data[TRANSPOSITION_TABLE_SIZE];
};
extern TranspositionTable trans_table;
class Map
{
public:
bool isWall(position) const;
bool isWall(Direction dir, Player p) const;
void move(Direction myDir, Player p);
void unmove(Direction myDir, Player p);
int cntMoves(Player p) const;
const std::vector<bool> &getBoard() const;
void print(FILE *) const;
position my_pos() const;
position enemy_pos() const;
HASH_TYPE hash() const;
// Load a board from an open file handle. To read from the console,
// pass stdin, which is actually a (FILE*). file_handle -- an open
// file handle from which to read.
//
// If there is a problem, the function returns NULL. Otherwise, a valid
// Board structure is returned.
//
// The file should be an ascii file. The first line contains the width
// and height of the board, separated by a space. subsequent lines
// contain visual representations of the rows of the board, using '#'
// and space characters. The starting positions of the two players are
// indicated by '1' and '2' characters. There must be exactly one '1'
// character and one '2' character on the board. For example:
// 6 4
// ######
// #1# 2#
// # ##
// ######
bool readFromFile(FILE *file_handle);
private:
// Indicates whether or not each cell in the board is passable.
std::vector<bool> is_wall;
position player_pos[2];
HASH_TYPE current_hash;
};
// Returns whether or not the given cell is a wall or not. TRUE means it's
// a wall, FALSE means it's not a wall, and is passable. Any spaces that are
// not on the board are deemed to be walls.
inline
bool Map::isWall(position pos) const
{
return is_wall[index(pos)];
}
inline
bool Map::isWall(Direction dir, Player p) const
{
position pos;
switch (p) {
case SELF:
pos = player_pos[0];
break;
case ENEMY:
pos = player_pos[1];
break;
default:
return false;
}
switch (dir) {
case NORTH: return is_wall[index(pos.north())];
case SOUTH: return is_wall[index(pos.south())];
case WEST: return is_wall[index(pos.west())];
case EAST: return is_wall[index(pos.east())];
default: return false;
}
}
inline
int Map::cntMoves(Player p) const
{
int cnt = 0;
for (Direction dir = DIR_MIN; dir <= DIR_MAX;
dir = static_cast<Direction>(dir + 1)) {
if (!isWall(dir, p))
++cnt;
}
return cnt;
}
inline
const std::vector<bool> &Map::getBoard() const
{
return is_wall;
}
inline
int cntMovesFromSquare(const std::vector<bool> &board, position pos)
{
int cnt = 0;
if (!board[index(pos.north())])
++cnt;
if (!board[index(pos.south())])
++cnt;
if (!board[index(pos.west())])
++cnt;
if (!board[index(pos.east())])
++cnt;
return cnt;
}
inline
position Map::my_pos() const
{
return player_pos[0];
}
inline
position Map::enemy_pos() const
{
return player_pos[1];
}
inline HASH_TYPE Map::hash() const
{
return current_hash;
}
#endif