-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.java
More file actions
144 lines (132 loc) · 3.96 KB
/
Pawn.java
File metadata and controls
144 lines (132 loc) · 3.96 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
package chessGame;
import javax.swing.*;
public class Pawn extends Unit{
Pawn(int _color) {
unitType = "P";
value = 4;
color = _color;
captured = false;
if (color == 0) {
img = new ImageIcon("PW.png");
}
else if (color == 1) {
img = new ImageIcon("PB.png");
}
}
public void setUnits(Unit[][] units)
{
this.units = units;
}
public void setAvailableMoves(int currentX, int currentY, int moveNum)
{
//boolean isFirstTurn = isFirstMove(moveNum);
if (color == 0)
{
if (isFirstTurn)
{
int[] twoUp = {currentX, currentY - 2};
int[] oneUp = {currentX, currentY - 1};
int[] upLeft = {currentX - 1, currentY - 1};
int[] upRight = {currentX + 1, currentY - 1};
if(onBoard(twoUp) && units[currentY - 1][currentX] == null && units[currentY - 2][currentX] == null)
{
possibleMoves.add(twoUp);
}
if(onBoard(oneUp) && units[currentY - 1][currentX] == null)
{
possibleMoves.add(oneUp);
}
if (onBoard(upLeft) && units[currentY - 1][currentX - 1] != null && units[currentY - 1][currentX - 1].color != this.color)
{
possibleMoves.add(upLeft);
}
if (onBoard(upRight) && units[currentY - 1][currentX + 1] != null && units[currentY - 1][currentX + 1].color != this.color)
{
possibleMoves.add(upRight);
}
}
else
{
int[] oneUp = {currentX, currentY - 1};
int[] upLeft = {currentX - 1, currentY - 1};
int[] upRight = {currentX + 1, currentY - 1};
if (onBoard(oneUp) && units[currentY - 1][currentX] == null)
{
possibleMoves.add(oneUp);
}
if (onBoard(upLeft) && units[currentY - 1][currentX - 1] != null && units[currentY - 1][currentX - 1].color != this.color)
{
possibleMoves.add(upLeft);
}
if (onBoard(upRight) && units[currentY - 1][currentX + 1] != null && units[currentY - 1][currentX + 1].color != this.color)
{
possibleMoves.add(upRight);
}
}
}
else
{
if (isFirstTurn)
{
int[] twoDown = {currentX, currentY + 2};
int[] oneDown = {currentX, currentY + 1};
int[] downLeft = {currentX - 1, currentY + 1};
int[] downRight = {currentX + 1, currentY + 1};
if(onBoard(twoDown) && units[currentY + 1][currentX] == null && units[currentY + 2][currentX] == null)
{
possibleMoves.add(twoDown);
}
if(onBoard(oneDown) && units[currentY + 1][currentX] == null)
{
possibleMoves.add(oneDown);
}
if (onBoard(downLeft) && units[currentY + 1][currentX - 1] != null && units[currentY + 1][currentX - 1].color != this.color)
{
possibleMoves.add(downLeft);
}
if (onBoard(downRight) && units[currentY + 1][currentX + 1] != null && units[currentY + 1][currentX + 1].color != this.color)
{
possibleMoves.add(downRight);
}
}
else
{
int[] oneDown = {currentX, currentY + 1};
int[] downLeft = {currentX - 1, currentY + 1};
int[] downRight = {currentX + 1, currentY + 1};
if (onBoard(oneDown) && units[currentY + 1][currentX] == null)
{
possibleMoves.add(oneDown);
}
if (onBoard(downLeft) && units[currentY + 1][currentX - 1] != null && units[currentY + 1][currentX - 1].color != this.color)
{
possibleMoves.add(downLeft);
}
if (onBoard(downRight) && units[currentY + 1][currentX + 1] != null && units[currentY + 1][currentX + 1].color != this.color)
{
possibleMoves.add(downRight);
}
}
}
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;
}
}