diff --git a/src/Main.java b/src/Main.java index 4f0590a..0a9dc74 100644 --- a/src/Main.java +++ b/src/Main.java @@ -37,7 +37,6 @@ public class Main { testBoard.setPiece(true, PieceType.Bishop, 4, 7); testBoard.toString(); - // launches graphical interface : MyInterface mjf = new MyInterface(); mjf.setVisible(true); diff --git a/src/backend/Board.java b/src/backend/Board.java index 8977016..c0aaf4e 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -6,6 +6,9 @@ public class Board { private int width; private int height; private Piece[][] board;//creating an array for the coordinates of the board + private Piece chosenPiece=null; + private int turnNumber=0; + private boolean isTurnWhite=true; public Board(int colNum, int lineNum) { this.width=colNum; @@ -14,29 +17,23 @@ public class Board { } public int getWidth() { - //TODO return width; } public int getHeight() { - //TODO return height; } public int getTurnNumber() { - //TODO - return 0; + return turnNumber; } public boolean isTurnWhite() { - //TODO - return false; + return isTurnWhite; } public void setPiece(boolean isWhite, PieceType type, int x, int y) { board[y][x]= new Piece (x, y, type, isWhite); - - } public void populateBoard() { @@ -62,23 +59,28 @@ public class Board { for(int x=0; x<8; x++) { board[x][y]= null; } - } + } } - public String toString() { - - + public String toString() { //review the stringbuilder class that already exists in java aand see if we can replace it by smth else + StringBuilder builder= new StringBuilder(); for (int y=0; y< height; y++) { - for (int x=0; x getPieces() { ArrayList pieces = new ArrayList<>(); for(int y=0; y<8; y++) { @@ -92,13 +94,30 @@ public class Board { } public void userTouch(int x, int y) { - //TODO + Piece clickedPiece=board[x][y]; + if(chosenPiece==null) { + //when no piece is selected + if(clickedPiece!=null) { + chosenPiece=clickedPiece; + } + } + else { //if a piece is already selected + if(isSelected(x,y)) { + chosenPiece=null; //unselect the chosen piece to revert to normal state + } + else { //move selected piece to new position + board[chosenPiece.getX()][chosenPiece.getY()]=null; //clear the old position + board[x][y]=new Piece(x, y, chosenPiece.getType(), chosenPiece.isWhite());//place at new position + chosenPiece=null; //unselect the piece moved + turnNumber++; //tracks the number of moves + isTurnWhite=!isTurnWhite; + } + } } - public boolean isSelected(int x, int y) { - //TODO - return false; + public boolean isSelected(int x, int y) { //checks if any piece is selected and if its position matches that of the x,y coordinates + return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y; } /* saving-loading feature :*/