diff --git a/OOP_2B1_Project/src/backend/Board.java b/OOP_2B1_Project/src/backend/Board.java index 1bf00ca..80eb9ac 100644 --- a/OOP_2B1_Project/src/backend/Board.java +++ b/OOP_2B1_Project/src/backend/Board.java @@ -6,7 +6,11 @@ public class Board { public int width; public int height; public ArrayList> board = new ArrayList<>(); - private int selectPosition = 0; + public boolean select = false; + public int xm; + public int ym; + public int turnNumber; + public boolean turnColor; public Board(int colNum, int lineNum) { this.width = colNum; this.height = lineNum; @@ -19,6 +23,8 @@ public class Board { } this.board.add(row); } + this.turnNumber = 0; + this.turnColor = true; } @@ -31,13 +37,11 @@ public class Board { } public int getTurnNumber() { - //TODO - return 0; + return this.turnNumber; } public boolean isTurnWhite() { - //TODO - return false; + return this.turnColor; } public void setPiece(int x, int y, PieceType type, boolean isWhite) { @@ -76,7 +80,16 @@ public class Board { } public void cleanBoard() { - //TODO + int rows = 8; + int cols = 8; + for (int i = 0; i < rows; i++) { + ArrayList row = new ArrayList<>(); + for (int j = 0; j < cols; j++) { + row.add(null); // Fill with null + } + this.board.add(row); + } + } @Override @@ -97,24 +110,37 @@ public class Board { } public Piece getPiece(int x, int y) { - return board.get(x).get(y); + return board.get(y).get(x); + } + + public void movePiece(int x, int y) { + Piece pieceToMove = this.board.get(this.ym).get(this.xm); + this.setPiece(x,y,pieceToMove.getType(),pieceToMove.isWhite()); + board.get(this.ym).set(this.xm,null); } public void userTouch(int x, int y) { - Piece pieceToMove = new Piece(); - while (this.selectPosition < 2) { - if (this.selectPosition == 0) { - pieceToMove = this.getPiece(y,x); - } else if (this.selectPosition == 1) { - this.setPiece(x, y, pieceToMove.getType(), pieceToMove.isWhite()); + if (this.select == false && board.get(y).get(x) != null) { + this.xm = x; + this.ym = y; + select = true; + } + else if (select == true && this.xm != x || this.ym != y){ + this.movePiece(x, y); + select = false; + this.turnNumber += 1; + // System.out.println(this.toString()); // Debug + this.turnColor = !this.turnColor; + } else { + select = false; } - } - } public boolean isSelected(int x, int y) { - //TODO - return false; + boolean S; + if (this.xm == x && this.ym == y) {S = true;} + else {S = false;} + return S; } /* saving-loading feature :*/ diff --git a/OOP_2B1_Project/src/backend/Move.java b/OOP_2B1_Project/src/backend/Move.java index 7b9ff9b..8a74ca0 100644 --- a/OOP_2B1_Project/src/backend/Move.java +++ b/OOP_2B1_Project/src/backend/Move.java @@ -1,5 +1,5 @@ package backend; public class Move { - + } diff --git a/OOP_2B1_Project/src/backend/Piece.java b/OOP_2B1_Project/src/backend/Piece.java index 25ceece..08d5da1 100644 --- a/OOP_2B1_Project/src/backend/Piece.java +++ b/OOP_2B1_Project/src/backend/Piece.java @@ -5,8 +5,7 @@ public class Piece { public int y; public PieceType type; public boolean isWhite; - public Piece() { - } + public Piece(int x, int y, PieceType type, boolean isWhite) { this.x = x; this.y = y;