refactor: add indents

This commit is contained in:
Stanisław Jarocki 2023-06-20 18:33:30 +02:00
parent c19b9a5a0a
commit c01c236c94
10 changed files with 128 additions and 42 deletions

View File

@ -7,17 +7,14 @@
<list default="true" id="594d24b9-cb67-479c-bd03-0c89ab1bafab" name="Changes" comment="">
<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/Coordinate/Column.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Coordinate/Column.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Coordinate/Coordinate.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Coordinate/Row.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Coordinate/Row.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/Color.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece/Color.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>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -67,7 +64,10 @@
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"last_opened_file_path": "/home/stanlee77/code/stanchezz",
"settings.editor.selected.configurable": "reference.settings.project.maven.runner"
"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 name="RecentsManager">
@ -126,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

@ -161,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;
}
@ -182,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;
}
@ -192,7 +203,7 @@ public class Board {
System.out.println("Move invalid for a particular piece. (Board)");
return false;
}
// make sure the move doesn't lead to check
// make sure the move doesn't lead to check
Board tempBoard = copy();
tempBoard.makeMove(fromRow, fromCol, toRow, toCol);
@ -233,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;
}
@ -247,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;
@ -270,10 +290,13 @@ 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;
}
}
@ -283,7 +306,10 @@ 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.getColor() != kingColor) {
if (
piece != null &&
piece.getColor() != kingColor
) {
Basic particularPiece = piece;
if(piece.getSymbol().equals(Symbol.QUEEN)){
particularPiece = new Queen(particularPiece.getColor(), particularPiece.getRow(), particularPiece.getColumn());
@ -303,15 +329,13 @@ public class Board {
if(piece.getSymbol().equals(Symbol.PAWN)){
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

@ -19,9 +19,9 @@ public class Game {
board.setUpPieces();
int iteration = 0; // tests only
String[] testMoves = Test.getOnlyPawnsOpening();
// for @tests
int iteration = 0;
// String[] testMoves = Test.getOnlyPawnsOpening();
// String[] testMoves = Test.getWrongPawnMoves();
// String[] testMoves = Test.takeQueenOut();
// String[] testMoves = Test.takeBishopOut();
@ -30,25 +30,30 @@ public class Game {
// String[] testMoves = Test.takeKingOut();
// String[] testMoves = Test.getCastlingMoves();
// String[] testMoves = Test.getPawnPromotion();
// String[] testMoves = Test.getOpeningMoves();
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 + " 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(" ");
@ -65,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;
@ -79,7 +90,7 @@ public class Game {
this.switchTurn();
currPlayer.switchRole();
}
// implement check win
// todo implement check win
System.out.println("Congratulations, check-mate!");
scanner.close();
}

View File

@ -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;
}

View File

@ -22,7 +22,12 @@ 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;
}
@ -30,7 +35,11 @@ public class King extends Basic {
int dCol = Math.abs(toCol - col);
// 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)) {
if (
(dRow != 1 || dCol != 0) &&
(dRow != 0 || dCol != 1) &&
(dRow != 1 || dCol != 1)
) {
// castling check
return isCastlingMove(toRow, toCol, board);
}
@ -60,13 +69,17 @@ public class King extends Basic {
int rookCol = (toCol == 2) ? 0 : 7;
Rook rook = (Rook) board.getPiece(fromRow, rookCol);
if (!(rook.getSymbol().equals(Symbol.ROOK)) || rook.getColor() != this.getColor()) {
if (
!(rook.getSymbol().equals(Symbol.ROOK)) ||
rook.getColor() != this.getColor()
) {
return false;
}
// check path between rook and king
int startCol = (toCol == 2) ? 1 : 5;
int endCol = (toCol == 2) ? 3 : 6;
for (int col = startCol; col <= endCol; col++) {
Basic piece = board.getPiece(fromRow, col);
if (piece != null) {

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;
}

View File

@ -49,19 +49,28 @@ public class Pawn extends Basic {
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();
// correct en passant
return lastMovedPawn != null && lastMovedPawn.getRow() == getRow() &&
return lastMovedPawn != null &&
lastMovedPawn.getRow() == getRow() &&
lastMovedPawn.getColumn().getNum() == targetColumn;
}
}
@ -69,12 +78,18 @@ public class Pawn extends Basic {
}
if (targetColumn == getColumn().getNum() - 1) {
// diagonal to top left
if (getColor() == Color.WHITE && targetRow == getRow().getNum() + 1) {
if (
getColor() == Color.WHITE &&
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 return getColor() == Color.BLACK && targetRow == getRow().getNum() - 1;
}

View File

@ -22,7 +22,10 @@ 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;
}

View File

@ -35,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;
}

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",