Skip to content

Commit

Permalink
fixed a major bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Natanel Rudyuklakir committed Jan 1, 2023
1 parent 56496d5 commit 2a9b18c
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 53 deletions.
7 changes: 0 additions & 7 deletions app/src/main/java/com/example/chessfirebase/GameAvtivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ protected void onCreate(Bundle savedInstanceState) {
e.printStackTrace();
}
cgv.start(p.getBoard());
cgv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
p.getBoard().psuedoClickListener(event.getX(),event.getY());
return false;
}
});
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public Bishop(int row,int col,boolean isWhite) {
//calculateing the moves using bfs

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

Expand Down
53 changes: 26 additions & 27 deletions app/src/main/java/com/example/chessfirebase/chessClasses/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@


import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

import com.example.chessfirebase.socketPlayer;

import java.util.List;

public class Board{//add the tap listener later in android studio
//variables
private BoardCell[][] board;
private King wk;
private King bk;
public Piece remove;
public boolean isFirstClick;
public boolean isFirstClick=true;
int[] from;
private socketPlayer player;
private Piece currSelectedPiece;

public Board(boolean isWhite,socketPlayer p){
this.player=p;
from = new int[2];
board=new BoardCell[8][8];
for(int i=0;i<board.length;i++){
for(int j=0;j<board.length;j++){
Expand Down Expand Up @@ -148,27 +150,28 @@ public Board(boolean isWhite,socketPlayer p){
}
}

public void psuedoClickListener(float rawX,float rawY){
int row=Math.round(rawY%BoardCell.size);
int col=Math.round(rawX%BoardCell.size);
Piece curr=this.getCell(row, col).getPiece();
public void psuedoClickListener(int row,int col){
// if(!player.ourTurn)return; TODO:uncomment for release
if(isFirstClick){
if(curr!=null && curr.isWhite==player.isWhite){
from=curr.getRawPos();
currSelectedPiece=board[row][col].getPiece();
if(currSelectedPiece!=null && currSelectedPiece.isWhite==player.isWhite){
from=currSelectedPiece.getRawPos();
isFirstClick=!isFirstClick;
curr.drawValidMoves();
currSelectedPiece.drawValidMoves();
}
}else{
row=Math.round(rawY%BoardCell.size);
col=Math.round(rawX%BoardCell.size);
for(int[] mov: curr.getMoves()){
}else if(currSelectedPiece!=null){
List<int[]> lst=currSelectedPiece.getMoves();
isFirstClick=!isFirstClick;
for(int[] mov: lst){
if(mov[0]==row && mov[1]==col){
player.move(from, new int[]{row,col});
isFirstClick=!isFirstClick;
this.move(from , mov);
player.toggleOurTurn();
this.playerMove(from , mov);
player.ch.move=from[0]+","+from[1]+" "+mov[0]+","+mov[1];
player.calculateMoves();
break;
}
}
currSelectedPiece=null;
}
}

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

public void playerMove(int[] from,int[] to){
Piece p=this.getCell(to[0], to[1]).getPiece();
if(p!=null){
player.remove(p);
this.board[to[0]][to[1]].setPiece(this.board[from[0]][from[1]].getPiece());
this.board[from[0]][from[1]].setPiece(null);
this.board[to[0]][to[1]].getPiece().move(to[0],to[1]);
}else{
this.board[to[0]][to[1]].setPiece(this.board[from[0]][from[1]].getPiece());
this.board[from[0]][from[1]].setPiece(null);
this.board[to[0]][to[1]].getPiece().move(to[0],to[1]);
}
if(p!=null && p.isWhite!=player.isWhite) player.remove(p);

this.board[to[0]][to[1]].setPiece(this.board[from[0]][from[1]].getPiece());
this.board[from[0]][from[1]].setPiece(null);
this.board[to[0]][to[1]].getPiece().move(to[0],to[1]);
Log.d("NEWSQUARE", "piece "+(board[to[0]][to[1]].getPiece()==null));
}

public void draw(Canvas canvas){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public King(int row,int col,boolean isWhite){
}

public void calculateMoves(Board b){
this.possibleMoves.clear();
int[][] dir={{-1,-1},{-1,1},{1,-1},{1,1},{1,0},{0,1},{-1,0},{0,-1}};
for(int[] d:dir){
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)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public Knight(int row, int col,boolean isWhite){
}

public void calculateMoves(Board b){
this.possibleMoves.clear();
int[][] moves={{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{-1,2},{1,-2},{-1,-2}};//the possible 8 moves.
for(int[] mov:moves){
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)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public Pawn(int row, int col, boolean isWhite) {

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


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

validateMoves(b);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,24 @@ public void validateMoves(Board b){
//checking for pinned pieces
List<int[]> moves=this.possibleMoves;
List<int[]> remove=new ArrayList<>();
int[] currPos;
int[] currPos=new int[]{this.row,this.col};
Log.i("posOfPiece",currPos[0]+", "+currPos[1]);
//check what moves to remove:
for(int i=0;i<moves.size();i++){
//move the piece without drawing
//move the piece
int[] mov=moves.get(i);
currPos=this.getRawPos();
Piece p=b.getCell(mov[0], mov[1]).getPiece();
//maybe add an edge case of same colored pieces but probably handled by cacl moves
b.move(currPos, mov);
if(b.isCheck(this.isWhite)){
remove.add(mov);
}
//move the piece back
b.move(mov,currPos);
if(mov[0]==currPos[0] && mov[1]==currPos[1]){
Log.d("MOVE","SAME_MOVE_ERROR");
Log.d("THE MOVE",mov[0]+","+mov[1]);
}
b.move(mov,currPos);//the problematic func
if(p!=null){
b.getCell(mov[0], mov[1]).setPiece(p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public Queen(int row,int col,boolean isWhite){
}
//TODO: implement movement.
public void calculateMoves(Board b){
this.possibleMoves.clear();
int[][] dir={{-1,-1},{-1,1},{1,-1},{-1,-1},{1,0},{0,1},{-1,0},{0,-1}};
for(int[] d:dir){
for(int r=this.row,c=this.col;r>=0 && c>=0 && r<8 && c<8;r+=d[0],c+=d[1]){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public Rook(int row, int col, boolean isWhite) {
//methods

public void calculateMoves(Board b){
this.possibleMoves.clear();

int[][] dir={{1,0},{0,1},{-1,0},{0,-1}};
//down right up left
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/java/com/example/chessfirebase/customGameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.example.chessfirebase.chessClasses.Board;
import com.example.chessfirebase.chessClasses.BoardCell;
Expand All @@ -26,9 +28,23 @@ public customGameView(Context context, float width, float height) {
}
public void start(Board b){
this.b=b;
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
boardMove(event.getX(), event.getY());
return false;
}
});
Thread t=new Thread(this);
t.start();
}
public void boardMove(float x, float y){
int row=(int)Math.floor(y/BoardCell.size);
int col=(int)Math.floor(x/BoardCell.size);
Log.d("Click at :",x+", "+y+", row:"+row+", col:"+col);
Log.i("PIECE:","the piece in the current cell:" + (this.b.getCell(row,col).getPiece()==null));
this.b.psuedoClickListener(row,col);
}
public void drawBoard(){
if(holder.getSurface().isValid()){
Canvas canvas = holder.lockCanvas();
Expand All @@ -43,7 +59,7 @@ public void run() {
while (true){
drawBoard();
try {
Thread.sleep(2000);
Thread.sleep(1000/30);
} catch (InterruptedException e) { e.printStackTrace();}
}
}
Expand Down
11 changes: 2 additions & 9 deletions app/src/main/java/com/example/chessfirebase/socketPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,9 @@ public void calculateMoves(){
}
}

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];
}
}
public void toggleOurTurn(){
this.ourTurn=!this.ourTurn;
}

//after we recieve the move:
public int situationCheck(){
//return 0 if nothing, 1 if check 2 if mate 3 if draw.
Expand Down

0 comments on commit 2a9b18c

Please sign in to comment.