-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRook.java
More file actions
102 lines (96 loc) · 2.37 KB
/
Rook.java
File metadata and controls
102 lines (96 loc) · 2.37 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
package chessGame;
import javax.swing.ImageIcon;
public class Rook extends Unit {
Rook(int _color){
unitType = "R";
int value = 25;
color = _color;
captured = false;
if (color == 0) {
img = new ImageIcon("RW.png");
}
else if (color == 1) {
img = new ImageIcon("RB.png");
}
}
public void setUnits(Unit[][] units)
{
this.units = units;
}
public void setAvailableMoves(int currentX, int currentY, int moveNum)
{
//Horizontal and Vertical Movement
for(int i = currentX - 1; i >= 0; --i)
{
if(units[currentY][i] != null && units[currentY][i].color == this.color)
{
break;
}
if(units[currentY][i] != null && units[currentY][i].color != this.color)
{
possibleMoves.add(new int[] {i, currentY});
break;
}
possibleMoves.add(new int[] {i, currentY});
}
for(int i = currentX + 1; i <= 7; ++i)
{
if(units[currentY][i] != null && units[currentY][i].color == this.color)
{
break;
}
if(units[currentY][i] != null && units[currentY][i].color != this.color)
{
possibleMoves.add(new int[] {i, currentY});
break;
}
possibleMoves.add(new int[] {i, currentY});
}
for(int i = currentY - 1; i >= 0; --i)
{
if(units[i][currentX] != null && units[i][currentX].color == this.color)
{
break;
}
if(units[i][currentX] != null && units[i][currentX].color != this.color)
{
possibleMoves.add(new int[] {currentX, i});
break;
}
possibleMoves.add(new int[] {currentX, i});
}
for(int i = currentY + 1; i <= 7; ++i)
{
if(units[i][currentX] != null && units[i][currentX].color == this.color)
{
break;
}
if(units[i][currentX] != null && units[i][currentX].color != this.color)
{
possibleMoves.add(new int[] {currentX, i});
break;
}
possibleMoves.add(new int[] {currentX, i});
}
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;
}
}