diff --git a/src/backend/Board.java b/src/backend/Board.java index 24c32b0..299b285 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -11,6 +11,15 @@ public class Board { private int y; private int turns; private boolean whiteTurn; + private int[][] knightMoves = {{1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}}; + private int[][] kingMoves = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; + private int[][] whitePawn = {{6}, {0, -1}, {1, -1}, {-1, -1}, {0, -2}}; + private int[][] blackPawn = {{1}, {0, 1}, {1, 1}, {-1, 1}, {0, 2}}; + private boolean enPassant; + private int enPassantX; + private int enPassantY; + private int pawnX; + private int pawnY; public Board(int colNum, int lineNum) { this.colNum = colNum; @@ -20,6 +29,9 @@ public class Board { y = -1; turns = 0; whiteTurn = true; + enPassant = false; + pawnX = -1; + pawnY = -1; } public int getWidth() { @@ -39,51 +51,35 @@ public class Board { } public void setPiece(boolean isWhite, PieceType type, int x, int y) { - Piece piece = new Piece(isWhite,type,x,y); - board[y][x] = piece; + board[y][x] = new Piece(isWhite, type, x, y); } public void populateBoard() { for (int i = 0; i < lineNum; i++) { - - boolean white = true; - if (i == 0 || i == 1) { - white = false; + boolean white = (i > 5); + if (i == 1 || i == 6) { + for (int j = 0; j < colNum; j++) { + setPiece(white, PieceType.Pawn, j, i); + } } - - for (int j = 0; j < colNum; j++) { - if (i == 1 || i == 6) { - Piece pawn = new Piece(white, PieceType.Pawn, j, i); - board[i][j] = pawn; - } - else if (i == 0 || i == 7) { - if (j == 0 || j == 7) { - Piece rook = new Piece(white, PieceType.Rook, j, i); - board[i][j] = rook; - } - else if (j == 1 || j == 6) { - Piece knight = new Piece(white, PieceType.Knight, j, i); - board[i][j] = knight; - } - else if (j == 2 || j == 5) { - Piece bishop = new Piece(white, PieceType.Bishop, j, i); - board[i][j] = bishop; - } - else if (j == 3) { - Piece queen = new Piece(white, PieceType.Queen, j, i); - board[i][j] = queen; - } - else if (j == 4) { - Piece king = new Piece(white, PieceType.King, j, i); - board[i][j] = king; - } - } - + else if (i == 0 || i == 7){ + setPiece(white, PieceType.Rook, 0, i); + setPiece(white, PieceType.Rook, 7, i); + setPiece(white, PieceType.Knight, 1, i); + setPiece(white, PieceType.Knight, 6, i); + setPiece(white, PieceType.Bishop, 2, i); + setPiece(white, PieceType.Bishop, 5, i); + setPiece(white, PieceType.Queen, 3, i); + setPiece(white, PieceType.King, 4, i); } } } public void cleanBoard() { + x = -1; + y = -1; + turns = 0; + whiteTurn = true; for (int i = 0; i < lineNum; i++) { for (int j = 0; j < colNum; j++) { board[i][j] = null; @@ -95,19 +91,16 @@ public class Board { String str = ""; for (int i = 0; i < lineNum; i++) { for (int j = 0; j < colNum; j++) { - if (board[i][j] == null) { str += " "; } - else if (board[i][j].isWhite() == true) { - str += "W"; - str += board[i][j].getType().getSummary(); + else if (board[i][j].isWhite()) { + str += "W" + board[i][j].getType().getSummary(); } else if (board[i][j].isWhite() == false) { - str += "B"; - str += board[i][j].getType().getSummary(); + str += "B" + board[i][j].getType().getSummary(); } - if (j != 7) { + if (j != colNum-1) { str += ","; } } @@ -118,11 +111,10 @@ public class Board { public ArrayList getPieces() { ArrayList pieces = new ArrayList<>(); - for (int col = 0; col < colNum; col ++) { - for (int row = 0; row < lineNum; row ++) { - Piece Piece = board[row][col]; - if (Piece != null) { - pieces.add(Piece); + for (int i = 0; i < colNum; i++) { + for (int j = 0; j < lineNum; j++) { + if (board[j][i] != null) { + pieces.add(board[j][i]); } } } @@ -130,383 +122,146 @@ public class Board { } public void userTouch(int x, int y) { - boolean prevSelect = true; if (this.x == -1 && this.y == -1) { - prevSelect = false; - } - - if (prevSelect == true) { - if (this.x == x && this.y == y) { - this.x = -1; - this.y = -1; - } - else if (board[this.y][this.x] == null) { - this.x = -1; - this.y = -1; - } - else { - board[this.y][this.x].setX(x); - board[this.y][this.x].setY(y); - board[y][x] = board[this.y][this.x]; - board[this.y][this.x] = null; - turns += 1; - this.x = -1; - this.y = -1; - if (whiteTurn == true) { - whiteTurn = false; - } - else { - whiteTurn = true; - } + if (board[y][x] != null && board[y][x].isWhite() == whiteTurn) { + this.x = x; + this.y = y; } } else { - this.x = x; - this.y = y; + if (this.x == x && this.y == y || board[this.y][this.x] == null) { + this.x = -1; + this.y = -1; + } + else if (isHighlighted(x,y)) { + if (enPassant == true && board[this.y][this.x].getType() == PieceType.Pawn && x == enPassantX && y == enPassantY) { + board[this.y][this.x].setX(x); + board[this.y][this.x].setY(y); + board[y][x] = board[this.y][this.x]; + board[this.y][this.x] = null; + board[pawnY][pawnX] = null; + enPassant = false; + pawnX = -1; + pawnY = -1; + } + else { + if (board[this.y][this.x].getType() == PieceType.Pawn && (y == this.y+2 || y == this.y-2)) { + enPassant = true; + pawnX = x; + pawnY = y; + } + board[this.y][this.x].setX(x); + board[this.y][this.x].setY(y); + board[y][x] = board[this.y][this.x]; + board[this.y][this.x] = null; + } + turns += 1; + this.x = -1; + this.y = -1; + whiteTurn = !whiteTurn; + } } } + + public boolean inBoard(int x, int y) { + if (x > -1 && x < colNum && y > -1 && y < lineNum) { + return true; + } + return false; + } + + public void linearMoves(ArrayList moves, int x, int y, int x2, int y2, boolean white) { + int i = x + x2; + int j = y + y2; + boolean metPiece = false; + while (metPiece == false && i > -1 && inBoard(i,j)) { + if (board[j][i] != null) { + metPiece = true; + if (board[j][i].isWhite() != white) { + moves.add(new int[]{i, j}); + } + } + else { + moves.add(new int[]{i, j}); + } + i += x2; + j += y2; + } + } public ArrayList getMove(PieceType type, boolean isWhite, int x, int y) { ArrayList moves = new ArrayList<>(); - int i = x; - int j = y; - boolean metPiece; + int i; + int j; + int[][] pieceMoves; - if (type == PieceType.Rook) { - metPiece = false; - i = x; - j = y; - while (metPiece == false && i+1 < 8) { - if (board[j][i+1] != null) { - moves.add(new int[] {i+1, j}); - metPiece = true; - } - else { - i += 1; - moves.add(new int[] {i, j}); - } - } - metPiece = false; - i = x; - j = y; - while (metPiece == false && j+1 < 8) { - if (board[j+1][i] != null) { - moves.add(new int[] {i, j+1}); - metPiece = true; - } - else { - j += 1; - moves.add(new int[] {i, j}); - } - } - metPiece = false; - i = x; - j = y; - while (metPiece == false && i-1 > -1) { - if (board[j][i-1] != null) { - moves.add(new int[] {i-1, j}); - metPiece = true; - } - else { - i -= 1; - moves.add(new int[] {i, j}); - } - } - metPiece = false; - i = x; - j = y; - while (metPiece == false && j-1 > -1) { - if (board[j-1][i] != null) { - moves.add(new int[] {i, j-1}); - metPiece = true; - } - else { - j -= 1; - moves.add(new int[] {i, j}); - } - } + if (type == PieceType.Rook || type == PieceType.Queen) { + linearMoves(moves, x, y, 1, 0, isWhite); + linearMoves(moves, x, y, -1, 0, isWhite); + linearMoves(moves, x, y, 0, -1, isWhite); + linearMoves(moves, x, y, 0, 1, isWhite); } - else if (type == PieceType.Knight) { - if (x+1 < 8) { - if (y+2 < 8) { - moves.add(new int[] {x+1, y+2}); - } - if (y-2 > -1) { - moves.add(new int[] {x+1, y-2}); - } - } - if (x+2 < 8) { - if (y+1 < 8) { - moves.add(new int[] {x+2, y+1}); - } - if (y-1 > -1) { - moves.add(new int[] {x+2, y-1}); - } - } - if (x-1 > -1) { - if (y+2 < 8) { - moves.add(new int[] {x-1, y+2}); - } - if (y-2 > -1) { - moves.add(new int[] {x-1, y-2}); - } - } - if (x-2 > -1) { - if (y+1 < 8) { - moves.add(new int[] {x-2, y+1}); - } - if (y-1 > -1) { - moves.add(new int[] {x-2, y-1}); - } - } + if (type == PieceType.Bishop || type == PieceType.Queen) { + linearMoves(moves, x, y, 1, 1, isWhite); + linearMoves(moves, x, y, -1, 1, isWhite); + linearMoves(moves, x, y, 1, -1, isWhite); + linearMoves(moves, x, y, -1, -1, isWhite); } - else if (type == PieceType.Bishop) { - metPiece = false; - i = x; - j = y; - while (metPiece == false && i+1 < 8 && j+1 < 8) { - if (board[j+1][i+1] != null) { - moves.add(new int[] {i+1, j+1}); - metPiece = true; - } - else { - i += 1; - j += 1; - moves.add(new int[] {i, j}); - } - } - metPiece = false; - i = x; - j = y; - while (metPiece == false && i-1 > -1 && j+1 < 8) { - if (board[j+1][i-1] != null) { - moves.add(new int[] {i-1, j+1}); - metPiece = true; - } - else { - i -= 1; - j += 1; - moves.add(new int[] {i, j}); - } - } - metPiece = false; - i = x; - j = y; - while (metPiece == false && i+1 < 8 && j-1 > -1) { - if (board[j-1][i+1] != null) { - moves.add(new int[] {i+1, j-1}); - metPiece = true; - } - else { - i += 1; - j -= 1; - moves.add(new int[] {i, j}); - } - } - metPiece = false; - i = x; - j = y; - while (metPiece == false && i-1 > -1 && j-1 > -1) { - if (board[j-1][i-1] != null) { - moves.add(new int[] {i-1, j-1}); - metPiece = true; - } - else { - i -= 1; - j -= 1; - moves.add(new int[] {i, j}); - } - } - } - - else if (type == PieceType.Queen) { - metPiece = false; - i = x; - j = y; - while (metPiece == false && i+1 < 8) { - if (board[j][i+1] != null) { - moves.add(new int[] {i+1, j}); - metPiece = true; - } - else { - i += 1; - moves.add(new int[] {i, j}); - } - } - metPiece = false; - i = x; - j = y; - while (metPiece == false && j+1 < 8) { - if (board[j+1][i] != null) { - moves.add(new int[] {i, j+1}); - metPiece = true; - } - else { - j += 1; - moves.add(new int[] {i, j}); - } - } - metPiece = false; - i = x; - j = y; - while (metPiece == false && i-1 > -1) { - if (board[j][i-1] != null) { - moves.add(new int[] {i-1, j}); - metPiece = true; - } - else { - i -= 1; - moves.add(new int[] {i, j}); - } - } - metPiece = false; - i = x; - j = y; - while (metPiece == false && j-1 > -1) { - if (board[j-1][i] != null) { - moves.add(new int[] {i, j-1}); - metPiece = true; - } - else { - j -= 1; - moves.add(new int[] {i, j}); - } - } - metPiece = false; - i = x; - j = y; - while (metPiece == false && i+1 < 8 && j+1 < 8) { - if (board[j+1][i+1] != null) { - moves.add(new int[] {i+1, j+1}); - metPiece = true; - } - else { - i += 1; - j += 1; - moves.add(new int[] {i, j}); - } - } - metPiece = false; - i = x; - j = y; - while (metPiece == false && i-1 > -1 && j+1 < 8) { - if (board[j+1][i-1] != null) { - moves.add(new int[] {i-1, j+1}); - metPiece = true; - } - else { - i -= 1; - j += 1; - moves.add(new int[] {i, j}); - } - } - metPiece = false; - i = x; - j = y; - while (metPiece == false && i+1 < 8 && j-1 > -1) { - if (board[j-1][i+1] != null) { - moves.add(new int[] {i+1, j-1}); - metPiece = true; - } - else { - i += 1; - j -= 1; - moves.add(new int[] {i, j}); - } - } - metPiece = false; - i = x; - j = y; - while (metPiece == false && i-1 > -1 && j-1 > -1) { - if (board[j-1][i-1] != null) { - moves.add(new int[] {i-1, j-1}); - metPiece = true; - } - else { - i -= 1; - j -= 1; - moves.add(new int[] {i, j}); - } - } - } - - else if (type == PieceType.King) { - if (x+1 < 8) { - moves.add(new int[] {x+1, y}); - if (y+1 < 8) { - moves.add(new int[] {x+1, y+1}); - } - if (y-1 > -1) { - moves.add(new int[] {x+1, y-1}); - } - } - if (x-1 > -1) { - moves.add(new int[] {x-1, y}); - if (y+1 < 8) { - moves.add(new int[] {x-1, y+1}); - } - if (y-1 > -1) { - moves.add(new int[] {x-1, y-1}); - } - } - if (y+1 < 8) { - moves.add(new int[] {x, y+1}); - } - if (y-1 > -1) { - moves.add(new int[] {x, y-1}); - } - } - else { - if (isWhite == false) { - if (y == 1) { - if (board[j+1][i] == null) { - moves.add(new int[] {i, j+1}); - if (board[j+2][i] == null) { - moves.add(new int[] {i, j+2}); - } - } - } - else { - if (board[j+1][i] == null) { - moves.add(new int[] {i, j+1}); - } - } - if (x != 7) { - if (board[j+1][i+1] != null) { - moves.add(new int[] {i+1, j+1}); - } - } - if (x != 0) { - if (board[j+1][i-1] != null) { - moves.add(new int[] {i-1, j+1}); - } - } + if (type == PieceType.Knight || type == PieceType.King) { + + if (type == PieceType.Knight) { + pieceMoves = knightMoves; } else { - if (y == 6) { - if (board[j-1][i] == null) { - moves.add(new int[] {i, j-1}); - if (board[j-2][i] == null) { - moves.add(new int[] {i, j-2}); - } + pieceMoves = kingMoves; + } + for (int n = 0; n < pieceMoves.length; n++) { + i = x + pieceMoves[n][0]; + j = y + pieceMoves[n][1]; + if (inBoard(i,j)) { + if (board[j][i] != null && board[j][i].isWhite() != isWhite) { + moves.add(new int[] {i, j}); + } + else if (board[j][i] == null){ + moves.add(new int[] {i, j}); } } - else { - if (board[j-1][i] == null) { - moves.add(new int[] {i, j-1}); + } + } + + if (type == PieceType.Pawn) { + if (isWhite == false) { + pieceMoves = blackPawn; + } + else { + pieceMoves = whitePawn; + } + + i = x + pieceMoves[1][0]; + j = y + pieceMoves[1][1]; + if (inBoard(i,j) && board[j][i] == null) { + moves.add(new int[] {i, j}); + if (y == pieceMoves[0][0]) { + i = x + pieceMoves[4][0]; + j = y + pieceMoves[4][1]; + if (inBoard(i,j) && board[j][i] == null) { + moves.add(new int[] {i, j}); } } - if (x != 7) { - if (board[j-1][i+1] != null) { - moves.add(new int[] {i+1, j-1}); - } + } + + for (int n = 2; n < 4; n++) { + i = x + pieceMoves[n][0]; + j = y + pieceMoves[n][1]; + if (inBoard(i,j) && board[j][i] != null && board[j][i].isWhite() != isWhite) { + moves.add(new int[] {i, j}); } - if (x != 0) { - if (board[j-1][i-1] != null) { - moves.add(new int[] {i-1, j-1}); - } + else if(inBoard(i,j) && board[j][i] == null && enPassant == true && i == pawnX && y == pawnY) { + moves.add(new int[] {i, j}); + enPassantX = i; + enPassantY = j; } } } @@ -514,16 +269,8 @@ public class Board { } public boolean isSelected(int x, int y) { - if (this.x != -1 && this.y != -1) { - if (this.x == x && this.y == y) { - return true; - } - - else if (board[this.y][this.x] == null) { - this.x = -1; - this.y = -1; - return false; - } + if (this.x == x && this.y == y) { + return true; } return false; } @@ -531,12 +278,11 @@ public class Board { /* saving-loading feature :*/ public String[] toFileRep() { - String myFile = this.toString(); String turn = "B"; - if (this.isTurnWhite() == true) { + if (isTurnWhite()) { turn = "W"; } - myFile += turn; + String myFile = toString() + turn; String[] myArray = myFile.split("\n"); return myArray; } @@ -549,23 +295,23 @@ public class Board { this.y = -1; this.turns = 0; this.whiteTurn = true; + this.enPassant = false; String[] line; boolean white; - char character; - PieceType type; - for (int i = 0; i < 8; i++) { + for (int i = 0; i < colNum; i++) { line = array[i].split(","); - for (int j = 0; j < 8; j++) { - if (!line[j].equals(" ")) { - white = line[j].charAt(0) == 'W'; - character = line[j].charAt(1); - type = PieceType.fromSummary(character); - setPiece(white, type, j, i); + for (int j = 0; j < lineNum; j++) { + if (line[j] != " ") { + white = false; + if (line[j].charAt(0) == 'W') { + white = true; + } + setPiece(white, PieceType.fromSummary(line[j].charAt(1)), j, i); } } } - if (array[8] == "B") { + if (array[lineNum] == "B") { whiteTurn = false; } } @@ -575,10 +321,9 @@ public class Board { public boolean isHighlighted(int x, int y) { if (this.x != -1 && this.y != -1) { - ArrayList moves = this.getMove(board[this.y][this.x].getType(), board[this.y][this.x].isWhite(), this.x, this.y); + ArrayList moves = getMove(board[this.y][this.x].getType(), board[this.y][this.x].isWhite(), this.x, this.y); for (int i = 0; i < moves.size(); i++) { - int[] coordinates = moves.get(i); - if (x == coordinates[0] && y == coordinates[1]) { + if (x == moves.get(i)[0] && y == moves.get(i)[1]) { return true; } }