toHighlight method OK but not optimal
This commit is contained in:
parent
2fdb6a4fc9
commit
1d59c578db
|
|
@ -1,6 +1,7 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.lang.Math;
|
||||
|
||||
public class Board {
|
||||
private int colNum;
|
||||
|
|
@ -32,7 +33,7 @@ public class Board {
|
|||
return turnNumber;//returns the turn number
|
||||
}
|
||||
|
||||
public boolean isTurnWhite() {
|
||||
public boolean isTurnWhite() { //check if it is the white player's turn
|
||||
boolean turnWhite = false;
|
||||
if (this.turnNumber % 2 == 0) {
|
||||
turnWhite = true;
|
||||
|
|
@ -88,6 +89,7 @@ public class Board {
|
|||
}
|
||||
|
||||
public void cleanBoard() {
|
||||
turnNumber = 0;
|
||||
pieces.clear();
|
||||
}
|
||||
|
||||
|
|
@ -127,9 +129,9 @@ public class Board {
|
|||
pieceHere =true;
|
||||
}
|
||||
else {
|
||||
index += 1;
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
|
|
@ -187,9 +189,233 @@ public class Board {
|
|||
|
||||
/* The following methods require more work ! */
|
||||
|
||||
public boolean isHighlighted(int x, int y) {
|
||||
//TODO
|
||||
return false;
|
||||
// COUCOU LES COPAINS SI VOUS ARRIVEZ A OPTIMISER LA SUITE CA SERAIT COOL
|
||||
public ArrayList<ArrayList<Integer>> highlightBishop(){ //creates a table of the x and y coordinates to highlight for the bishop
|
||||
boolean spotOccupied = false;
|
||||
ArrayList<ArrayList<Integer>> toHighlight = new ArrayList<> ();
|
||||
toHighlight.add(new ArrayList<>()); // ArrayList that will contain the x coordinates
|
||||
toHighlight.add(new ArrayList<>()); // ArrayList that will contain the y coordinates
|
||||
int i = 0;
|
||||
while (spotOccupied == false) { //left top diagonal
|
||||
if ((x-i-1 >= 0) && (y-i-1 >= 0)) { //check if we are still in the board
|
||||
toHighlight.get(0).add(this.x-i-1);
|
||||
toHighlight.get(1).add(this.y-i-1);
|
||||
if (positionOccupied(this.x-i-1,this.y-i-1)) { //if we reach a position already occupied, stops highlighting the diagonal
|
||||
spotOccupied = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
spotOccupied = true;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
spotOccupied = false;
|
||||
i = 0;
|
||||
while (spotOccupied == false) { //left bottom diagonal
|
||||
if ((x-i-1 >= 0) && (y+1+i <= 7)) {
|
||||
toHighlight.get(0).add(this.x-i-1);
|
||||
toHighlight.get(1).add(this.y+i+1);
|
||||
if (positionOccupied(this.x-i-1,this.y+i+1)) {
|
||||
spotOccupied = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
spotOccupied = true;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
spotOccupied = false;
|
||||
i = 0;
|
||||
while (spotOccupied == false) { //right bottom diagonal
|
||||
if ((x+1+i <= 7) && (y+1+i <= 7)) {
|
||||
toHighlight.get(0).add(this.x+i+1);
|
||||
toHighlight.get(1).add(this.y+i+1);
|
||||
if (positionOccupied(this.x+i+1,this.y+i+1)) {
|
||||
spotOccupied = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
spotOccupied = true;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
spotOccupied = false;
|
||||
i = 0;
|
||||
while (spotOccupied == false) { //right top diagonal
|
||||
if ((y-i-1 >= 0) && (x+1+i <= 7)) {
|
||||
toHighlight.get(0).add(this.x+i+1);
|
||||
toHighlight.get(1).add(this.y-i-1);
|
||||
if (positionOccupied(this.x+i+1,this.y-i-1)) {
|
||||
spotOccupied = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
spotOccupied = true;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
return toHighlight;
|
||||
}
|
||||
|
||||
public ArrayList<ArrayList<Integer>> highlightQueen(){ //list of positions to highlight for the queens
|
||||
ArrayList<ArrayList<Integer>> toHighlight = highlightBishop();
|
||||
ArrayList<ArrayList<Integer>> toHighlight2 = highlightRook();
|
||||
toHighlight.get(0).addAll(toHighlight2.get(0)); //combine the x coordinates
|
||||
toHighlight.get(1).addAll(toHighlight2.get(1)); //combine the y coordinates
|
||||
return toHighlight;
|
||||
}
|
||||
|
||||
public ArrayList<ArrayList<Integer>> highlightRook(){ //list of positions to highlight for the rooks
|
||||
boolean spotOccupied = false;
|
||||
ArrayList<ArrayList<Integer>> toHighlight = new ArrayList<> ();
|
||||
toHighlight.add(new ArrayList<>());
|
||||
toHighlight.add(new ArrayList<>());
|
||||
int i = 0;
|
||||
while (spotOccupied == false) { //horizontal top line
|
||||
if (y-i-1 >= 0) { // check if we are still in the board (works the same as the bishop)
|
||||
toHighlight.get(0).add(this.x);
|
||||
toHighlight.get(1).add(this.y-i-1);
|
||||
if (positionOccupied(this.x,this.y-i-1)) {
|
||||
spotOccupied = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
spotOccupied = true;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
spotOccupied = false;
|
||||
i = 0;
|
||||
while (spotOccupied == false) { //horizontal bottom line
|
||||
if (y+i+1 <= 7) {
|
||||
toHighlight.get(0).add(this.x);
|
||||
toHighlight.get(1).add(this.y+i+1);
|
||||
if (positionOccupied(this.x,this.y+i+1)) {
|
||||
spotOccupied = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
spotOccupied = true;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
spotOccupied = false;
|
||||
i = 0;
|
||||
while (spotOccupied == false) { //vertical left line
|
||||
if (x-i-1 >= 0) {
|
||||
toHighlight.get(0).add(this.x-i-1);
|
||||
toHighlight.get(1).add(this.y);
|
||||
if (positionOccupied(this.x-i-1,this.y)) {
|
||||
spotOccupied = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
spotOccupied = true;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
spotOccupied = false;
|
||||
i = 0;
|
||||
while (spotOccupied == false) { //vertical right line
|
||||
if (x+i+1 <= 7) {
|
||||
toHighlight.get(0).add(this.x+i+1);
|
||||
toHighlight.get(1).add(this.y);
|
||||
if (positionOccupied(this.x+i+1,this.y)) {
|
||||
spotOccupied = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
spotOccupied = true;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
return toHighlight;
|
||||
}
|
||||
|
||||
|
||||
public boolean isHighlighted(int x, int y) { //not at all optimized, if you have ideas go ahead
|
||||
boolean highlight = false;
|
||||
if (positionOccupied(this.x,this.y)) { //check if we have selected a position with a piece
|
||||
int indexPieceSelect = whatPiece(this.x,this.y);
|
||||
if (pieces.get(indexPieceSelect).getType() == PieceType.Pawn) {//highlight for pawns
|
||||
|
||||
if (pieces.get(indexPieceSelect).isWhite()) {//white pawns
|
||||
if(y == this.y-1) {
|
||||
if((x == this.x) && (positionOccupied(x,y)==false)) {//advance one case
|
||||
highlight = true;
|
||||
}
|
||||
if((Math.abs(x-this.x) == 1) && (positionOccupied(x,y))) {//eat the piece in diagonal, if existing
|
||||
highlight = true;
|
||||
}
|
||||
}
|
||||
if((y == this.y-2) && (this.y == 6) && (x == this.x)) {//move by two if not moved
|
||||
highlight = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (pieces.get(indexPieceSelect).isWhite() == false) {//black pawns
|
||||
if(y == this.y+1) {
|
||||
if((x == this.x) && (positionOccupied(x,y)==false)) {
|
||||
highlight = true;
|
||||
}
|
||||
if((Math.abs(x-this.x) == 1) && (positionOccupied(x,y))) {
|
||||
highlight = true;
|
||||
}
|
||||
}
|
||||
if((y == this.y+2) && (this.y == 1) && (x == this.x)) {
|
||||
highlight = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pieces.get(indexPieceSelect).getType() == PieceType.King) { //highlight for Kings
|
||||
if((Math.abs(x-this.x) <= 1) && (Math.abs(y-this.y) <= 1)) { //consider all cases at distance one from the king
|
||||
if ((Math.abs(x-this.x) != 0) || (Math.abs(y-this.y) != 0)) { //check if we are not considering the place where the king is
|
||||
highlight = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pieces.get(indexPieceSelect).getType() == PieceType.Knight) { //highlight for knights
|
||||
if (((Math.abs(x-this.x) == 1) && (Math.abs(y-this.y) == 2)) || ((Math.abs(x-this.x) == 2) && (Math.abs(y-this.y) == 1))) {//consider all positions at 2 by 1 from the knight
|
||||
highlight = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (pieces.get(indexPieceSelect).getType() == PieceType.Bishop) { //highlight for bishops
|
||||
int numHighlight = highlightBishop().get(0).size();
|
||||
for(int i = 0; i < numHighlight; i++) {
|
||||
if ((x == highlightBishop().get(0).get(i)) && (y == highlightBishop().get(1).get(i))) {//check if the considered x and y are in the list of positions that should be highlighted
|
||||
highlight = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pieces.get(indexPieceSelect).getType() == PieceType.Queen) { //highlight for Queens
|
||||
int numHighlight = highlightQueen().get(0).size();
|
||||
for(int i = 0; i < numHighlight; i++) {
|
||||
if ((x == highlightQueen().get(0).get(i)) && (y == highlightQueen().get(1).get(i))) {//check if the considered x and y are in the list of positions that should be highlighted
|
||||
highlight = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pieces.get(indexPieceSelect).getType() == PieceType.Rook) { //highlight for rooks
|
||||
int numHighlight = highlightRook().get(0).size();
|
||||
for(int i = 0; i < numHighlight; i++) {
|
||||
if ((x == highlightRook().get(0).get(i)) && (y == highlightRook().get(1).get(i))) {//check if the considered x and y are in the list of positions that should be highlighted
|
||||
highlight = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return highlight;
|
||||
}
|
||||
|
||||
public void undoLastMove() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue