-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pawn.java
201 lines (164 loc) · 4.53 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package Pawn_pack;
import java.io.*;
import Pieces_pack.*;
import Board_pack.*;
import java.util.*;
import Position_pack.*;
import Queen_pack.*;
import Knight_pack.*;
import Rook_pack.*;
import Bishop_pack.*;
public class Pawn extends Piece implements Serializable
{
public Pawn(Position pos,String clr) //constructor for pawn class
{
init_pos=pos;
team_clr=clr;
}
public boolean validmove(Position final_pos) //check whether the move made is valid or not
{
int sub_x=final_pos.x-init_pos.x;
int sub_y=final_pos.y-init_pos.y;
Scanner scan=new Scanner(System.in);
boolean res=false;
Piece temp=null;
int i;
int c;
/*System.out.println(sub_x+" "+sub_y);*/
if(sub_y>0 && team_clr.equals("white"))
{
if((sub_x==-1 || sub_x==1) && (sub_y==1)) // pawn can move only one step forward slantly if the other piece is of opposite colour
{
temp=Board.piece[final_pos.x][final_pos.y];
if(temp!=null) // checking if the piece at the final pos is empty of filled, and if it is not filled
{
if((temp.team_clr).equals("black")) // if the colour is of opposite type
{
res=true; // retuning boolean value true
}
}
}
else if(sub_x==0) // if it is moving forward, if it is at the starting position, it can make 2 steps
{
if(sub_y==1) // if it is making one step independent of the starting position
{
temp=Board.piece[final_pos.x][final_pos.y];
if(temp==null)
{
res=true;
}
}
else if(sub_y==2 && init_pos.y==1)// if it is making 2 positions,checking whether the starting position is 1 or not
{
i=1;
c=0;
while(i<=2)
{
temp=Board.piece[init_pos.x][init_pos.y+i];
if(temp!=null)
{
break;
}
else
{
c++;
}
i++;
}
if(c==2)
{
res=true;
}
}
}
}
else if(sub_y<0 && team_clr.equals("black")) // same as above follows if the player_clr is black
{
if((sub_x==-1 || sub_x==1) && (sub_y==-1))
{
/*System.out.println(sub_x+" "+sub_y);*/
temp=Board.piece[final_pos.x][final_pos.y];
if(temp!=null)
{
if((temp.team_clr).equals("white"))
{
res=true;
}
}
}
else if(sub_x==0)
{
if(sub_y==-1 )
{
temp=Board.piece[final_pos.x][final_pos.y];
if(temp==null)
{
res=true;
}
}
else if(sub_y==-2 && init_pos.y==6)
{
i=1;
c=0;
while(i<=2)
{
temp=Board.piece[init_pos.x][init_pos.y-i];
if(temp!=null)
{
break;
}
else
{
c++;
}
i++;
}
if(c==2)
{
res=true;
}
}
}
}
return res;
}
public void re_entry(Position final_pos,String again) // if the player's pawn reaches the opposite end line , he will get a chance to re-enter the killed pieces
{
Scanner scan=new Scanner(System.in);
switch(again) // switch case for choosing which piece to re-enter on the board
{
case "Queen":
if(again.equals("Queen"))
{
Queen queen=new Queen(new Position(final_pos.x,final_pos.y),team_clr);
Board.piece[final_pos.x][final_pos.y]=queen;
break;
}
case "Bishop":
if(again.equals("Bishop"))
{
Bishop bishop=new Bishop(new Position(final_pos.x,final_pos.y),team_clr);
Board.piece[final_pos.x][final_pos.y]=bishop;
break;
}
case "Rook":
if(again.equals("Rook"))
{
Rook rook=new Rook(new Position(final_pos.x,final_pos.y),team_clr);
Board.piece[final_pos.x][final_pos.y]=rook;
break;
}
case "Knight":
if(again.equals("Knight"))
{
Knight knight=new Knight(new Position(final_pos.x,final_pos.y),team_clr);
Board.piece[final_pos.x][final_pos.y]=knight;
break;
}
default:
System.out.println("you have entered an invalid input"); // if it is not the above pieces this will ask for a valid input
String returns=scan.nextLine();
re_entry(final_pos,returns);
}
}
}