diff --git a/src/backend/Board.java b/src/backend/Board.java index f257810..d2aaed7 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -5,13 +5,16 @@ import java.util.ArrayList; public class Board { private int colNum; private int lineNum; - private ArrayList pieces = new ArrayList<>(); - private int x = -1; - private int y = -1; + private ArrayList pieces; + private int x; + private int y ; public Board(int colNum, int lineNum) { this.colNum = colNum; this.lineNum = lineNum; + pieces = new ArrayList<>(); + x = -1; + y = -1; } public int getWidth() { @@ -84,46 +87,72 @@ public class Board { } public String toString() { - String state = pieces.toString(); - return state; + String outputString = ""; + for(int i=0; i<8; i++ ) { + for(int j=0; j<8; j++) { + if (this.positionOccupied(j,i)==true){ + outputString += this.pieces.get(this.whatPiece(j, i)).getSummaryColor() + this.pieces.get(this.whatPiece(j, i)).getType().getSummary() +","; + } + else { + outputString += " ,"; + } + + } + outputString += "\n"; + } + return outputString; + } + + public boolean positionOccupied(int x, int y) { // method which verifies if there is a piece at a given location + int sizePieces = pieces.size(); + boolean positionOccupied = false; + for(int j = 0;j < sizePieces; j++) { + if ((x == pieces.get(j).getX()) && (y == pieces.get(j).getY())){ + positionOccupied = true; + } + } + return positionOccupied; + } + + public int whatPiece(int x, int y) { // method which gives the index (in the pieces array) of the piece at a position x,y + boolean pieceHere = false; + int index = 0; + while (pieceHere == false) { + if((x == pieces.get(index).getX()) && (y == pieces.get(index).getY())){ + pieceHere =true; + } + else { + index += 1; + } + } + return index; } public ArrayList getPieces() { return pieces; } - public void userTouch(int x, int y) { //NOT WORKING - int sizePieces = pieces.size(); - boolean positionOccupied = false; - for(int j = 1;j <= sizePieces; j++) { - if ((x == pieces.get(j).getX()) && (y == pieces.get(j).getY())){ - positionOccupied = true; - } - } - if ((this.x == -1 ) && (positionOccupied == true)){ + public void userTouch(int x, int y) { + boolean positionOccupied = this.positionOccupied(x, y) ; // verify if the square is occupied + // + if ((this.x == -1 ) && (positionOccupied == true)){ // set coordinates of the piece to play this.x = x; this.y = y; } else { - if((this.x == x) && (this.y == y)) { + if((this.x == x) && (this.y == y)) { // reset since square is already selected this.x = -1; this.y = -1; } - else { - boolean foundPiece = false; - int k = 1; - while (foundPiece == false ) { - if ((this.x == pieces.get(k).getX()) && (this.y == pieces.get(k).getY())){ - foundPiece = true; - pieces.get(k).setX(x); - pieces.get(k).setY(y); - } - k += 1; - } - + else { // we know we already have a piece so we move it to another position + int indexPiece = this.whatPiece(this.x, this.y); + pieces.get(indexPiece).setX(x); + pieces.get(indexPiece).setY(y); + // we now reset our parameters + this.x=-1; + this.y=-1; } } - } public boolean isSelected(int x, int y) { diff --git a/src/backend/Piece.java b/src/backend/Piece.java index fa970a6..81e4e93 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -36,4 +36,15 @@ public class Piece { return isWhite; } + public String getSummaryColor() { + String color = ""; + if (this.isWhite == true){ + color = "W"; + } + else { + color = "B"; + } + return color; + } + }