Skip to content

Commit 7b5a51d

Browse files
author
Natanel Rudyuklakir
committed
first almost fully working Firebase player and maybe SocketPlayer
1 parent 9962090 commit 7b5a51d

File tree

12 files changed

+210
-121
lines changed

12 files changed

+210
-121
lines changed

.idea/deploymentTargetDropDown.xml

Lines changed: 0 additions & 17 deletions
This file was deleted.

app/src/main/java/com/example/chessfirebase/FirebasePlayer.java

Lines changed: 77 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
import androidx.annotation.Nullable;
77

88
import com.example.chessfirebase.chessClasses.Board;
9+
import com.example.chessfirebase.chessClasses.King;
910
import com.example.chessfirebase.chessClasses.Move;
11+
import com.example.chessfirebase.chessClasses.Pawn;
12+
import com.example.chessfirebase.chessClasses.Piece;
13+
import com.example.chessfirebase.chessClasses.Rook;
1014
import com.google.android.gms.tasks.OnCompleteListener;
1115
import com.google.android.gms.tasks.Task;
1216
import com.google.firebase.database.ChildEventListener;
@@ -35,68 +39,96 @@ public void run() {
3539
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
3640
Iterable<DataSnapshot> it= dataSnapshot.getChildren();
3741
for(DataSnapshot child: it) {
38-
if(child.getKey().equals("msg")){
42+
if(child.getKey().equals("msg") && !child.getValue().equals("")){
3943
String data = child.getValue(String.class);
40-
}
41-
}
42-
}
43-
44-
@Override
45-
public void onCancelled(@NonNull DatabaseError databaseError) {
46-
47-
}
48-
});
49-
/*
50-
* if(dataSnapshot.getKey().equals("msg")){
51-
String data=dataSnapshot.getValue(String.class);
52-
if(data.startsWith((!isWhite ? "white" :"black"))){
53-
data=data.split(".")[1];
54-
int a = situationCheck();
55-
if(data.contains("DRAW")){
56-
if(a==3){
44+
if(data.startsWith((!isWhite ? "white" :"black"))){
45+
data=data.split("-")[1];
46+
int a = situationCheck();
47+
if(data.contains("DRAW")){
5748
//its a draw
5849
//handle draw
5950
gameOver();
6051
return;
61-
}
62-
}else if(data.contains("MATE")) {
63-
//we won
64-
gameOver();
65-
return;
66-
}else if(data.contains("STALEMATE_MAT")){
67-
if (a==4){
52+
}else if(data.contains("MATE")) {
53+
//we won
6854
gameOver();
55+
return;
56+
}else if(data.contains("STALEMATE_MAT")){
57+
if (a==4){
58+
gameOver();
59+
sendDraw();
60+
return;
61+
}
62+
data=data.split(" ")[1];
63+
}
64+
Move m=convertStringToMove(data);
65+
if(board.getCell(m.from[0],m.from[1]).getPiece() instanceof King && Math.abs(m.from[1]-m.to[1])==2){
66+
// castles
67+
// move the rook to the place
68+
if(isWhite){
69+
if(m.to[1]==2){
70+
//long castles
71+
int[] from= new int[]{0,0};
72+
int[] to=new int[]{0,3};
73+
board.getCell(from[0],from[1]).getPiece().move(to[0],to[1]);
74+
board.opponentMove(from,to);
75+
}else{
76+
int[] from= new int[]{0,7},to= new int[]{0,5};
77+
board.getCell(from[0],from[1]).getPiece().move(to[0],to[1]);
78+
board.opponentMove(from,to);
79+
}
80+
}else{
81+
if(m.to[1]==1){
82+
//short castles
83+
int[] from=new int[]{0,0},to=new int[]{0,2};
84+
board.opponentMove(from,to);
85+
}else{
86+
//long castles
87+
int[] from=new int[]{0,7},to=new int[]{0,4};
88+
board.opponentMove(from,to);
89+
}
90+
}
91+
}
92+
board.opponentMove(m.from,m.to);
93+
addMove(m);
94+
a=situationCheck();
95+
if(a==2){
96+
//its mate we lost
97+
sendOppWon();
98+
gameOver();//TODO:add the winner in here as a var
99+
return;
100+
}else if(a==3) {
69101
sendDraw();
102+
gameOver();
70103
return;
71104
}
72-
data=data.split(" ")[1];
105+
toggleOurTurn();
73106
}
74-
Move m=convertStringToMove(data);
75-
board.move(m.from,m.to);
76-
toggleOurTurn();
77107
}
78108
}
79-
* */
109+
}
110+
111+
@Override
112+
public void onCancelled(@NonNull DatabaseError databaseError) {
113+
114+
}
115+
});
80116
}
81117
public void sendMove(Move mv){
82118
//se send the move
83119
int a =situationCheck();
84-
if(a==3){
85-
//handle draw by stalemate
86-
sendDraw();
87-
gameOver();
88-
return;
89-
}else if(a==2){
90-
//Mate, we lost
91-
sendOppWon();
92-
gameOver();
93-
return;
94-
}else if(a==1){
95-
//check , make a vibration
96-
}else if (a==4){
120+
if (a==4){
97121
sendStaleMateByMaterial(mv);
98122
}
99-
roomRef.child("msg").setValue((isWhite ? "white.":"black.")+mv.toString());
123+
Piece p=board.getCell(mv.from[0],mv.from[1]).getPiece();
124+
if(p instanceof King){
125+
((King)p).isFirstMove=false;
126+
}else if(p instanceof Pawn){
127+
((Pawn)p).isFirstMove=false;
128+
}else if(p instanceof Rook){
129+
((Rook)p).isFirstMove=false;
130+
}
131+
roomRef.child("msg").setValue((isWhite ? "white-":"black-")+mv.toString());
100132
}
101133
public void sendDraw(){
102134
roomRef.child("msg").setValue("DRAW");
@@ -105,7 +137,7 @@ public void sendOppWon(){
105137
roomRef.child("msg").setValue("MATE");
106138
}
107139
public void sendStaleMateByMaterial(Move mv){
108-
roomRef.child("msg").setValue("STALEMATE_MAT "+(isWhite ? "white":"black")+mv.toString());
140+
roomRef.child("msg").setValue("STALEMATE_MAT "+(isWhite ? "white-":"black-")+mv.toString());
109141
}
110142

111143
@Override

app/src/main/java/com/example/chessfirebase/Player.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Player(){
4242
}
4343

4444
public Move convertStringToMove(String mv){
45-
String[] conv=mv.split(":");
45+
String[] conv=mv.replaceAll("\\s+","").split(":");
4646
int[] from={Integer.parseInt(conv[0].split(",")[0]),Integer.parseInt(conv[0].split(",")[1])};
4747
int[] to={Integer.parseInt(conv[1].split(",")[0]),Integer.parseInt(conv[1].split(",")[1])};
4848
from[0]=7-from[0];
@@ -87,8 +87,8 @@ public void calculateMoves(){
8787

8888
public int situationCheck(){
8989
//return 0 if nothing, 1 if check 2 if mate 3 if draw.
90+
calculateMoves();
9091
if(board.isCheck(isWhite)){
91-
calculateMoves();
9292
//here if we get true then its mate because its check and there are no possible moves.
9393
if(stalemate()){
9494
return 2;//we have no moves and its mate

app/src/main/java/com/example/chessfirebase/chessClasses/Bishop.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ public char toChar() {
5454
return this.isWhite ? 'B' : 'b';
5555
}
5656

57+
@Override
58+
public void oppMove(int r, int c) {
59+
move(r, c);
60+
}
61+
5762
}

app/src/main/java/com/example/chessfirebase/chessClasses/Board.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,31 @@ public void psuedoClickListener(int row,int col){//TODO: chancge the calculate m
147147
currSelectedPiece.isSelected=true;
148148
}
149149
}else if(currSelectedPiece!=null){
150-
List<int[]> lst=currSelectedPiece.getMoves();
150+
if(board[row][col].getPiece()!=null && board[row][col].getPiece().isWhite==player.isWhite){
151+
currSelectedPiece.isSelected=false;
152+
currSelectedPiece=board[row][col].getPiece();
153+
currSelectedPiece.isSelected=true;
154+
from=currSelectedPiece.getRawPos();
155+
}
151156
isFirstClick=!isFirstClick;
152-
for(int[] mov: lst){
157+
List<int[]> lst=currSelectedPiece.getMoves();
158+
for(int i=0;i<lst.size();i++){
159+
int[] mov=lst.get(i);
153160
if(mov[0]==row && mov[1]==col){
154161
player.toggleOurTurn();
155162
this.playerMove(from , mov);
156163
Move m=new Move(from,mov);
164+
if(currSelectedPiece instanceof Pawn)((Pawn) currSelectedPiece).isFirstMove=false;
165+
if(currSelectedPiece instanceof King)((King) currSelectedPiece).isFirstMove=false;
166+
if(currSelectedPiece instanceof Rook)((Rook) currSelectedPiece).isFirstMove=false;
157167
if(player instanceof socketPlayer){
158168
((socketPlayer)player).ch.move=from[0]+","+from[1]+":"+mov[0]+","+mov[1];
159169
}else{
160170
//the player is a firebase player
161171
((FirebasePlayer)player).sendMove(m);
162172
}
163173
player.addMove(m);
164-
if(currSelectedPiece instanceof Pawn)((Pawn) currSelectedPiece).isFirstMove=false;
165-
if(currSelectedPiece instanceof King)((King) currSelectedPiece).isFirstMove=false;
166-
if(currSelectedPiece instanceof Rook)((Rook) currSelectedPiece).isFirstMove=false;
174+
167175
player.calculateMoves();
168176
break;
169177
}
@@ -201,6 +209,15 @@ public void draw(Canvas canvas){
201209
}
202210
drawValidMoves(canvas);
203211
}
212+
public void opponentMove(int[] from, int[] to){
213+
Piece p=this.board[to[0]][to[1]].getPiece();
214+
if(p!=null){
215+
player.remove(p);
216+
}
217+
this.board[to[0]][to[1]].setPiece(this.board[from[0]][from[1]].getPiece());
218+
this.board[from[0]][from[1]].setPiece(null);
219+
this.board[to[0]][to[1]].getPiece().oppMove(to[0],to[1]);
220+
}
204221

205222
public void drawValidMoves(Canvas canvas){
206223
for(Piece p: player.pieces){

app/src/main/java/com/example/chessfirebase/chessClasses/King.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ public boolean isCheck(Board b){
9696
//run BFS to check if its check+ 8 moves for Knight and 2 for pawns.
9797
//first diagonal then vertical
9898
Piece p;
99-
for(int i=1;i<dir.length;i++) {
100-
for(int r=this.row+dir[0][0],c=this.col+dir[0][1];r<8 && r>=0 && c<8 && c>=0;r+=dir[i][0],c+=dir[i][1]){
99+
for(int i=0;i<dir.length;i++) {
100+
for(int r=this.row+dir[i][0],c=this.col+dir[i][1];r<8 && r>=0 && c<8 && c>=0;r+=dir[i][0],c+=dir[i][1]){
101101
if((p=b.getCell(r,c).getPiece())!=null){
102102
if(p.isWhite!=this.isWhite){//check if not our color
103103
if(i<=3){
@@ -167,6 +167,11 @@ public void move(int row, int col) {
167167
super.move(row, col);
168168
}
169169

170+
@Override
171+
public void oppMove(int r, int c) {
172+
super.move(r, c);
173+
}
174+
170175
@Override
171176
public char toChar() {
172177
return this.isWhite ? 'K' : 'k';

app/src/main/java/com/example/chessfirebase/chessClasses/Knight.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,9 @@ public char toChar() {
3232
return this.isWhite ? 'N' : 'n';
3333
}
3434

35+
@Override
36+
public void oppMove(int r, int c) {
37+
super.move(r, c);
38+
}
39+
3540
}

app/src/main/java/com/example/chessfirebase/chessClasses/Pawn.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public void move(int row, int col) {
6161
super.move(row, col);
6262
}
6363

64+
@Override
65+
public void oppMove(int r, int c) {
66+
super.move(r, c);
67+
}
68+
6469
@Override
6570
public char toChar() {
6671
return this.isWhite ? 'P':'p';

app/src/main/java/com/example/chessfirebase/chessClasses/Piece.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ public void drawValidMoves(Canvas canvas){
116116
paint.setAlpha(85);
117117
float circleSize = 0.5f*size;
118118
int c=0;
119-
for(int[] mov:possibleMoves){
119+
for(int i=0;i<possibleMoves.size();i++){
120+
int[] mov=possibleMoves.get(i);
120121
//draw the gray circle
121122
canvas.drawCircle(mov[1]*size+0.5f*size,mov[0]*size+0.5f*size,circleSize/2,paint);
122123
c++;
@@ -132,4 +133,6 @@ public void move(int row,int col){
132133
this.row=row;
133134
this.col=col;
134135
}
136+
137+
public abstract void oppMove(int r, int c);
135138
}

app/src/main/java/com/example/chessfirebase/chessClasses/Queen.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ public void calculateMoves(Board b){
3737
public char toChar() {
3838
return this.isWhite ? 'Q' : 'q';
3939
}
40+
41+
@Override
42+
public void oppMove(int r, int c) {
43+
super.move(r, c);
44+
}
4045
}

0 commit comments

Comments
 (0)