-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
240 lines (212 loc) · 7.83 KB
/
Player.java
File metadata and controls
240 lines (212 loc) · 7.83 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//imports
import java.io.*;
import java.net.Socket;
import java.util.*;
public class Player{
private Board board;//think about how move mechanism will work (almost done)
public boolean isWhite;
public boolean ourTurn;
public List<Piece> pieces;
public ConnectionHandler ch;
String host="localhost";
int port = 8888;
public Player(){
this.pieces=new ArrayList<Piece>();
this.ch=new ConnectionHandler(host,port);
}
public Board getBoard() {return board;}
public ConnectionHandler getConnectionHandler(){
return this.ch;
}
public static void main(String[] args) throws Exception {
Player p=new Player();
p.playerSetup(p.getConnectionHandler().getIsWhite());
p.getBoard().printBoard();
System.out.println("\n");
p.calculateMoves();
new Thread(p.ch).run();
System.out.println("\n");
}
public void playerSetup(boolean isWhite) throws InterruptedException{
this.board=new Board(isWhite, this);
this.isWhite=isWhite;
this.ourTurn=this.isWhite;
}
//add a check for what pieces there are to know if its draw!.
public boolean checkPossibleMoves(){
boolean bishop=false,knight=false;
if(pieces.size()==1)return true;//only a king left
for(Piece p : pieces){
if(p instanceof Bishop || p instanceof Knight || p instanceof King){
bishop=p instanceof Bishop;
knight=p instanceof Knight;
}
if(p.possibleMoves.size()>0 && pieces.size()>1){
return false;
}
}
if(bishop^knight){
// only the bishop or the knight is left or both kings
return true;
}
return true ;
}
public void calculateMoves(){
for(int i=0;i<this.pieces.size();i++){
Piece p=this.pieces.get(i);
if(p != null){
p.calculateMoves(board);
}
}
}
public void move(int[] curr,int[] to){//add a listener that will listen where clicked after a piece was selected to get the x and y
Piece p=board.getCell(curr[0], curr[1]).getPiece();
for(int[] mov:p.possibleMoves){
if(mov[0]==to[0] && mov[1]==to[1]){
ourTurn=!ourTurn;
this.ch.move=curr[0]+","+curr[1]+" "+to[0]+","+to[1];
}
}
}
//after we recieve the move:
public int situationCheck(){
//return 0 if nothing, 1 if check 2 if mate 3 if draw.
if(board.isCheck(isWhite)){
calculateMoves();
//here if we get true then its mate because its check and there are no possible moves.
if(checkPossibleMoves()){
return 2;
}else{
return 1;
}
}else{
//check for draw
if(checkPossibleMoves()){
return 3;
}
}
return 0;
}
public boolean checkForPromotion(){
for(int i=0;i<8;i++){
if(board.getCell(0, i).getPiece() instanceof Pawn && board.getCell(0, i).getPiece().isWhite==this.isWhite){
return true;//handle promotion logic to send to opp.
}
}
return false;
}
public void remove(Piece p){
this.pieces.remove(p);
}
//will add the listener in the gameview in android studio which willl be a thread.
//it will call the move function
public class ConnectionHandler implements Runnable{
private Socket s;
private BufferedReader reader;
private PrintWriter writer;
private boolean ourTurn;
private boolean isWhite;
private boolean isGameOver=false;
public String move;
public ConnectionHandler(String host,int port){
try {
s=new Socket(host,port);
writer=new PrintWriter(s.getOutputStream(),true);
reader=new BufferedReader(new InputStreamReader(s.getInputStream()));
String msg="";
//first we send a message wheter the playee is white of=r black
while(msg==null || msg.equals(""))
msg=this.reader.readLine();
this.isWhite = Boolean.parseBoolean(msg);
this.ourTurn=this.isWhite;
}catch (IOException e) {
e.printStackTrace();
System.out.println("Eror");
}
}
public boolean getIsWhite(){
return isWhite;
}
public void sendDraw() {
try {
writer.write("DRAW");
} catch (Exception e) {
// TODO: handle exception
}
}
public void sendOppWon(){
try {
writer.println("MATE");
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void run() {
String msg="";
while(true){
try{
if(ourTurn){
calculateMoves();
int a=situationCheck();//need to recheck the function
if(a==3){
isGameOver=true;
//handle draw
}else if(a==2){
//Mate, we lost
isGameOver=true;
}else if(a==1){
//check , make a vibreation
}
if(this.move!=null && !this.move.equals("")){
writer.println(this.move);
this.move="";
ourTurn=!ourTurn;
}
}
else{
msg=reader.readLine();
if(msg!=null && msg.equals("DRAW")){
if(situationCheck()==3){
//its a draw
//handle draw
this.isGameOver=true;
}
}else if(msg != null && msg.equals("MATE")){
//we won
isGameOver=true;
}
this.move=msg;
//make the move
if(this.move != null && !this.move.equals("")){
String[] conv=this.move.split(" ");
int[] from={Integer.parseInt(conv[0].split(",")[0]),Integer.parseInt(conv[0].split(",")[1])};
int[] to={Integer.parseInt(conv[1].split(",")[0]),Integer.parseInt(conv[1].split(",")[1])};
from[0]=7-from[0];
from[1]=7-from[1];
to[0]=7-to[0];
to[1]=7-to[1];
board.move(from, to);
this.ourTurn=!ourTurn;
int a =situationCheck();
if(a==1){
//Makwe a sound or vibration
}else if(a==2){
isGameOver=true;
sendOppWon();
}else if(a==3){
sendDraw();
}
ourTurn=!ourTurn;
}
}
}catch(Exception e){
//this.isGameOver=true;
//TODO handle exception
e.printStackTrace();
break;
}
}
}
}
}