Repository navigation
Expand file tree
/
Copy pathGameBoard.java
More file actions
294 lines (273 loc) · 8.66 KB
/
Copy pathGameBoard.java
File metadata and controls
294 lines (273 loc) · 8.66 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import java.util.*;
public class GameBoard {
Scanner kb = new Scanner(System.in);
Checkers[][] dgb= new Checkers[8][8]; //digital gameboard
int toX;
public GameBoard(){
initBoard();
}
public Checkers[][] getBoard(){
return dgb;
}
public void movePiece(String color, boolean isKing){
boolean noMove = true;
while(noMove){
boolean firstCheck = false;
boolean secondCheck = false;
boolean thirdCheck = false;
boolean fourthCheck = false;
//check that these inputs are valid and doesn't make it go off the board
boolean isNull = true;
char fromXChar = 'a';
int fromX = 0;
int fromY = 0;
char toXChar = 'a';
int toX = 0;
int toY = 0;
while(isNull){
System.out.println("Which piece do you want to move? Enter a letter (A-H) and number (0-7)");
String from = kb.nextLine();
fromXChar = from.charAt(0);
fromX = toInt(fromXChar);
fromY = Integer.parseInt(from.substring(1));
System.out.println("Where would you like to move this piece? Enter a letter (A-H) and number (0-7)");
String to = kb.nextLine();
toXChar = to.charAt(0);
toX = toInt(toXChar);
toY = Integer.parseInt(to.substring(1));
if(!(dgb[fromX][fromY] == null)){
isNull = false;
}
else{
System.out.println("Please put a valid option");
}
}
//check if color of piece matches player
if((dgb[fromX][fromY].getColor()).equals(color)){
//System.out.println("Check 1");
firstCheck = true;
}
//checks if the space is open
if(dgb[toX][toY] == null){
//System.out.println("Check 2");
secondCheck = true;
}
if(color.equals("Red")){
//check that the piece is moving forward
if(toY>fromY){
//System.out.println("Check 3");
thirdCheck = true;
}
//check for diagonal movement
if((toX == fromX+1 && toY == fromY+1)||(toX == fromX-1 && toY == fromY+1)){
//System.out.println("Check 4");
fourthCheck = true;
}
}
else if(color.equals("Blue")){
//check that the piece is moving forward
if(toY<fromY){
//System.out.println("Check 3");
thirdCheck = true;
}
//check for diagonal movement
if((toX == fromX+1 && toY == fromY-1)||(toX == fromX-1 && toY == fromY-1)){
//System.out.println("Check 4");
fourthCheck = true;
}
}
//Kings can move backwards
if(dgb[fromX][fromY].isKing()){
if((toX == fromX+1 && toY == fromY+1)||(toX == fromX-1 && toY == fromY+1) ||(toX == fromX+1 && toY == fromY-1)||(toX == fromX-1 && toY == fromY-1)){
//System.out.println("Check 3");
//System.out.println("Check 4");
System.out.println("King Check");
thirdCheck = true;
fourthCheck = true;
}
}
//make sure all checks are true before moving the piece
if(firstCheck && secondCheck && thirdCheck){
//check for jump
if(checkJump(fromX, fromY) == 0){
forceJumpRight(fromX, fromY);
noMove = false;
}else if(checkJump(fromX, fromY) == 1){
forceJumpLeft(fromX, fromY);
noMove = false;
}else{
if(fourthCheck){
dgb[toX][toY] = dgb[fromX][fromY];
dgb[fromX][fromY] = null;
noMove = false;
}
}
}
}
}
public int checkJump(int x, int y){
//check for jump
if(x <= 5 && y <= 5){
if(dgb[x+2][y+2]==null && dgb[x+1][y+1]!= null && !(dgb[x+1][y+1].getColor().equalsIgnoreCase(dgb[x][y].getColor()))){ //check that the space one right and one up is a piece of the opposite color
return 0;
}
}
if(x >= 2 && y <= 5){
if(dgb[x-2][y+2]==null && dgb[x-1][y+1]!= null && !(dgb[x-1][y+1].getColor().equalsIgnoreCase(dgb[x][y].getColor()))){
return 1;
}
}
return 2;
}
//if piece is a king, it can jump in any direction 3/23
public void forceJumpRight(int x, int y){
dgb[x+2][y+2] = dgb[x][y];
dgb[x+1][y+1] = null;
//award point to the color that captured the piece in the jump
dgb[x][y] = null;
//check for double jump
if(checkJump(x+2, y+2)==0){
doubleJumpRight(x+2, y+2);
}else if(checkJump(x+2, y+2)==1){
doubleJumpLeft(x+2, y+2);
}
}
public void forceJumpLeft(int x, int y){
dgb[x-2][y+2] = dgb[x][y];
dgb[x-1][y+1] = null;
//award point to the color that captured the piece in the jump
dgb[x][y] = null;
//check for double jump
if(checkJump(x-2, y+2)==0){
doubleJumpLeft(x-2, y+2);
}else if(checkJump(x-2, y+2)==1){
doubleJumpLeft(x-2, y+2);
}
}
public void doubleJumpRight(int x, int y){
//double jump to the right
dgb[x+2][y+2] = dgb[x][y];
//System.out.print("GO RIGHT");
dgb[x+1][y+1] = null;
}
public void doubleJumpLeft(int x, int y){
dgb[x-2][y+2] = dgb[x][y];
//System.out.print("GO LEFT");
dgb[x][y] = null;
}
public void initBoard(){
dgb[0][0] = new Checkers(0,0,"Red");
dgb[2][0] = new Checkers(2,0,"Red");
dgb[4][0] = new Checkers(4,0,"Red");
dgb[6][0] = new Checkers(6,0,"Red");
dgb[1][1] = new Checkers(1,1,"Red");
dgb[3][1] = new Checkers(3,1,"Red");
dgb[5][1] = new Checkers(5,1,"Red");
dgb[7][1] = new Checkers(7,1,"Red");
dgb[0][2] = new Checkers(0,2,"Red");
dgb[2][2] = new Checkers(2,2,"Red");
dgb[4][2] = new Checkers(4,2,"Red");
dgb[6][2] = new Checkers(6,2,"Red");
dgb[1][5] = new Checkers(1,5,"Blue");
dgb[3][5] = new Checkers(3,5,"Blue");
dgb[5][5] = new Checkers(5,5,"Blue");
dgb[7][5] = new Checkers(7,5,"Blue");
dgb[0][6] = new Checkers(0,6,"Blue");
dgb[2][6] = new Checkers(2,6,"Blue");
dgb[4][6] = new Checkers(4,6,"Blue");
dgb[6][6] = new Checkers(6,6,"Blue");
dgb[1][7] = new Checkers(1,7,"Blue");
dgb[3][7] = new Checkers(3,7,"Blue");
dgb[5][7] = new Checkers(5,7,"Blue");
dgb[7][7] = new Checkers(7,7,"Blue");
}
public int toInt(char c){
int toX = 0;
switch (c){
case 'A':
toX = 0;
break;
case 'B':
toX = 1;
break;
case 'C':
toX = 2;
break;
case 'D':
toX = 3;
break;
case 'E':
toX = 4;
break;
case 'F':
toX = 5;
break;
case 'G':
toX = 6;
break;
case 'H':
toX = 7;
break;
default:
System.out.println(c + " is invalid");
}
return toX;
}
public String checkWin() {
int numRed = 0;
int numBlue = 0;
for (Checkers[] j : dgb) {
for (Checkers i : j) {
if(i != null) {
if (i.getColor().toLowerCase().equals("blue")) {
numBlue += 1;
}
if (i.getColor().toLowerCase().equals("red")) {
numRed += 1;
}
}
}
}
if (numRed == 0) {
return "blue";
}
if (numBlue == 0) {
return "red";
}
return "null";
}
public void checkKing() {
if (dgb[0][0].getColor().toLowerCase().equals("blue")) {
dgb[0][0] = null;
dgb[0][0] = new KingChecker(0, 0, "blue");
}
if (dgb[0][2].getColor().toLowerCase().equals("blue")) {
dgb[0][2] = null;
dgb[0][2] = new KingChecker(0, 2, "blue");
}
if (dgb[0][4].getColor().toLowerCase().equals("blue")) {
dgb[0][4] = null;
dgb[0][4] = new KingChecker(0, 0, "blue");
}
if (dgb[0][6].getColor().toLowerCase().equals("blue")) {
dgb[0][6] = null;
dgb[0][6] = new KingChecker(0, 0, "blue");
}
if (dgb[1][7].getColor().toLowerCase().equals("red")) {
dgb[1][7] = null;
dgb[1][7] = new KingChecker(0, 0, "red");
}
if (dgb[3][7].getColor().toLowerCase().equals("red")) {
dgb[3][7] = null;
dgb[3][7] = new KingChecker(0, 0, "red");
}
if (dgb[5][7].getColor().toLowerCase().equals("red")) {
dgb[5][7] = null;
dgb[5][7] = new KingChecker(0, 0, "red");
}
if (dgb[7][7].getColor().toLowerCase().equals("red")) {
dgb[7][7] = null;
dgb[7][7] = new KingChecker(0, 0, "red");
}
}
}