Added a constraibnt to only allow to move to empty or case with a black
piece
This commit is contained in:
parent
4b55e20327
commit
73d71f2568
|
|
@ -1,27 +1,26 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Board {
|
||||
|
||||
|
||||
|
||||
|
||||
private int Width;
|
||||
private int Height;
|
||||
private ArrayList <Piece> pieces;
|
||||
|
||||
|
||||
private int Width;
|
||||
private int Height;
|
||||
private ArrayList <Piece> pieces;
|
||||
private int turnNumber = 0;
|
||||
|
||||
private boolean turnIsWhite = true;
|
||||
|
||||
private int turnNumber = 0;
|
||||
private Integer selectedX = null;
|
||||
|
||||
private boolean turnIsWhite = true;
|
||||
private Integer selectedY = null;
|
||||
|
||||
private Integer selectedX = null;
|
||||
|
||||
private Integer selectedY = null;
|
||||
|
||||
private ArrayList<int[]> highlightedPositions = new ArrayList<>();
|
||||
private ArrayList<int[]> highlightedPositions = new ArrayList<>();
|
||||
|
||||
|
||||
|
||||
|
|
@ -29,9 +28,9 @@ public class Board {
|
|||
this.Width=colNum;
|
||||
this.Height=lineNum;
|
||||
this.pieces= new ArrayList <> ();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
|
|
@ -43,21 +42,21 @@ public class Board {
|
|||
}
|
||||
|
||||
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() {
|
||||
|
|
@ -83,77 +82,76 @@ public class Board {
|
|||
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<Piece> 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");
|
||||
|
||||
return sb.toString();
|
||||
sb.append("Turn: ").append(turnIsWhite ? "White" : "Black");
|
||||
sb.append(" (Turn number: ").append(turnNumber).append(")\n");
|
||||
|
||||
return sb.toString();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void userTouch(int x, int y) {
|
||||
<<<<<<< HEAD
|
||||
|
||||
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) {
|
||||
|
|
@ -164,7 +162,7 @@ public class Board {
|
|||
if (pieceAtDestination == null ||
|
||||
pieceAtDestination.isWhite() != pieceToMove.isWhite()) {
|
||||
|
||||
|
||||
|
||||
if (pieceAtDestination != null) {
|
||||
pieces.remove(pieceAtDestination);
|
||||
}
|
||||
|
|
@ -173,121 +171,65 @@ public class Board {
|
|||
pieces.remove(pieceToMove);
|
||||
pieces.add(new Piece(pieceToMove.isWhite(), pieceToMove.getType(), x, y));
|
||||
|
||||
|
||||
|
||||
turnNumber++;
|
||||
turnIsWhite = !turnIsWhite;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
selectedX = null;
|
||||
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) {
|
||||
//TODO
|
||||
return false;
|
||||
return selectedX != null && selectedY != null && selectedX == x && selectedY == y;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* 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) {
|
||||
this.Height=array.length-1;
|
||||
this.pieces= new ArrayList <> ();
|
||||
for (int y=0;y<Height;y++) {
|
||||
String line = array[y];
|
||||
String[] decomposedLine = line.split(",");
|
||||
this.Width=decomposedLine.length;
|
||||
for (int x=0;x<Width;x++) {
|
||||
String pieceStr = decomposedLine[x];
|
||||
if(!pieceStr.equals("")) {
|
||||
boolean isWhite = true;
|
||||
if(pieceStr.charAt(0)=='B') {
|
||||
isWhite = false;
|
||||
}
|
||||
PieceType myType = PieceType.fromSummary(pieceStr.charAt(1));
|
||||
setPiece(isWhite, myType, x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
String turnIndicator = array[Height];
|
||||
if(turnIndicator.equals("W")) {
|
||||
this.turnIsWhite = true;
|
||||
} else {
|
||||
turnIsWhite = false;
|
||||
}
|
||||
//TODO
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* The following methods require more work ! */
|
||||
|
||||
public boolean isHighlighted(int x, int y) {
|
||||
|
|
@ -295,19 +237,16 @@ public class Board {
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
private LinkedList<Move> moveHistory = new LinkedList<>();
|
||||
|
||||
public void undoLastMove() {
|
||||
//TODO
|
||||
|
||||
|
||||
}
|
||||
|
||||
public Board(Board board) {
|
||||
//TODO
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void playMove(Move move) {
|
||||
//TODO
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue