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:
parent
1b2c24f007
commit
f6a2129ee0
|
|
@ -5,13 +5,16 @@ import java.util.ArrayList;
|
||||||
public class Board {
|
public class Board {
|
||||||
private int colNum;
|
private int colNum;
|
||||||
private int lineNum;
|
private int lineNum;
|
||||||
private ArrayList<Piece> pieces = new ArrayList<>();
|
private ArrayList<Piece> pieces;
|
||||||
private int x = -1;
|
private int x;
|
||||||
private int y = -1;
|
private int y ;
|
||||||
|
|
||||||
public Board(int colNum, int lineNum) {
|
public Board(int colNum, int lineNum) {
|
||||||
this.colNum = colNum;
|
this.colNum = colNum;
|
||||||
this.lineNum = lineNum;
|
this.lineNum = lineNum;
|
||||||
|
pieces = new ArrayList<>();
|
||||||
|
x = -1;
|
||||||
|
y = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
|
|
@ -84,46 +87,72 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String state = pieces.toString();
|
String outputString = "";
|
||||||
return state;
|
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() {
|
public ArrayList<Piece> getPieces() {
|
||||||
return pieces;
|
return pieces;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void userTouch(int x, int y) { //NOT WORKING
|
public void userTouch(int x, int y) {
|
||||||
int sizePieces = pieces.size();
|
boolean positionOccupied = this.positionOccupied(x, y) ; // verify if the square is occupied
|
||||||
boolean positionOccupied = false;
|
//
|
||||||
for(int j = 1;j <= sizePieces; j++) {
|
if ((this.x == -1 ) && (positionOccupied == true)){ // set coordinates of the piece to play
|
||||||
if ((x == pieces.get(j).getX()) && (y == pieces.get(j).getY())){
|
|
||||||
positionOccupied = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ((this.x == -1 ) && (positionOccupied == true)){
|
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
}
|
}
|
||||||
else {
|
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.x = -1;
|
||||||
this.y = -1;
|
this.y = -1;
|
||||||
}
|
}
|
||||||
else {
|
else { // we know we already have a piece so we move it to another position
|
||||||
boolean foundPiece = false;
|
int indexPiece = this.whatPiece(this.x, this.y);
|
||||||
int k = 1;
|
pieces.get(indexPiece).setX(x);
|
||||||
while (foundPiece == false ) {
|
pieces.get(indexPiece).setY(y);
|
||||||
if ((this.x == pieces.get(k).getX()) && (this.y == pieces.get(k).getY())){
|
// we now reset our parameters
|
||||||
foundPiece = true;
|
this.x=-1;
|
||||||
pieces.get(k).setX(x);
|
this.y=-1;
|
||||||
pieces.get(k).setY(y);
|
|
||||||
}
|
|
||||||
k += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSelected(int x, int y) {
|
public boolean isSelected(int x, int y) {
|
||||||
|
|
|
||||||
|
|
@ -36,4 +36,15 @@ public class Piece {
|
||||||
return isWhite;
|
return isWhite;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSummaryColor() {
|
||||||
|
String color = "";
|
||||||
|
if (this.isWhite == true){
|
||||||
|
color = "W";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
color = "B";
|
||||||
|
}
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue