-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardGenerator.java
More file actions
207 lines (171 loc) · 7.45 KB
/
BoardGenerator.java
File metadata and controls
207 lines (171 loc) · 7.45 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
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
//Nick Soetaert
import java.util.*;
public class BoardGenerator{
//For debug stuff
public static long getBoardKey(){
/*String boardString[][] = {
{"x"," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "}
};*/
String boardString[][] = {
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{"x","x","x","x","x","x","x","x"},
{" "," "," "," "," "," "," "," "}
};
long result = 0;
String binaryStr;
for(int i = 0; i < 64; i++){
binaryStr = "0000000000000000000000000000000000000000000000000000000000000000";
binaryStr = binaryStr.substring(i+1) + "1" + binaryStr.substring(0,i);
switch(boardString[i/8][i%8]){
case "x": result += stringToBinary(binaryStr);
break;
}
}
System.out.println(result);
return result;
}
public static Board initStandardBoard(){
long WP=0L, WN=0L, WB=0L, WR=0L, WQ=0L, WK=0L, BP=0L, BN=0L, BB=0L, BR=0L, BQ=0L, BK=0L;
//Black pieces are lowercase, a1 square is bottom left corner.
String boardString[][] =
{
{"r","n","b","q","k","b","n","r"},
{"z","z","z","z","z","z","z","z"},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{"P","P","P","P","P","P","P","P"},
{"R","N","B","Q","K","B","N","R"}
};
/*{
{" "," ","R"," ","K","k"," "," "},
{" "," "," "," "," "," ","n"," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," ","N"," "," "," ","r"},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "}
};
*/
String binaryStr;
//For every square on the chess board
for(int i = 0; i < 64; i++){
binaryStr = "0000000000000000000000000000000000000000000000000000000000000000";
binaryStr = binaryStr.substring(i+1) + "1" + binaryStr.substring(0,i);
//cycles through board, top left to bottom right
switch(boardString[i/8][i%8]){
case "P": WP += stringToBinary(binaryStr);
break;
case "N": WN += stringToBinary(binaryStr);
break;
case "B": WB += stringToBinary(binaryStr);
break;
case "R": WR += stringToBinary(binaryStr);
break;
case "Q": WQ += stringToBinary(binaryStr);
break;
case "K": WK += stringToBinary(binaryStr);
break;
case "z": BP += stringToBinary(binaryStr);
break;
case "n": BN += stringToBinary(binaryStr);
break;
case "b": BB += stringToBinary(binaryStr);
break;
case "r": BR += stringToBinary(binaryStr);
break;
case "q": BQ += stringToBinary(binaryStr);
break;
case "k": BK += stringToBinary(binaryStr);
break;
default:
break;
}
}
Board curBoard = new Board(WP, WN, WB, WR, WQ, WK, BP, BN, BB, BR, BQ, BK);
//drawBoard(curBoard);
return curBoard;
}
public static void drawBoard(Board curBoard){
String chessBoard[][] = new String[8][8];
for(int i = 0; i < 64; i++){
chessBoard[i/8][i%8] = " ";
}
for(int i = 0; i < 64; i++){
/*
* Shift bits i bits to the right
* After shift, if leading bit is a 1, there is a piece on the current square
*/
if(((curBoard.get(BitBoardEnum.WP)>>i) & 1) == 1) {chessBoard[i/8][i%8] = " P ";}
if(((curBoard.get(BitBoardEnum.WN)>>i) & 1) == 1) {chessBoard[i/8][i%8] = " N ";}
if(((curBoard.get(BitBoardEnum.WB)>>i) & 1) == 1) {chessBoard[i/8][i%8] = " B ";}
if(((curBoard.get(BitBoardEnum.WR)>>i) & 1) == 1) {chessBoard[i/8][i%8] = " R ";}
if(((curBoard.get(BitBoardEnum.WQ)>>i) & 1) == 1) {chessBoard[i/8][i%8] = " Q ";}
if(((curBoard.get(BitBoardEnum.WK)>>i) & 1) == 1) {chessBoard[i/8][i%8] = " K ";}
if(((curBoard.get(BitBoardEnum.BP)>>i) & 1) == 1) {chessBoard[i/8][i%8] = " z ";}
if(((curBoard.get(BitBoardEnum.BN)>>i) & 1) == 1) {chessBoard[i/8][i%8] = " n ";}
if(((curBoard.get(BitBoardEnum.BB)>>i) & 1) == 1) {chessBoard[i/8][i%8] = " b ";}
if(((curBoard.get(BitBoardEnum.BR)>>i) & 1) == 1) {chessBoard[i/8][i%8] = " r ";}
if(((curBoard.get(BitBoardEnum.BQ)>>i) & 1) == 1) {chessBoard[i/8][i%8] = " q ";}
if(((curBoard.get(BitBoardEnum.BK)>>i) & 1) == 1) {chessBoard[i/8][i%8] = " k ";}
}
int j = 0;
for(String[] arr : chessBoard){
System.out.println(" ________________________________________");
System.out.print(8-j + " |");
j++;
for(String c : arr){
System.out.print(c + " |");
}
System.out.println("");
}
System.out.println(" ========================================");
System.out.println(" A | B | C | D | E | F | G | H |\n");
}
//Takes a 64 character string of 1s and 0s and converts that into a 64 bit binary representation
private static long stringToBinary(String binaryStr){
/*
1000000000000000000000000000000000000000000000000000000000000000
is too large for parseLong.
The else statement fixes this, converting it to -9223372036854775808,
which is 1000000... in signed 2's compliment binary.
*/
if(binaryStr.charAt(0) == '0'){
return Long.parseLong(binaryStr, 2);
}
else {
return Long.parseLong("1"+binaryStr.substring(2), 2) * 2;
}
}
public static void drawPiece(long bb){
String chessBoard[][] = new String[8][8];
for(int i = 0; i < 64; i++){
chessBoard[i/8][i%8] = " ";
}
for(int i = 0; i < 64; i++){
/*
* Shift bits i bits to the right
* After shift, if leading bit is a 1, there is a piece on the current square
*/
if(((bb>>i) & 1) == 1) {chessBoard[i/8][i%8] = "x";}
}
for(int i = 0; i < 8; i++){
System.out.println(8-i + " " + Arrays.toString(chessBoard[i]));
}
System.out.println(" A B C D E F G H\n");
}
}