diff --git a/OOP_2B1_Project/src/backend/Board.java b/OOP_2B1_Project/src/backend/Board.java index ea69b72..4a20097 100644 --- a/OOP_2B1_Project/src/backend/Board.java +++ b/OOP_2B1_Project/src/backend/Board.java @@ -1,26 +1,27 @@ package backend; import java.util.ArrayList; +import java.util.LinkedList; public class Board { - - -private int Width; -private int Height; -private ArrayList pieces; -private int turnNumber = 0; + private int Width; + private int Height; + private ArrayList pieces; -private boolean turnIsWhite = true; -private Integer selectedX = null; + private int turnNumber = 0; -private Integer selectedY = null; + private boolean turnIsWhite = true; -private ArrayList highlightedPositions = new ArrayList<>(); + private Integer selectedX = null; + + private Integer selectedY = null; + + private ArrayList highlightedPositions = new ArrayList<>(); @@ -28,9 +29,9 @@ private ArrayList highlightedPositions = new ArrayList<>(); this.Width=colNum; this.Height=lineNum; this.pieces= new ArrayList <> (); - - - + + + } public int getWidth() { @@ -42,21 +43,21 @@ private ArrayList highlightedPositions = new ArrayList<>(); } public int getTurnNumber() { - + return turnNumber; } public boolean isTurnWhite() { - + return turnIsWhite; } public void setPiece(boolean isWhite, PieceType type, int x, int y) { pieces.add(new Piece(isWhite, type, x, y)); - - - + + + } public void populateBoard() { @@ -82,58 +83,59 @@ private ArrayList highlightedPositions = new ArrayList<>(); for (int i=0;i<8;i++) { setPiece(false,PieceType.Pawn,i,6); } - - + + } - + public void cleanBoard() { - pieces.clear(); - selectedX = null; - selectedY = null; + pieces.clear(); + selectedX = null; + selectedY = null; } - - - + + + public ArrayList getPieces() { - return pieces; + return pieces; } - + public String toString() { StringBuilder sb = new StringBuilder(); - for (int y = 0; y < Height; y++) { - for (int x = 0; x < Width; x++) { - Piece pieceAtPos = null; - for (Piece p : pieces) { - if (p.getX() == x && p.getY() == y) { - pieceAtPos = p; - break; - } - } + for (int y = 0; y < Height; y++) { + for (int x = 0; x < Width; x++) { + Piece pieceAtPos = null; + for (Piece p : pieces) { + if (p.getX() == x && p.getY() == y) { + pieceAtPos = p; + break; + } + } - if (pieceAtPos != null) { - char colorChar = pieceAtPos.isWhite() ? 'W' : 'B'; - String typeChar = pieceAtPos.getType().getSummary(); - sb.append(colorChar).append(typeChar); - } else { - sb.append(", "); - } + if (pieceAtPos != null) { + char colorChar = pieceAtPos.isWhite() ? 'W' : 'B'; + String typeChar = pieceAtPos.getType().getSummary(); + sb.append(colorChar).append(typeChar); + } else { + sb.append(", "); + } - sb.append(" "); - } - sb.append("\n"); - } + sb.append(" "); + } + sb.append("\n"); + } - sb.append("Turn: ").append(turnIsWhite ? "White" : "Black"); - sb.append(" (Turn number: ").append(turnNumber).append(")\n"); + sb.append("Turn: ").append(turnIsWhite ? "White" : "Black"); + sb.append(" (Turn number: ").append(turnNumber).append(")\n"); + + return sb.toString(); - return sb.toString(); - } - + public void userTouch(int x, int y) { +<<<<<<< HEAD if (selectedX == null && selectedY == null) { @@ -183,49 +185,109 @@ private ArrayList highlightedPositions = new ArrayList<>(); selectedY = null; } } +======= + + if (selectedX == null && selectedY == null) { + + + Piece pieceAtPos = getPieceAt(x, y); + + if (pieceAtPos != null) { + + selectedX = x; + selectedY = y; + } + } else { + + if (selectedX == x && selectedY == y) { + + selectedX = null; + selectedY = null; + } else { + + Piece pieceToMove = getPieceAt(selectedX, selectedY); + + if (pieceToMove != null) { + + pieces.remove(pieceToMove); + pieces.add(new Piece(pieceToMove.isWhite(), pieceToMove.getType(), x, y)); + + + turnNumber++; + turnIsWhite = !turnIsWhite; + + } + + selectedX = null; + selectedY = null; + } + } +>>>>>>> branch 'master' of https://gitarero.ecam.fr/yohan.montagne/OOP_2B1_Project.git } - + private Piece getPieceAt(int x, int y) { - for (Piece piece : pieces) { - if (piece.getX() == x && piece.getY() == y) { - return piece; - } - } - return null; + for (Piece piece : pieces) { + if (piece.getX() == x && piece.getY() == y) { + return piece; + } + } + return null; } public boolean isSelected(int x, int y) { - return selectedX != null && selectedY != null && selectedX == x && selectedY == y; + //TODO + return false; } - - + /* saving-loading feature :*/ public String[] toFileRep() { - String[] lines = new String[Height + 1]; - for (int y = 0; y < Height; y++) { - StringBuilder row = new StringBuilder(); - for (int x = 0; x < Width; x++) { - Piece p = getPieceAt(x, y); - if (p != null) { - row.append (p.isWhite() ? "W" : "B").append(p.getType().getSummary()); - } - row.append(x < Width - 1? "," : ""); - } - lines[y] = row.toString(); - } - lines[Height] = isTurnWhite() ? "W" : "B"; - return lines; + String[] lines = new String[Height + 1]; + for (int y = 0; y < Height; y++) { + StringBuilder row = new StringBuilder(); + for (int x = 0; x < Width; x++) { + Piece p = getPieceAt(x, y); + if (p != null) { + row.append (p.isWhite() ? "W" : "B").append(p.getType().getSummary()); + } + row.append(x < Width - 1? "," : ""); + } + lines[y] = row.toString(); + } + lines[Height] = isTurnWhite() ? "W" : "B"; + return lines; } public Board(String[] array) { - //TODO - + this.Height=array.length-1; + this.pieces= new ArrayList <> (); + for (int y=0;y highlightedPositions = new ArrayList<>(); return false; } + + private LinkedList moveHistory = new LinkedList<>(); + public void undoLastMove() { //TODO - + } public Board(Board board) { //TODO } - + public void playMove(Move move) { //TODO diff --git a/OOP_2B1_Project/testboard b/OOP_2B1_Project/testboard new file mode 100644 index 0000000..940541a --- /dev/null +++ b/OOP_2B1_Project/testboard @@ -0,0 +1,9 @@ +WR,WN,WB,WQ,WK,WB,WN,WR +WP,WP,WP,WP,WP,WP,WP,WP +,,,,,,, +,,BK,,,,, +,,,,,,, +,,,,,,, +BP,BP,BP,BP,,BP,BP,BP +BR,BN,BB,,BQ,BB,BN,BR +W