Change of the toString to get a better overview

Add of two methods
- one to find the index in the array list in function of the position
- another is to verify if a square is occupied

Repaired the user Touch, we can now move the pieces
This commit is contained in:
edwin 2025-04-18 17:31:06 +02:00
parent 1b2c24f007
commit f6a2129ee0
2 changed files with 68 additions and 28 deletions

View File

@ -5,13 +5,16 @@ import java.util.ArrayList;
public class Board {
private int colNum;
private int lineNum;
private ArrayList<Piece> pieces = new ArrayList<>();
private int x = -1;
private int y = -1;
private ArrayList<Piece> 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<Piece> 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) {

View File

@ -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;
}
}