-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPawn.java
151 lines (123 loc) · 3.88 KB
/
Pawn.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// This is inheritance. The current unit we are looking at, Unit 9.
// The Pawn is a Piece and is allowed to use all of the attibutes, constructors and methods of its parent
public class Pawn extends Piece {
private boolean[][] possibleMoves = new boolean[8][8]; // 2D array of booleans indicating valid moves
private Position nextPosition; // the next move, as decided by your code in setNextPosition
private int x;
private int y;
// constructor for a Pawn, which calls the Piece constructor using "super"
public Pawn(Team c, int id, int x, int y) {
super(c,Piece.Type.PAWN,id);
this.x = x;
this.y = y;
for(int i=0; i<8; ++i) {
for(int j=0; j<8; ++j) {
possibleMoves[i][j] = false;
}
}
nextPosition = new Position(x,y);
}
// utility method to output the possibleMoves 2D Array
private void printPossibleMoves() {
for(int i=0; i<8; ++i) {
System.out.print("|");
for(int j=0; j<8; ++j) {
if(possibleMoves[j][i]) System.out.print("T|");
else System.out.print("F|");
}
System.out.println();
}
}
// overridden method. Makes a 2D array of booleans. False is no move is possible. True if possible to move there.
// I have coded the possible moves for a pawn.
@Override public boolean[][] getPossibleMoves(ChessGameBoard b) {
int x = myX();
int y = myY();
//System.out.println("x:" + x + " y:"+y + " "+ getTeam());
if(getTeam()==Team.BLACK && x<7) {
//if space in front is open
if(b.getPiece(x+1,y)==null) {
//System.out.println("x+1");
possibleMoves[y][x+1] = true;
//if in starting position and two spaces in front are open
if(x==1 && b.getPiece(x+2,y)==null) {
//System.out.println("x+2");
possibleMoves[y][x+2] = true;
}
} else //System.out.println(""+ b.getPiece(x+1,y).getID()) ;
//if a diagonal kill is possible
if((y>1 && b.getPiece(x+1,y-1)!=null && !isTeamate(b.getPiece(x+1,y-1)))) {
possibleMoves[y-1][x+1] = true;
}
//if a diagonal kill is possible
if((y<7 && b.getPiece(x+1,y+1)!=null && !isTeamate(b.getPiece(x+1,y+1)))) {
possibleMoves[y+1][x+1] = true;
}
}
if(getTeam()==Team.WHITE && x>1) {
//if space in front is open
if(b.getPiece(x-1,y)==null) {
possibleMoves[y][x-1] = true;
//if in starting position and two spaces in front are open
if(x==6 && b.getPiece(x-2,y)== null) {
possibleMoves[y][x-2] = true;
}
}
//if a diagonal kill is possible
if((y>1 && b.getPiece(x-1,y-1)!= null && !isTeamate(b.getPiece(x-1,y-1)))) {
possibleMoves[y-1][x-1] = true;
}
//if a diagonal kill is possible
if((y<7 && b.getPiece(x-1,y+1)!= null && !isTeamate(b.getPiece(x-1,y+1)))) {
possibleMoves[y+1][x-1] = true;
}
}
//printPossibleMoves();
//System.out.println();
return possibleMoves;
}
// you need to code this based on the results of possibleMoves()
public void setNextPosition(ChessGameBoard b) {
// update the 2D array of possible moves
possibleMoves = getPossibleMoves(b);
int x = myX();
int y = myY();
int random_x = (int) (Math.random()*8);
int random_y = (int) (Math.random()*8);
int count = 10;
//System.out.println("x:" + random_x + "y:" + random_y);
while (!possibleMoves[random_y][random_x] && count > 0) {
random_x = (int) (Math.random()*8);
random_y = (int) (Math.random()*8);
//System.out.println("x:" + random_x + "y:" + random_y);
count--;
}
if (count > 0) {
x = random_x;
y = random_y;
}
// set the next position
nextPosition.setPosition(x,y);
}
public int myX() {
return x;
}
public int myY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public Position getPosition() {
return new Position(x,y);
}
public Position getNextPosition() {
return nextPosition;
}
public boolean isStayingPut() {
return getPosition() == nextPosition;
}
}