-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.java
More file actions
91 lines (83 loc) · 1.95 KB
/
Knight.java
File metadata and controls
91 lines (83 loc) · 1.95 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 Knight extends Unit{
Knight(int _color){
unitType = "KN";
value = 10;
color = _color;
captured = false;
if (color == 0) {
img = new ImageIcon("KNW.png");
}
else if (color == 1) {
img = new ImageIcon("KNB.png");
}
}
public void setUnits(Unit[][] units)
{
this.units = units;
}
public void setAvailableMoves(int currentX, int currentY, int moveNum)
{
int[] twoLeftUp = {currentX - 2, currentY + 1};
int[] twoLeftDown = {currentX -2, currentY - 1};
int[] twoDownLeft = {currentX - 1, currentY - 2};
int[] twoDownRight = {currentX + 1, currentY - 2};
int[] twoRightDown = {currentX + 2, currentY - 1};
int[] twoRightUp = {currentX + 2, currentY + 1};
int[] twoUpRight = {currentX + 1, currentY + 2};
int[] twoUpLeft = {currentX - 1, currentY + 2};
if(onBoard(twoLeftUp))
{
possibleMoves.add(twoLeftUp);
}
if(onBoard(twoLeftDown))
{
possibleMoves.add(twoLeftDown);
}
if(onBoard(twoDownLeft))
{
possibleMoves.add(twoDownLeft);
}
if(onBoard(twoDownRight))
{
possibleMoves.add(twoDownRight);
}
if(onBoard(twoRightDown))
{
possibleMoves.add(twoRightDown);
}
if(onBoard(twoRightUp))
{
possibleMoves.add(twoRightUp);
}
if(onBoard(twoUpRight))
{
possibleMoves.add(twoUpRight);
}
if(onBoard(twoUpLeft))
{
possibleMoves.add(twoUpLeft);
}
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;
}
}