retry
This commit is contained in:
parent
5f8450adc0
commit
b359744200
|
|
@ -34,7 +34,7 @@ public class Board {
|
||||||
return isWhiteTurn;
|
return isWhiteTurn;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||||
for (int i = 0; i < pieces.size(); i++) {
|
for (int i = 0; i < pieces.size(); i++) {
|
||||||
Piece p = pieces.get(i);
|
Piece p = pieces.get(i);
|
||||||
if (p.getX() == x && p.getY() == y) {
|
if (p.getX() == x && p.getY() == y) {
|
||||||
|
|
@ -43,7 +43,7 @@ public class Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Piece newPiece = new Piece( x, y, type,isWhite);
|
Piece newPiece = new Piece( x, y, type,isWhite);
|
||||||
pieces.add(newPiece);
|
pieces.add(newPiece);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void populateBoard() {
|
public void populateBoard() {
|
||||||
|
|
@ -92,36 +92,69 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String result = "";
|
//TODO
|
||||||
for (int y = 0; y < height; y++) {
|
return "";
|
||||||
for (int x = 0; x < width; x++) {
|
}
|
||||||
char c = ' ';
|
|
||||||
for (Piece p : pieces) {
|
public ArrayList<Piece> getPieces() {
|
||||||
if (p.getX() == x && p.getY() == y) {
|
ArrayList<Piece> result = new ArrayList<>();
|
||||||
if(p.isWhite() == true) {
|
for (Piece p : this.pieces) {
|
||||||
c = 'W';
|
result.add(p);
|
||||||
String letter =p.getType().name();
|
|
||||||
c += letter.charAt(0);
|
|
||||||
}
|
|
||||||
if(p.isWhite() == false) {
|
|
||||||
c = 'B';
|
|
||||||
String letter =p.getType().name();
|
|
||||||
c += letter.charAt(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result += c + " ";
|
|
||||||
}
|
|
||||||
result += "\n";
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
private Piece getPieceAt(int x, int y) {
|
||||||
|
for (Piece p : pieces) {
|
||||||
|
if (p.getX() == x && p.getY() == y) {
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Integer selectedX = null;
|
||||||
|
private Integer selectedY = null;
|
||||||
|
|
||||||
public void userTouch(int x, int y) {
|
public void userTouch(int x, int y) {
|
||||||
|
Piece clickedPiece = getPieceAt(x, y);
|
||||||
|
|
||||||
|
if (selectedX == null || selectedY == null) {
|
||||||
|
// No piece selected yet
|
||||||
|
if (clickedPiece != null && clickedPiece.isWhite() == isWhiteTurn) {
|
||||||
|
selectedX = x;
|
||||||
|
selectedY = y;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (selectedX == x && selectedY == y) {
|
||||||
|
// Unselection
|
||||||
|
selectedX = null;
|
||||||
|
selectedY = null;
|
||||||
|
} else {
|
||||||
|
// Attempt to move
|
||||||
|
Piece selectedPiece = getPieceAt(selectedX, selectedY);
|
||||||
|
if (selectedPiece != null) {
|
||||||
|
// Remove captured piece (if any)
|
||||||
|
Piece toRemove = getPieceAt(x, y);
|
||||||
|
if (toRemove != null && toRemove.isWhite() != isWhiteTurn) {
|
||||||
|
pieces.remove(toRemove);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move piece
|
||||||
|
selectedPiece.moveTo(x, y);
|
||||||
|
|
||||||
|
// Advance turn
|
||||||
|
turnNumber++;
|
||||||
|
isWhiteTurn = !isWhiteTurn;
|
||||||
|
|
||||||
|
// Unselect
|
||||||
|
selectedX = null;
|
||||||
|
selectedY = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean isSelected(int x, int y) {
|
public boolean isSelected(int x, int y) {
|
||||||
//TODO
|
//TODO
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -28,4 +28,9 @@ public class Piece {
|
||||||
public boolean isWhite() {
|
public boolean isWhite() {
|
||||||
return isWhite; // Returns true if the piece is white, false if black
|
return isWhite; // Returns true if the piece is white, false if black
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void moveTo(int x, int y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue