-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKing.java
More file actions
91 lines (83 loc) · 1.81 KB
/
King.java
File metadata and controls
91 lines (83 loc) · 1.81 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
package chessGame;
import javax.swing.ImageIcon;
public class King extends Unit{
King(int _color){
unitType = "K";
value = 10000;
color = _color;
captured = false;
if (color == 0) {
img = new ImageIcon("KW.png");
}
else if (color == 1) {
img = new ImageIcon("KB.png");
}
}
public void setUnits(Unit[][] units)
{
this.units = units;
}
public void setAvailableMoves(int currentX, int currentY, int moveNum)
{
int[] up = {currentX, currentY + 1};
int[] upLeft = {currentX - 1, currentY + 1};
int[] left = {currentX - 1, currentY};
int[] leftDown = {currentX - 1, currentY - 1};
int[] down = {currentX, currentY - 1};
int[] downRight = {currentX + 1, currentY - 1};
int[] right = {currentX + 1, currentY};
int[] rightUp = {currentX + 1, currentY + 1};
if(onBoard(up))
{
possibleMoves.add(up);
}
if(onBoard(upLeft))
{
possibleMoves.add(upLeft);
}
if(onBoard(left))
{
possibleMoves.add(left);
}
if(onBoard(leftDown))
{
possibleMoves.add(leftDown);
}
if(onBoard(down))
{
possibleMoves.add(down);
}
if(onBoard(downRight))
{
possibleMoves.add(downRight);
}
if(onBoard(right))
{
possibleMoves.add(right);
}
if(onBoard(rightUp))
{
possibleMoves.add(rightUp);
}
removeFriendlyMoves();
}
public boolean checkMove(int desiredX, int desiredY)
{
int[] possibleMove = new int[2];
int possibleX = 0;
int possibleY = 0;
for(int i = 0; i < possibleMoves.size(); i++)
{
possibleMove = possibleMoves.get(i);
possibleX = possibleMove[0];
possibleY = possibleMove[1];
if ((desiredX == possibleX) && (desiredY == possibleY))
{
possibleMoves.clear();
return true;
}
}
possibleMoves.clear();
return false;
}
}