Compare commits

...

2 Commits

Author SHA1 Message Date
Stanisław Jarocki
c01c236c94 refactor: add indents 2023-06-20 18:33:30 +02:00
Stanisław Jarocki
c19b9a5a0a refactor: refactor and remove unnecessary code 2023-06-20 17:48:03 +02:00
14 changed files with 188 additions and 296 deletions

View File

@ -8,7 +8,11 @@
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Board/Board.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Board/Board.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Game.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Game.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece/Bishop.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece/Bishop.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece/King.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece/King.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece/Knight.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece/Knight.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece/Pawn.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece/Pawn.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece/Queen.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece/Queen.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece/Rook.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece/Rook.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Test.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Test.java" afterDir="false" />
</list>
@ -55,14 +59,17 @@
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;last_opened_file_path&quot;: &quot;/home/stanlee77/code/chessEngine/src/com/jarocki/stanislaw/chess/Piece&quot;,
&quot;settings.editor.selected.configurable&quot;: &quot;reference.settings.project.maven.runner&quot;
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"last_opened_file_path": "/home/stanlee77/code/stanchezz",
"project.structure.last.edited": "Project",
"project.structure.proportion": "0.0",
"project.structure.side.proportion": "0.0",
"settings.editor.selected.configurable": "editor.preferences.fonts.default"
}
}</component>
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece" />
@ -119,6 +126,10 @@
</pinned-members>
</pin-to-top-manager>
</component>
<component name="XSLT-Support.FileAssociations.UIState">
<expand />
<select />
</component>
<component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/chessEngine$Run_Main.ic" NAME="Run Main Coverage Results" MODIFIED="1687198501398" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="idea" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" />
</component>

View File

@ -5,8 +5,6 @@ import com.jarocki.stanislaw.chess.Coordinate.Row;
import com.jarocki.stanislaw.chess.Piece.*;
import com.jarocki.stanislaw.chess.Piece.Color;
import java.awt.*;
public class Board {
private final Basic[][] fields;
private boolean hasWhiteKingMoved;
@ -48,16 +46,6 @@ public class Board {
fields[Row.EIGHT.getNum()][Column.F.ordinal()] = new Bishop(Color.BLACK, Row.EIGHT, Column.F);
}
public void movePiece(Column currentColumn, Row currentRow, Column targetColumn, Row targetRow) {
int currRow = currentRow.ordinal();
int currCol = currentColumn.ordinal();
int targRow = targetRow.ordinal();
int targCol = targetColumn.ordinal();
Basic piece = fields[currRow][currCol];
fields[currRow][currCol] = null;
fields[targRow][targCol] = piece;
}
public void printBoard() {
System.out.println();
@ -116,17 +104,13 @@ public class Board {
// remove en passant captured pawn from board
if (piece.getSymbol().equals(Symbol.PAWN)) {
Pawn pawn = new Pawn(piece.getColor(), piece.getRow(), piece.getColumn());
if (Math.abs(fromRow - toRow) == 1 && Math.abs(fromCol - toCol) == 1 && fields[toRow][toCol] == null) {
int capturedPawnRow = fromRow;
int capturedPawnCol = toCol;
removePiece(capturedPawnRow, capturedPawnCol);
removePiece(fromRow, toCol);
}
}
// castling check
if (piece.getSymbol().equals(Symbol.KING)) {
King king = new King(piece.getColor(), piece.getRow(), piece.getColumn());
if (Math.abs(fromCol - toCol) == 2) {
// short castling
if (toCol > fromCol) {
@ -177,8 +161,16 @@ public class Board {
public boolean isMoveValid(int fromRow, int fromCol, int toRow, int toCol, Color currPlayerColor) {
// check if not trying to go from or to outside the board
if (fromRow < 0 || fromRow >= 8 || fromCol < 0 || fromCol >= 8 ||
toRow < 0 || toRow >= 8 || toCol < 0 || toCol >= 8) {
if (
fromRow < 0 ||
fromRow >= 8 ||
fromCol < 0 ||
fromCol >= 8 ||
toRow < 0 ||
toRow >= 8 ||
toCol < 0 ||
toCol >= 8
) {
System.out.println("Coordinates out of the board! (Board)");
return false;
}
@ -198,7 +190,10 @@ public class Board {
}
// check if target field is not occupied by piece of the same color
if (fields[toRow][toCol] != null && fields[toRow][toCol].getColor() == currPlayerColor) {
if (
fields[toRow][toCol] != null &&
fields[toRow][toCol].getColor() == currPlayerColor
) {
System.out.println("There is your piece already! (Board)");
return false;
}
@ -208,7 +203,7 @@ public class Board {
System.out.println("Move invalid for a particular piece. (Board)");
return false;
}
// make sure the move doesnt lead to check
// make sure the move doesn't lead to check
Board tempBoard = copy();
tempBoard.makeMove(fromRow, fromCol, toRow, toCol);
@ -228,7 +223,7 @@ public class Board {
copy.removePiece(row, col);
}
}
// copyt board state
// copy board state
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
Basic piece = fields[row][col];
@ -249,7 +244,11 @@ public class Board {
int col = this.lastMovedPawn.getRow().getNum();
Basic lastMovedPawnCopy = copy.getPiece(row, col);
if(lastMovedPawnCopy != null){
copy.lastMovedPawn = new Pawn(lastMovedPawnCopy.getColor(), lastMovedPawnCopy.getRow(), lastMovedPawnCopy.getColumn());
copy.lastMovedPawn = new Pawn(
lastMovedPawnCopy.getColor(),
lastMovedPawnCopy.getRow(),
lastMovedPawnCopy.getColumn()
);
} else {
copy.lastMovedPawn = null;
}
@ -263,7 +262,12 @@ public class Board {
}
public Basic getPiece(int row, int column) {
if (row >= 0 && row < 8 && column >= 0 && column < 8) {
if (
row >= 0 &&
row < 8 &&
column >= 0 &&
column < 8
) {
return fields[row][column];
} else {
return null;
@ -286,48 +290,52 @@ public class Board {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
Basic piece = fields[row][col];
if (piece != null && piece.getSymbol().equals(Symbol.KING) && piece.getColor() == kingColor) {
if (
piece != null &&
piece.getSymbol().equals(Symbol.KING) &&
piece.getColor() == kingColor
) {
kingRow = row;
kingCol = col;
// System.out.println("[DEBUG] " + kingColor + " king is in " + Column.getColByNum(String.valueOf(kingCol + 1)) + String.valueOf(kingRow + 1));
break;
}
}
}
// check for each opponent piece if can take the king out
// check for each opponent piece if it can take the king out
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
Basic piece = fields[row][col];
if (piece != null && piece.getColor() != kingColor) {
if (
piece != null &&
piece.getColor() != kingColor
) {
Basic particularPiece = piece;
if(piece.getSymbol().equals(Symbol.QUEEN)){
particularPiece = (Queen) new Queen(particularPiece.getColor(), particularPiece.getRow(), particularPiece.getColumn());
particularPiece = new Queen(particularPiece.getColor(), particularPiece.getRow(), particularPiece.getColumn());
}
if(piece.getSymbol().equals(Symbol.KING)){
particularPiece = (King) new King(particularPiece.getColor(), particularPiece.getRow(), particularPiece.getColumn());
particularPiece = new King(particularPiece.getColor(), particularPiece.getRow(), particularPiece.getColumn());
}
if(piece.getSymbol().equals(Symbol.KNIGHT)){
particularPiece = (Knight) new Knight(particularPiece.getColor(), particularPiece.getRow(), particularPiece.getColumn());
particularPiece = new Knight(particularPiece.getColor(), particularPiece.getRow(), particularPiece.getColumn());
}
if(piece.getSymbol().equals(Symbol.BISHOP)){
particularPiece = (Bishop) new Bishop(particularPiece.getColor(), particularPiece.getRow(), particularPiece.getColumn());
particularPiece = new Bishop(particularPiece.getColor(), particularPiece.getRow(), particularPiece.getColumn());
}
if(piece.getSymbol().equals(Symbol.ROOK)){
particularPiece = (Rook) new Rook(particularPiece.getColor(), particularPiece.getRow(), particularPiece.getColumn());
particularPiece = new Rook(particularPiece.getColor(), particularPiece.getRow(), particularPiece.getColumn());
}
if(piece.getSymbol().equals(Symbol.PAWN)){
particularPiece = (Pawn) new Pawn(particularPiece.getColor(), particularPiece.getRow(), particularPiece.getColumn());
particularPiece = new Pawn(particularPiece.getColor(), particularPiece.getRow(), particularPiece.getColumn());
}
// check if piece can kill king
if (particularPiece.isMoveValid(kingRow, kingCol, this)) {
return true;
}
}
}
}
return false;
}
}

View File

@ -15,25 +15,16 @@ public enum Column {
}
public static Column getColByNum(String number) {
switch (number) {
case "1":
return Column.A;
case "2":
return Column.B;
case "3":
return Column.C;
case "4":
return Column.D;
case "5":
return Column.E;
case "6":
return Column.F;
case "7":
return Column.G;
case "8":
return Column.H;
default:
throw new IllegalArgumentException("Invalid column number: " + number);
}
return switch (number) {
case "1" -> Column.A;
case "2" -> Column.B;
case "3" -> Column.C;
case "4" -> Column.D;
case "5" -> Column.E;
case "6" -> Column.F;
case "7" -> Column.G;
case "8" -> Column.H;
default -> throw new IllegalArgumentException("Invalid column number: " + number);
};
}
}

View File

@ -1,117 +0,0 @@
//package com.jarocki.stanislaw.chess.Coordinate;
//
//import com.jarocki.stanislaw.chess.Player.Role;
//
//public class Coordinate {
// private String lastMoveFrom;
// private String lastMoveTo;
// private Column lastMoveFromCol;
// private Row lastMoveFromRow;
// private Column lastMoveToCol;
// private Row lastMoveToRow;
//
// public void setLastMove(String moveFrom, String moveTo) {
// if(moveFrom == null || moveTo == null) {
// System.out.println("Some moves are missing: moveFrom = " + moveFrom + " and moveTo = " + moveTo + " (setLastMove)");
// }
// setLastMoveFrom(moveFrom);
// setLastMoveTo(moveTo);
// }
//
// public void convertMoveToCoordinates() {
// String moveFrom = this.getLastMoveFrom();
// String moveTo = this.getLastMoveTo();
//
// if (moveFrom.length() != 2 || moveTo.length() != 2) {
// throw new RuntimeException("Incorrect move coordinates (convertMoveToCoordinates)");
// }
//
// Column moveFromCol = Column.valueOf(moveFrom.substring(0, 1));
// Row moveFromRow = Row.getRowByNumber(moveFrom.substring(1,2));
// Column moveToCol = Column.valueOf(moveTo.substring(0, 1));
// Row moveToRow = Row.getRowByNumber(moveTo.substring(1,2));
//
// setLastMoveFromCol(moveFromCol);
// setLastMoveFromRow(moveFromRow);
// setLastMoveToCol(moveToCol);
// setLastMoveToRow(moveToRow);
// }
//
// public String convertMoveToString() {
// Row rowFrom = this.getLastMoveFromRow();
// Row rowTo = this.getLastMoveToRow();
// Column colFrom = this.getLastMoveFromCol();
// Column colTo = this.getLastMoveToCol();
//
// return (
// colFrom.toString() +
// String.valueOf(rowFrom.getNum()) +
// colTo.toString() +
// String.valueOf(rowTo.getNum())
// );
// }
//
// public void displayMoveInfo() {
// String from = this.getLastMoveFrom();
// String to = this.getLastMoveTo();
// String playerName = this.getRole().formatName();
//
// if (from == null || to == null) {
// System.out.println("Player " + playerName + " hasn't moved yet");
// return;
// }
//
// System.out.println("Player " + playerName + " moved from " + from + " to " + to);
// }
// public Column getLastMoveFromCol() {
// return lastMoveFromCol;
// }
//
// public void setLastMoveFromCol(Column lastMoveFromCol) {
// this.lastMoveFromCol = lastMoveFromCol;
// }
//
// public Row getLastMoveFromRow() {
// return lastMoveFromRow;
// }
//
// public void setLastMoveFromRow(Row lastMoveFromRow) {
// this.lastMoveFromRow = lastMoveFromRow;
// }
//
// public Column getLastMoveToCol() {
// return lastMoveToCol;
// }
//
// public void setLastMoveToCol(Column lastMoveToCol) {
// this.lastMoveToCol = lastMoveToCol;
// }
//
// public Row getLastMoveToRow() {
// return lastMoveToRow;
// }
//
// public void setLastMoveToRow(Row lastMoveToRow) {
// this.lastMoveToRow = lastMoveToRow;
// }
//
// public Role getRole() {
// return role;
// }
//
// public String getLastMoveFrom() {
// return lastMoveFrom;
// }
//
// private void setLastMoveFrom(String lastMoveFrom) {
// this.lastMoveFrom = lastMoveFrom;
// }
//
// public String getLastMoveTo() {
// return lastMoveTo;
// }
//
// private void setLastMoveTo(String lastMoveTo) {
// this.lastMoveTo = lastMoveTo;
// }
//}

View File

@ -11,26 +11,17 @@ public enum Row {
EIGHT;
public static Row getRowByNum(String number) {
switch (number) {
case "1":
return Row.ONE;
case "2":
return Row.TWO;
case "3":
return Row.THREE;
case "4":
return Row.FOUR;
case "5":
return Row.FIVE;
case "6":
return Row.SIX;
case "7":
return Row.SEVEN;
case "8":
return Row.EIGHT;
default:
throw new IllegalArgumentException("Invalid row number: " + number);
}
return switch (number) {
case "1" -> Row.ONE;
case "2" -> Row.TWO;
case "3" -> Row.THREE;
case "4" -> Row.FOUR;
case "5" -> Row.FIVE;
case "6" -> Row.SIX;
case "7" -> Row.SEVEN;
case "8" -> Row.EIGHT;
default -> throw new IllegalArgumentException("Invalid row number: " + number);
};
}
public int getNum() {

View File

@ -8,16 +8,9 @@ import com.jarocki.stanislaw.chess.Player.Role;
import java.util.Scanner;
public class Game {
private Board board;
private boolean isGameOn;
private boolean isWhiteTurn = true;
public Game() {
board = new Board();
}
public void play() {
Scanner scanner = new Scanner(System.in);
this.setIsGameOn(true);
@ -26,8 +19,8 @@ public class Game {
board.setUpPieces();
int iteration = 0; // tests only
// for @tests
int iteration = 0;
// String[] testMoves = Test.getOnlyPawnsOpening();
// String[] testMoves = Test.getWrongPawnMoves();
// String[] testMoves = Test.takeQueenOut();
@ -36,27 +29,31 @@ public class Game {
// String[] testMoves = Test.takeRookOut();
// String[] testMoves = Test.takeKingOut();
// String[] testMoves = Test.getCastlingMoves();
String[] testMoves = Test.getPawnPromotion();
// String[] testMoves = Test.getOpeningMoves();
// String[] testMoves = Test.getPawnPromotion();
String[] testMoves = Test.getOpeningMoves();
// end of tests
while(getIsGameOn()) {
board.printBoard();
Color playerColor = this.getIsWhiteTurn() ? Color.WHITE : Color.BLACK;
Color playerColor =
this.getIsWhiteTurn()
? Color.WHITE
: Color.BLACK;
System.out.println("Enter " + playerColor.toString() + " next move: ");
System.out.println("Enter " + playerColor + " next move: ");
// for tests
Test.delay(300);
// tests
String input;
if(iteration >= testMoves.length) {
input = scanner.nextLine();
} else { // tests only
} else {
input = testMoves[iteration];
iteration = iteration + 1;
}
// end of tests
String[] moves = input.toUpperCase().split(" ");
@ -73,10 +70,16 @@ public class Game {
int toRow = currPlayer.getLastMoveToRow().getNum();
int toCol = currPlayer.getLastMoveToCol().getNum();
Test.displayCurrentMove(currPlayer.getRole().formatName(), moves[0], moves[1]);
if (!board.isMoveValid(fromRow, fromCol, toRow, toCol, playerColor)) {
System.out.println("Moving " + currPlayer.getRole().formatName() + " from " + moves[0] + " to " + moves[1] + " is illegal. (Game)");
System.out.println(
"Moving " +
currPlayer.getRole().formatName() +
" from " +
moves[0] +
" to " +
moves[1] +
" is illegal. (Game)"
);
this.switchTurn();
currPlayer.switchRole();
continue;
@ -87,6 +90,8 @@ public class Game {
this.switchTurn();
currPlayer.switchRole();
}
// todo implement check win
System.out.println("Congratulations, check-mate!");
scanner.close();
}

View File

@ -7,7 +7,7 @@ import com.jarocki.stanislaw.chess.Coordinate.Row;
public class Bishop extends Basic {
public Bishop(Color color, Row row, Column col) {
super(color, row, col);
setSymbol(getSymbol().BISHOP);
setSymbol(Symbol.BISHOP);
}
@Override
@ -15,7 +15,12 @@ public class Bishop extends Basic {
int row = this.getRow().getNum();
int col = this.getColumn().getNum();
if (toRow < 0 || toRow >= 8 || toCol < 0 || toCol >= 8) {
if (
toRow < 0 ||
toRow >= 8 ||
toCol < 0 ||
toCol >= 8
) {
return false;
}
@ -34,7 +39,10 @@ public class Bishop extends Basic {
int currentRow = row + stepRow;
int currentCol = col + stepCol;
while (currentRow != toRow || currentCol != toCol) {
while (
currentRow != toRow ||
currentCol != toCol
) {
if (board.getPiece(currentRow, currentCol) != null) {
return false;
}
@ -47,8 +55,4 @@ public class Bishop extends Basic {
Basic destinationPiece = board.getPiece(toRow, toCol);
return destinationPiece == null || destinationPiece.getColor() != this.getColor();
}
public Basic copy() {
return new Bishop(this.getColor(), this.getRow(), this.getColumn());
}
}

View File

@ -5,6 +5,6 @@ public enum Color {
BLACK;
public String toString() {
return this.name().substring(0,1) + this.name().substring(1).toLowerCase();
return this.name().charAt(0) + this.name().substring(1).toLowerCase();
}
}

View File

@ -5,7 +5,7 @@ import com.jarocki.stanislaw.chess.Coordinate.Column;
import com.jarocki.stanislaw.chess.Coordinate.Row;
public class King extends Basic {
private boolean hasMoved;
private final boolean hasMoved;
public King(Color color, Row row, Column col) {
super(color, row, col);
@ -22,20 +22,26 @@ public class King extends Basic {
int col = this.getColumn().getNum();
// check if inside the board
if (toRow < 0 || toRow >= 8 || toCol < 0 || toCol >= 8) {
if (
toRow < 0 ||
toRow >= 8 ||
toCol < 0 ||
toCol >= 8
) {
return false;
}
int dRow = Math.abs(toRow - row);
int dCol = Math.abs(toCol - col);
// king can move only by one tile unless its a castling
if ((dRow != 1 || dCol != 0) && (dRow != 0 || dCol != 1) && (dRow != 1 || dCol != 1)) {
// Check for castling
if (isCastlingMove(toRow, toCol, board)) {
return true;
}
return false;
// king can move only by one tile unless it's a castling
if (
(dRow != 1 || dCol != 0) &&
(dRow != 0 || dCol != 1) &&
(dRow != 1 || dCol != 1)
) {
// castling check
return isCastlingMove(toRow, toCol, board);
}
// check if destination is empty or with opponent
@ -43,15 +49,8 @@ public class King extends Basic {
return destinationPiece == null || destinationPiece.getColor() != this.getColor();
}
public Basic copy() {
King copy = new King(this.getColor(), this.getRow(), this.getColumn());
copy.hasMoved = hasMoved;
return copy;
}
private boolean isCastlingMove(int toRow, int toCol, Board board) {
int fromRow = this.getRow().getNum();
int fromCol = this.getColumn().getNum();
// check if king moved
if (hasMoved()) {
@ -68,19 +67,21 @@ public class King extends Basic {
return false;
}
//
int rookCol = (toCol == 2) ? 0 : 7;
Rook rook = (Rook) board.getPiece(fromRow, rookCol);
if (!(rook instanceof Rook) || rook.getColor() != this.getColor()) {
if (
!(rook.getSymbol().equals(Symbol.ROOK)) ||
rook.getColor() != this.getColor()
) {
return false;
}
// Check if the path between the king and rook is clear
// check path between rook and king
int startCol = (toCol == 2) ? 1 : 5;
int endCol = (toCol == 2) ? 3 : 6;
int row = fromRow;
for (int col = startCol; col <= endCol; col++) {
Basic piece = board.getPiece(row, col);
Basic piece = board.getPiece(fromRow, col);
if (piece != null) {
return false;
}

View File

@ -14,7 +14,12 @@ public class Knight extends Basic {
int row = this.getRow().getNum();
int col = this.getColumn().getNum();
// make sure the destination coordinates are on the board
if (toRow < 0 || toRow >= 8 || toCol < 0 || toCol >= 8) {
if (
toRow < 0 ||
toRow >= 8 ||
toCol < 0 ||
toCol >= 8
) {
return false;
}
@ -24,8 +29,4 @@ public class Knight extends Basic {
// knight can move only in "L" path = to cols up one row vert and vice versa
return (dRow == 2 && dCol == 1) || (dRow == 1 && dCol == 2);
}
public Basic copy() {
return new Knight(this.getColor(), this.getRow(), this.getColumn());
}
}

View File

@ -26,7 +26,7 @@ public class Pawn extends Basic {
if (targetRow - getRow().getNum() == 1) {
return true;
} else if (isStartingPosition(getRow(), getColor()) && targetRow - getRow().getNum() == 2) {
// check if can move two tiles
// check if it can move two tiles
if (board.getPiece(getRow().getNum() + 1, targetColumn) == null) {
return true;
}
@ -35,59 +35,63 @@ public class Pawn extends Basic {
if (targetRow - getRow().getNum() == -1) {
return true;
} else if (isStartingPosition(getRow(), getColor()) && targetRow - getRow().getNum() == -2) {
// check if can move two tiles
// check if thing can move two tiles
if (board.getPiece(getRow().getNum() - 1, targetColumn) == null) {
return true;
}
}
}
}
// if en passant
return isValidCapture(targetRow, targetColumn, board);
} else {
// i know it's silly but works
return isValidCapture(targetRow, targetColumn, board);
}
// if en passant
return isValidCapture(targetRow, targetColumn, board);
}
private boolean isValidCapture(int targetRow, int targetColumn, Board board) {
// inside board
if (targetRow < 0 || targetRow >= 8 || targetColumn < 0 || targetColumn >= 8) {
if (
targetRow < 0 ||
targetRow >= 8 ||
targetColumn < 0 ||
targetColumn >= 8
) {
return false;
}
Basic targetPiece = board.getPiece(targetRow, targetColumn);
if (targetPiece == null) {
// en passant check
if (targetColumn == getColumn().getNum() - 1 || targetColumn == getColumn().getNum() + 1) {
if (
targetColumn == getColumn().getNum() - 1 ||
targetColumn == getColumn().getNum() + 1
) {
// check target tile
if (targetRow == getRow().getNum() + (getColor() == Color.WHITE ? 1 : -1)) {
Pawn lastMovedPawn = board.getLastMovedPawn();
if (lastMovedPawn != null && lastMovedPawn.getRow() == getRow() &&
lastMovedPawn.getColumn().getNum() == targetColumn) {
// correct en passant
return true;
}
// correct en passant
return lastMovedPawn != null &&
lastMovedPawn.getRow() == getRow() &&
lastMovedPawn.getColumn().getNum() == targetColumn;
}
}
return false;
}
int col = getColumn().getNum();
if (targetColumn == getColumn().getNum() - 1) {
// diagonal to top left
Color color = getColor();
if (getColor() == Color.WHITE && targetRow == getRow().getNum() + 1) {
if (
getColor() == Color.WHITE &&
targetRow == getRow().getNum() + 1
) {
return true;
} else if (getColor() == Color.BLACK && targetRow == getRow().getNum() - 1) {
return true;
}
} else return getColor() == Color.BLACK && targetRow == getRow().getNum() - 1;
} else if (targetColumn == getColumn().getNum() + 1) {
// one tile to top right
if (getColor() == Color.WHITE && targetRow == getRow().getNum() + 1) {
if (
getColor() == Color.WHITE &&
targetRow == getRow().getNum() + 1
) {
return true;
} else if (getColor() == Color.BLACK && targetRow == getRow().getNum() - 1) {
return true;
}
} else return getColor() == Color.BLACK && targetRow == getRow().getNum() - 1;
}
return false;

View File

@ -22,13 +22,16 @@ public class Queen extends Basic {
int dCol = toCol - col;
// queen moving vertical/horizontally/diagonally
if (dRow != 0 && dCol != 0 && Math.abs(dRow) != Math.abs(dCol)) {
if (
dRow != 0 && dCol != 0 &&
Math.abs(dRow) != Math.abs(dCol)
) {
return false;
}
// check if there's sth between her and the destination
int stepRow = dRow == 0 ? 0 : (dRow > 0 ? 1 : -1);
int stepCol = dCol == 0 ? 0 : (dCol > 0 ? 1 : -1);
int stepRow = Integer.compare(dRow, 0);
int stepCol = Integer.compare(dCol, 0);
int currentRow = row + stepRow;
int currentCol = col + stepCol;
@ -46,8 +49,4 @@ public class Queen extends Basic {
Basic destinationPiece = board.getPiece(toRow, toCol);
return destinationPiece == null || destinationPiece.getColor() != this.getColor();
}
public Basic copy() {
return new Queen(this.getColor(), this.getRow(), this.getColumn());
}
}

View File

@ -5,7 +5,6 @@ import com.jarocki.stanislaw.chess.Coordinate.Column;
import com.jarocki.stanislaw.chess.Coordinate.Row;
public class Rook extends Basic {
private boolean hasMoved = false;
public Rook(Color color, Row row, Column col) {
super(color, row, col);
setSymbol(Symbol.ROOK);
@ -36,7 +35,10 @@ public class Rook extends Basic {
int currentRow = row + stepRow;
int currentCol = col + stepCol;
while (currentRow != toRow || currentCol != toCol) {
while (
currentRow != toRow ||
currentCol != toCol
) {
if (board.getPiece(currentRow, currentCol) != null) {
return false;
}
@ -49,12 +51,4 @@ public class Rook extends Basic {
Basic destinationPiece = board.getPiece(toRow, toCol);
return destinationPiece == null || destinationPiece.getColor() != this.getColor();
}
public void setHasMoved() {
hasMoved = true;
}
public Basic copy() {
return new Rook(this.getColor(), this.getRow(), this.getColumn());
}
}

View File

@ -96,7 +96,7 @@ public class Test {
"f3 d4", "f6 d5",
"e4 d5", "e7 e6",
"c1 g5", "d8 f6",
"d1 f3", "e8 g8",
"d1 f3", "h7 h6",
"e1 g1", "f8 e7", // castling
"g1 h1", "d5 c6",
"d4 c5", "c8 d7",