-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitboard.cpp
217 lines (191 loc) · 4.52 KB
/
bitboard.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//#include "stdafx.h"
#include "bitboard.h"
void board::init(){
m_board = 0;
insertNewPiece();
insertNewPiece();
}
void board::init(board_t pos){
m_board = pos;
}
int board::getNewPiece(){
int r = configure::uRNG.rand()%10;
if(r == 0) return 2;
else return 1;
};
int board::getEmptyPos(){
int empty = 0;
for(int i =0; i < 64; i+=4){
if(((m_board >> i) & 0xf ) == 0) empty ++;
}
int iStart = configure::uRNG.rand()%empty;
cout << iStart << endl;
bool existEmpty = false;
for(int i = 0; i < 16; i++){
board_t b = m_board;
if(((m_board>>(4*(15-i)))&0xf) == 0){
if(iStart == 0){
return i;
}
iStart --;
existEmpty = true;
}else if(i == 15 && existEmpty == false){
return -1;
}
}
return -1;
}
void board::insertNewPiece(){
int pos = getEmptyPos();
if(pos == -1){
cout << "Wrong with getting new empty position" << endl;
while(1);
}else{
board_t num = getNewPiece();
num = num << 4*(15-pos);
board_t mask = 0x000f;
mask = mask << 4*(15-pos);
//cout << pos << endl;
//cout << hex << num << endl;
//cout << hex << mask << endl;
mask = ~mask;
//cout << hex << mask << endl;
m_board = (m_board&mask) | num;
}
};
bool board::isEnd(){
for(int i =0; i < 4; i++){
row_t r = operation::getRow(m_board, i);
row_t c = operation::getCol(m_board, i);
for(int j =0; j < 3; j++){
if(((r>>4*j)&0xf) == ((r>>4*(j+1))&0xf) || ((r>>4*j)&0xf) == 0){
return false;
}
if(((c>>4*j)&0xf) == ((c>>4*(j+1))&0xf) || ((c>>4*j)&0xf) == 0){
return false;
}
}
if(((r>>12)&0xf) == 0) return false;
if(((c>>12)&0xf) == 0) return false;
}
return true;
}
bool board::isFull(){
for(int i = 0; i < 16; i ++){
if(((m_board >> 4*i) & 0xf) == 0) return false;
}
return true;
};
// 0 : up
// 1 : right
// 2 : down
// 3 : left
int board::canMove(int direct){
row_t row[4];
int score = 0;
bool notMove = true;
switch(direct){
case 0:
for(int i =0; i < 4; i++){
row[i] = operation::reverseRow(operation::getCol(m_board,i));
if(configure::rightInfo[row[i]].score == -1);
else{
row_t result = configure::rightInfo[row[i]].r;
score += configure::rightInfo[row[i]].score;
row[i] = result;
notMove = false;
}
//score += moveRight(row[i]);
row[i] = operation::reverseRow(row[i]);
}
if(notMove) return -1;
else{
m_board = operation::setCols(row);
return score;
}
break;
case 1:
for(int i =0; i < 4; i++){
row[i] = operation::getRow(m_board,i);
if(configure::rightInfo[row[i]].score == -1);
else{
row_t result = configure::rightInfo[row[i]].r;
score += configure::rightInfo[row[i]].score;
row[i] = result;
notMove = false;
}
//score += moveRight(row[i]);
}
if(notMove) return -1;
else{
m_board = operation::setRows(row);
return score;
}
break;
case 2:
for(int i =0; i < 4; i++){
row[i] = operation::getCol(m_board,i);
if(configure::rightInfo[row[i]].score == -1);
else{
row_t result = configure::rightInfo[row[i]].r;
score += configure::rightInfo[row[i]].score;
row[i] = result;
notMove = false;
}
//score += moveRight(row[i]);
}
if(notMove) return -1;
else{
m_board = operation::setCols(row);
return score;
}
break;
case 3:
for(int i =0; i < 4; i++){
row[i] = operation::reverseRow(operation::getRow(m_board,i));
if(configure::rightInfo[row[i]].score == -1);
else{
row_t result = configure::rightInfo[row[i]].r;
score += configure::rightInfo[row[i]].score;
row[i] = result;
notMove = false;
}
row[i] = operation::reverseRow(row[i]);
}
if(notMove) return -1;
else{
m_board = operation::setRows(row);
return score;
}
break;
default:
cout << "Wrong direction!" << endl;
break;
}
return score;
};
void board::showBoard(){
for(int i = 0; i < 4; i++){
row_t r = operation::getRow(m_board,i);
for(int j = 3; j >= 0; j--){
cout << configure::tile_score[((r>>4*j)&0xf)] << "\t";
}
cout << endl;
}
cout << endl;
}
void board::showBoard(int positionOf1st32k,int positionOf2nd32k,int positionOf64k){
for(int i = 0; i < 4; i++){
row_t r = operation::getRow(m_board,i);
for(int j = 3; j >= 0; j--){
if((i*4+3-j) == positionOf1st32k || (i*4+3-j) == positionOf2nd32k) cout << 32768 << "\t";
else if((i*4+3-j) == positionOf64k) cout << 65536 << "\t";
else cout << configure::tile_score[((r>>4*j)&0xf)] << "\t";
}
cout << endl;
}
cout << endl;
}
board_t board::getCurrentPosition(){
return m_board;
}