Skip to content

Commit 2a9b18c

Browse files
author
Natanel Rudyuklakir
committed
fixed a major bug
1 parent 56496d5 commit 2a9b18c

File tree

11 files changed

+62
-53
lines changed

11 files changed

+62
-53
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,6 @@ protected void onCreate(Bundle savedInstanceState) {
6060
e.printStackTrace();
6161
}
6262
cgv.start(p.getBoard());
63-
cgv.setOnTouchListener(new View.OnTouchListener() {
64-
@Override
65-
public boolean onTouch(View v, MotionEvent event) {
66-
p.getBoard().psuedoClickListener(event.getX(),event.getY());
67-
return false;
68-
}
69-
});
7063
}
7164

7265

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public Bishop(int row,int col,boolean isWhite) {
2323
//calculateing the moves using bfs
2424

2525
public void calculateMoves(Board b){
26+
this.possibleMoves.clear();
2627
int[][] dir={{-1,-1},{-1,1},{1,-1},{-1,-1}};
2728
//top left, top right, bottom left, bottom right
2829

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

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@
44

55

66
import android.graphics.Canvas;
7-
import android.graphics.Paint;
8-
import android.graphics.Rect;
97
import android.util.Log;
108

119
import com.example.chessfirebase.socketPlayer;
1210

11+
import java.util.List;
12+
1313
public class Board{//add the tap listener later in android studio
1414
//variables
1515
private BoardCell[][] board;
1616
private King wk;
1717
private King bk;
1818
public Piece remove;
19-
public boolean isFirstClick;
19+
public boolean isFirstClick=true;
2020
int[] from;
2121
private socketPlayer player;
22+
private Piece currSelectedPiece;
2223

2324
public Board(boolean isWhite,socketPlayer p){
2425
this.player=p;
26+
from = new int[2];
2527
board=new BoardCell[8][8];
2628
for(int i=0;i<board.length;i++){
2729
for(int j=0;j<board.length;j++){
@@ -148,27 +150,28 @@ public Board(boolean isWhite,socketPlayer p){
148150
}
149151
}
150152

151-
public void psuedoClickListener(float rawX,float rawY){
152-
int row=Math.round(rawY%BoardCell.size);
153-
int col=Math.round(rawX%BoardCell.size);
154-
Piece curr=this.getCell(row, col).getPiece();
153+
public void psuedoClickListener(int row,int col){
154+
// if(!player.ourTurn)return; TODO:uncomment for release
155155
if(isFirstClick){
156-
if(curr!=null && curr.isWhite==player.isWhite){
157-
from=curr.getRawPos();
156+
currSelectedPiece=board[row][col].getPiece();
157+
if(currSelectedPiece!=null && currSelectedPiece.isWhite==player.isWhite){
158+
from=currSelectedPiece.getRawPos();
158159
isFirstClick=!isFirstClick;
159-
curr.drawValidMoves();
160+
currSelectedPiece.drawValidMoves();
160161
}
161-
}else{
162-
row=Math.round(rawY%BoardCell.size);
163-
col=Math.round(rawX%BoardCell.size);
164-
for(int[] mov: curr.getMoves()){
162+
}else if(currSelectedPiece!=null){
163+
List<int[]> lst=currSelectedPiece.getMoves();
164+
isFirstClick=!isFirstClick;
165+
for(int[] mov: lst){
165166
if(mov[0]==row && mov[1]==col){
166-
player.move(from, new int[]{row,col});
167-
isFirstClick=!isFirstClick;
168-
this.move(from , mov);
167+
player.toggleOurTurn();
168+
this.playerMove(from , mov);
169169
player.ch.move=from[0]+","+from[1]+" "+mov[0]+","+mov[1];
170+
player.calculateMoves();
171+
break;
170172
}
171173
}
174+
currSelectedPiece=null;
172175
}
173176
}
174177

@@ -180,16 +183,12 @@ public void move(int[] from,int[] to) {
180183

181184
public void playerMove(int[] from,int[] to){
182185
Piece p=this.getCell(to[0], to[1]).getPiece();
183-
if(p!=null){
184-
player.remove(p);
185-
this.board[to[0]][to[1]].setPiece(this.board[from[0]][from[1]].getPiece());
186-
this.board[from[0]][from[1]].setPiece(null);
187-
this.board[to[0]][to[1]].getPiece().move(to[0],to[1]);
188-
}else{
189-
this.board[to[0]][to[1]].setPiece(this.board[from[0]][from[1]].getPiece());
190-
this.board[from[0]][from[1]].setPiece(null);
191-
this.board[to[0]][to[1]].getPiece().move(to[0],to[1]);
192-
}
186+
if(p!=null && p.isWhite!=player.isWhite) player.remove(p);
187+
188+
this.board[to[0]][to[1]].setPiece(this.board[from[0]][from[1]].getPiece());
189+
this.board[from[0]][from[1]].setPiece(null);
190+
this.board[to[0]][to[1]].getPiece().move(to[0],to[1]);
191+
Log.d("NEWSQUARE", "piece "+(board[to[0]][to[1]].getPiece()==null));
193192
}
194193

195194
public void draw(Canvas canvas){

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public King(int row,int col,boolean isWhite){
1818
}
1919

2020
public void calculateMoves(Board b){
21+
this.possibleMoves.clear();
2122
int[][] dir={{-1,-1},{-1,1},{1,-1},{1,1},{1,0},{0,1},{-1,0},{0,-1}};
2223
for(int[] d:dir){
2324
if(this.row+d[0]<8 && this.row+d[0]>=0 && this.col+d[1]<8 && this.col+d[1]>=0 && (b.getCell(this.row+d[0],this.col+d[1]).getPiece()==null || b.getCell(this.row+d[0],this.col+d[1]).getPiece().getIsWhite()!=this.isWhite)){

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public Knight(int row, int col,boolean isWhite){
1717
}
1818

1919
public void calculateMoves(Board b){
20+
this.possibleMoves.clear();
2021
int[][] moves={{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{-1,2},{1,-2},{-1,-2}};//the possible 8 moves.
2122
for(int[] mov:moves){
2223
if(this.row+mov[0]<8 && this.row+mov[0]>=0 && this.col+mov[1]<8 && this.col+mov[1]>=0 && (b.getCell(this.row+mov[0],this.col+mov[1]).getPiece()==null || b.getCell(this.row+mov[0],this.col+mov[1]).getPiece().getIsWhite()!=this.isWhite)){

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public Pawn(int row, int col, boolean isWhite) {
2626

2727
//methods:
2828
public void calculateMoves(Board b){
29+
this.possibleMoves.clear();
2930
//change to check the moves better - we dont have a check checker
3031
if (this.row-1>=0 && b.getCell(this.row-1,this.col).getPiece()==null){
3132
if(isFirstMove && this.row-2>=0 && b.getCell(this.row-2,this.col).getPiece()==null){
@@ -34,12 +35,9 @@ public void calculateMoves(Board b){
3435
isFirstMove = false;
3536
super.possibleMoves.add(new int[] {this.row-1,this.col});
3637
}
37-
if(row-1>=0 && col+1<8 && b.getCell(row-1, col+1).getPiece()!=null)
38-
super.possibleMoves.add(new int[] {row-1,col+1});
38+
if(row-1>=0 && col+1<8 && b.getCell(row-1, col+1).getPiece()!=null)super.possibleMoves.add(new int[] {row-1,col+1});
3939

40-
41-
if(row-1>=0 && col-1>=0 && b.getCell(row-1, col-1).getPiece()!=null)
42-
super.possibleMoves.add(new int[] {row-1,col-1});
40+
if(row-1>=0 && col-1>=0 && b.getCell(row-1, col-1).getPiece()!=null)super.possibleMoves.add(new int[] {row-1,col-1});
4341

4442
validateMoves(b);
4543
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,24 @@ public void validateMoves(Board b){
6666
//checking for pinned pieces
6767
List<int[]> moves=this.possibleMoves;
6868
List<int[]> remove=new ArrayList<>();
69-
int[] currPos;
69+
int[] currPos=new int[]{this.row,this.col};
70+
Log.i("posOfPiece",currPos[0]+", "+currPos[1]);
7071
//check what moves to remove:
7172
for(int i=0;i<moves.size();i++){
72-
//move the piece without drawing
73+
//move the piece
7374
int[] mov=moves.get(i);
74-
currPos=this.getRawPos();
7575
Piece p=b.getCell(mov[0], mov[1]).getPiece();
76+
//maybe add an edge case of same colored pieces but probably handled by cacl moves
7677
b.move(currPos, mov);
7778
if(b.isCheck(this.isWhite)){
7879
remove.add(mov);
7980
}
8081
//move the piece back
81-
b.move(mov,currPos);
82+
if(mov[0]==currPos[0] && mov[1]==currPos[1]){
83+
Log.d("MOVE","SAME_MOVE_ERROR");
84+
Log.d("THE MOVE",mov[0]+","+mov[1]);
85+
}
86+
b.move(mov,currPos);//the problematic func
8287
if(p!=null){
8388
b.getCell(mov[0], mov[1]).setPiece(p);
8489
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public Queen(int row,int col,boolean isWhite){
1515
}
1616
//TODO: implement movement.
1717
public void calculateMoves(Board b){
18+
this.possibleMoves.clear();
1819
int[][] dir={{-1,-1},{-1,1},{1,-1},{-1,-1},{1,0},{0,1},{-1,0},{0,-1}};
1920
for(int[] d:dir){
2021
for(int r=this.row,c=this.col;r>=0 && c>=0 && r<8 && c<8;r+=d[0],c+=d[1]){

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public Rook(int row, int col, boolean isWhite) {
2323
//methods
2424

2525
public void calculateMoves(Board b){
26+
this.possibleMoves.clear();
2627

2728
int[][] dir={{1,0},{0,1},{-1,0},{0,-1}};
2829
//down right up left

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import android.graphics.Canvas;
55
import android.graphics.Color;
66
import android.util.AttributeSet;
7+
import android.util.Log;
78
import android.view.MotionEvent;
89
import android.view.SurfaceHolder;
910
import android.view.SurfaceView;
11+
import android.view.View;
1012

1113
import com.example.chessfirebase.chessClasses.Board;
1214
import com.example.chessfirebase.chessClasses.BoardCell;
@@ -26,9 +28,23 @@ public customGameView(Context context, float width, float height) {
2628
}
2729
public void start(Board b){
2830
this.b=b;
31+
this.setOnTouchListener(new OnTouchListener() {
32+
@Override
33+
public boolean onTouch(View v, MotionEvent event) {
34+
boardMove(event.getX(), event.getY());
35+
return false;
36+
}
37+
});
2938
Thread t=new Thread(this);
3039
t.start();
3140
}
41+
public void boardMove(float x, float y){
42+
int row=(int)Math.floor(y/BoardCell.size);
43+
int col=(int)Math.floor(x/BoardCell.size);
44+
Log.d("Click at :",x+", "+y+", row:"+row+", col:"+col);
45+
Log.i("PIECE:","the piece in the current cell:" + (this.b.getCell(row,col).getPiece()==null));
46+
this.b.psuedoClickListener(row,col);
47+
}
3248
public void drawBoard(){
3349
if(holder.getSurface().isValid()){
3450
Canvas canvas = holder.lockCanvas();
@@ -43,7 +59,7 @@ public void run() {
4359
while (true){
4460
drawBoard();
4561
try {
46-
Thread.sleep(2000);
62+
Thread.sleep(1000/30);
4763
} catch (InterruptedException e) { e.printStackTrace();}
4864
}
4965
}

0 commit comments

Comments
 (0)