pretty cool
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
package chess.model;
|
||||
|
||||
import chess.model.pieces.Bishop;
|
||||
import chess.model.pieces.King;
|
||||
import chess.model.pieces.Knight;
|
||||
import chess.model.pieces.Pawn;
|
||||
import chess.model.pieces.Queen;
|
||||
import chess.model.pieces.Rook;
|
||||
import chess.model.visitor.KingIdentifier;
|
||||
import chess.model.visitor.PiecePathChecker;
|
||||
|
||||
@@ -30,6 +36,7 @@ public class ChessBoard {
|
||||
this.cells[i][j] = new Cell();
|
||||
}
|
||||
}
|
||||
initPieces();
|
||||
}
|
||||
|
||||
public void applyMove(Move move) {
|
||||
@@ -41,6 +48,7 @@ public class ChessBoard {
|
||||
Piece movingPiece = pieceAt(move.getStart());
|
||||
pieceComes(movingPiece, move.getFinish());
|
||||
pieceLeaves(move.getStart());
|
||||
movingPiece.move();
|
||||
}
|
||||
|
||||
public void undoMove(Move move) {
|
||||
@@ -52,7 +60,8 @@ public class ChessBoard {
|
||||
}
|
||||
|
||||
public Piece pieceAt(Coordinate coordinate) {
|
||||
assert (coordinate.isValid());
|
||||
if (!coordinate.isValid())
|
||||
return null;
|
||||
return cellAt(coordinate).getPiece();
|
||||
}
|
||||
|
||||
@@ -103,10 +112,56 @@ public class ChessBoard {
|
||||
continue;
|
||||
|
||||
PiecePathChecker checker = new PiecePathChecker(this, new Move(attackCoords, kingPos));
|
||||
if (checker.isValidForPiece(attackPiece))
|
||||
if (checker.isValid())
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean[][] getAllowedMoves(Coordinate pieceCoords) {
|
||||
Piece piece = pieceAt(pieceCoords);
|
||||
if (piece == null)
|
||||
return null;
|
||||
|
||||
boolean[][] result = new boolean[Coordinate.VALUE_MAX][Coordinate.VALUE_MAX];
|
||||
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||
PiecePathChecker piecePathChecker = new PiecePathChecker(this, new Move(pieceCoords, new Coordinate(i, j)));
|
||||
result[i][j] = piecePathChecker.isValid();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void initPieces() {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
pieceComes(new Pawn(Color.Black), new Coordinate(i, 0));
|
||||
pieceComes(new Pawn(Color.White), new Coordinate(i, Coordinate.VALUE_MAX - 1));
|
||||
}
|
||||
|
||||
pieceComes(new Rook(Color.Black), new Coordinate(0, 1));
|
||||
pieceComes(new Rook(Color.Black), new Coordinate(Coordinate.VALUE_MAX - 1, 1));
|
||||
|
||||
pieceComes(new Rook(Color.White), new Coordinate(0, Coordinate.VALUE_MAX - 2));
|
||||
pieceComes(new Rook(Color.White), new Coordinate(Coordinate.VALUE_MAX - 1, Coordinate.VALUE_MAX - 2));
|
||||
|
||||
pieceComes(new Knight(Color.Black), new Coordinate(1, 1));
|
||||
pieceComes(new Knight(Color.Black), new Coordinate(Coordinate.VALUE_MAX - 2, 1));
|
||||
|
||||
pieceComes(new Knight(Color.White), new Coordinate(1, Coordinate.VALUE_MAX - 2));
|
||||
pieceComes(new Knight(Color.White), new Coordinate(Coordinate.VALUE_MAX - 2, Coordinate.VALUE_MAX - 2));
|
||||
|
||||
pieceComes(new Bishop(Color.Black), new Coordinate(2, 1));
|
||||
pieceComes(new Bishop(Color.Black), new Coordinate(Coordinate.VALUE_MAX - 3, 1));
|
||||
|
||||
pieceComes(new Bishop(Color.White), new Coordinate(2, Coordinate.VALUE_MAX - 2));
|
||||
pieceComes(new Bishop(Color.White), new Coordinate(Coordinate.VALUE_MAX - 3, Coordinate.VALUE_MAX - 2));
|
||||
|
||||
pieceComes(new Queen(Color.Black), new Coordinate(4, 1));
|
||||
pieceComes(new King(Color.Black), new Coordinate(3, 1));
|
||||
|
||||
pieceComes(new Queen(Color.White), new Coordinate(4, Coordinate.VALUE_MAX - 2));
|
||||
pieceComes(new King(Color.White), new Coordinate(3, Coordinate.VALUE_MAX - 2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ package chess.model;
|
||||
public enum Direction {
|
||||
|
||||
Unset(65),
|
||||
Front(8), Back(-8), Left(-1), Right(1),
|
||||
FrontLeft(7), FrontRight(9), BackLeft(-9), BackRight(-7);
|
||||
Front(-8), Back(8), Left(-1), Right(1),
|
||||
FrontLeft(-9), FrontRight(-7), BackLeft(7), BackRight(9);
|
||||
|
||||
private final int indexOffset;
|
||||
|
||||
@@ -16,15 +16,23 @@ public enum Direction {
|
||||
return indexOffset;
|
||||
}
|
||||
|
||||
public static Direction fromInt(int direction) {
|
||||
for (Direction dir : Direction.values()) {
|
||||
if (dir.getIndexOffset() == direction)
|
||||
return dir;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Direction findDirection(Move move) {
|
||||
assert move.isValid() : "Move is invalid!";
|
||||
int diffX = move.getFinish().getX() - move.getStart().getX();
|
||||
int diffY = move.getFinish().getY() - move.getStart().getY();
|
||||
|
||||
if (diffX == 0 && diffY < 0)
|
||||
return Direction.Back;
|
||||
if (diffX == 0 && diffY > 0)
|
||||
return Direction.Front;
|
||||
if (diffX == 0 && diffY > 0)
|
||||
return Direction.Back;
|
||||
|
||||
if (diffX < 0 && diffY == 0)
|
||||
return Direction.Left;
|
||||
@@ -32,14 +40,14 @@ public enum Direction {
|
||||
return Direction.Right;
|
||||
|
||||
if (diffX < 0 && -diffX == diffY)
|
||||
return Direction.FrontLeft;
|
||||
return Direction.BackLeft;
|
||||
if (diffX > 0 && diffX == diffY)
|
||||
return Direction.FrontRight;
|
||||
return Direction.BackRight;
|
||||
|
||||
if (diffY < 0 && diffX == diffY)
|
||||
return Direction.BackLeft;
|
||||
if (diffY > 0 && diffX == -diffY)
|
||||
return Direction.BackRight;
|
||||
return Direction.FrontLeft;
|
||||
if (diffX > 0 && diffX == -diffY)
|
||||
return Direction.FrontRight;
|
||||
|
||||
return Direction.Unset;
|
||||
}
|
||||
|
||||
31
app/src/main/java/chess/model/Game.java
Normal file
31
app/src/main/java/chess/model/Game.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package chess.model;
|
||||
|
||||
import chess.model.visitor.PiecePathChecker;
|
||||
import common.Signal0;
|
||||
import common.Signal1;
|
||||
|
||||
public class Game {
|
||||
private final ChessBoard board;
|
||||
|
||||
public final Signal0 OnRenderUpdate = new Signal0();
|
||||
public final Signal1<Move> OnMoveRefused = new Signal1<>();
|
||||
|
||||
public Game(ChessBoard board) {
|
||||
this.board = board;
|
||||
}
|
||||
|
||||
public ChessBoard getBoard() {
|
||||
return board;
|
||||
}
|
||||
|
||||
public void tryMove(Move move) {
|
||||
boolean valid = new PiecePathChecker(board, move).isValid();
|
||||
if (!valid) {
|
||||
this.OnMoveRefused.emit(move);
|
||||
return;
|
||||
}
|
||||
this.board.applyMove(move);
|
||||
this.OnRenderUpdate.emit();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -75,19 +75,19 @@ public class PermissiveRuleChecker implements PieceVisitor<Boolean> {
|
||||
int distance = this.move.traversedCells();
|
||||
|
||||
// Revoke moving backwards
|
||||
if (directionIndexOffset * pawn.multiplier() < 0)
|
||||
if (directionIndexOffset * pawn.multiplier() > 0)
|
||||
return false;
|
||||
|
||||
// Allowing straight moves
|
||||
if (Math.abs(directionIndexOffset) == Direction.Front.getIndexOffset()) {
|
||||
if (Math.abs(directionIndexOffset) == Math.abs(Direction.Front.getIndexOffset())) {
|
||||
if (pawn.hasMoved())
|
||||
return distance == 1;
|
||||
return distance == 1 || distance == 2;
|
||||
}
|
||||
|
||||
// Allowing small diagonal moves
|
||||
if (Math.abs(directionIndexOffset) == Direction.FrontLeft.getIndexOffset()
|
||||
|| Math.abs(directionIndexOffset) == Direction.FrontRight.getIndexOffset()) {
|
||||
if (directionIndexOffset == Direction.FrontLeft.getIndexOffset()
|
||||
|| directionIndexOffset == Direction.FrontRight.getIndexOffset()) {
|
||||
return distance == 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,12 @@ public class PiecePathChecker implements PieceVisitor<Boolean> {
|
||||
this.board = board;
|
||||
}
|
||||
|
||||
public boolean isValidForPiece(Piece piece) {
|
||||
public boolean isValid() {
|
||||
if (this.move.getStart().equals(this.move.getFinish()))
|
||||
return false;
|
||||
Piece piece = this.board.pieceAt(move.getStart());
|
||||
if (piece == null)
|
||||
return false;
|
||||
return visit(piece);
|
||||
}
|
||||
|
||||
@@ -62,18 +67,16 @@ public class PiecePathChecker implements PieceVisitor<Boolean> {
|
||||
if (!new PermissiveRuleChecker(this.move).isValidFor(pawn))
|
||||
return false;
|
||||
|
||||
Direction moveDirection = Direction.findDirection(this.move);
|
||||
// ...
|
||||
// moveDirection = Directions(int(findDirection(move)) * pawn.multiplier())
|
||||
Direction moveDirection = Direction.fromInt(Direction.findDirection(move).getIndexOffset() * pawn.multiplier());
|
||||
|
||||
if (moveDirection == Direction.Front)
|
||||
return testPath(pawn.getColor());
|
||||
return testPath(pawn.getColor()) && this.board.pieceAt(this.move.getFinish()) == null;
|
||||
|
||||
assert moveDirection == Direction.FrontLeft || moveDirection == Direction.FrontRight;
|
||||
|
||||
Piece destPiece = this.board.pieceAt(this.move.getFinish());
|
||||
if (destPiece == null)
|
||||
return true;
|
||||
return false;
|
||||
|
||||
return destPiece.getColor() != pawn.getColor();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user