-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKingChecker.java
123 lines (109 loc) · 4.55 KB
/
KingChecker.java
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
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
@SuppressWarnings("serial")
public class KingChecker extends Checker {
public KingChecker(int row, int col, boolean player) {
super(row, col, player);
}
@Override
public boolean isKing() {
return true;
}
/**
* the king checker can jump along diagonals forwards AND backwards
* this is why dynamic dispatch is necessary
* this is an ESSENTIAL METHOD
* I use this in the game board class on all the squares on the board in jumpExits
*/
@Override
public ArrayList<Square> jumpPossibilities(Square[][] board, boolean currPlayer) {
ArrayList<Square> arr = new ArrayList<Square>();
if (getPlayer() == currPlayer) {
Square jumpSq1 = null;
Square jumpSq2 = null;
Square jumpSq3 = null;
Square jumpSq4 = null;
if (getRow() >= 0 && getRow() < 6 && getCol() >= 0 && getCol() < 6) {
Square sq1 = board[(getRow() + 1)] [(getCol() + 1)];
if (sq1 instanceof Checker) {
if (((Checker) sq1).getPlayer() == !getPlayer()) {
jumpSq1 = board[(getRow() + 2)] [(getCol() + 2)];
}
}
}
if (getRow() >= 0 && getRow() < 6 && getCol() > 1 && getCol() < 8) {
Square sq2 = board[(getRow() + 1)] [(getCol() - 1)];
if (sq2 instanceof Checker) {
if (((Checker) sq2).getPlayer() == !getPlayer()) {
jumpSq2 = board[(getRow() + 2)] [(getCol() - 2)];
}
}
}
if (getRow() > 1 && getRow() < 8 && getCol() >= 0 && getCol() < 6) {
Square sq3 = board[(getRow() - 1)] [(getCol() + 1)];
if (sq3 instanceof Checker) {
if (((Checker) sq3).getPlayer() == !getPlayer()) {
jumpSq3 = board[(getRow() - 2)] [(getCol() + 2)];
}
}
}
if (getRow() > 1 && getRow() < 8 && getCol() > 1 && getCol() < 8) {
Square sq4 = board[(getRow() - 1)] [(getCol() - 1)];
if (sq4 instanceof Checker) {
if (((Checker) sq4).getPlayer() == !getPlayer()) {
jumpSq4 = board[(getRow() - 2)] [(getCol() - 2)];
}
}
}
if (jumpSq1 != null && !jumpSq1.isOccupied()) {
arr.add(jumpSq1);
}
if (jumpSq2 != null && !jumpSq2.isOccupied()) {
arr.add(jumpSq2);
}
if (jumpSq3 != null && !jumpSq3.isOccupied()) {
arr.add(jumpSq3);
}
if (jumpSq4 != null && !jumpSq4.isOccupied()) {
arr.add(jumpSq4);
}
}
return arr;
}
/**
* this is also another instance of dynamic dispatch
* king checkers can move forwards AND backwards along diagonals
* they can only move one space though without jumping
*/
@Override
public boolean isValidMoveNoJump(int startRow, int startCol, int endRow, int endCol,
Square endSquare, boolean currPlayer) {
if (endSquare.isValidEndSpot() && this.isValidStartSpot(currPlayer)) {
if ((startRow + 1 == endRow || startRow - 1 == endRow)
&& (startCol + 1 == endCol || startCol - 1 == endCol)) {
return true;
}
}
return false;
}
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(super.getX(), super.getY(), super.getThickness(), super.getThickness());
g.setColor(super.getCheckerColor());
g.fillOval(super.getX() + 5, super.getY() + 5, RADIUS, RADIUS);
g.setColor(Color.WHITE);
g.drawLine(super.getX() + 30, super.getY() + 20, super.getX() + 30, super.getY() + 60);
g.drawLine(super.getX() + 30, super.getY() + 40, super.getX() + 50, super.getY() + 60);
g.drawLine(super.getX() + 30, super.getY() + 40, super.getX() + 50, super.getY() + 20);
}
@Override
public String toString() {
if (this.getColor().equals(Color.BLACK)) {
return "4";
} else {
return "5";
}
}
}