refactor: refactor and remove unnecessary code

This commit is contained in:
Stanisław Jarocki 2023-06-20 17:48:03 +02:00
parent fc76c43754
commit c19b9a5a0a
13 changed files with 73 additions and 267 deletions

View File

@ -7,10 +7,17 @@
<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" />
@ -55,14 +62,14 @@
<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",
"settings.editor.selected.configurable": "reference.settings.project.maven.runner"
}
}</component>
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece" />

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) {
@ -208,7 +192,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 +212,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];
@ -295,29 +279,29 @@ public class Board {
}
}
// 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) {
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());
}
if (particularPiece.isMoveValid(kingRow, kingCol, this)) {

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);
@ -28,7 +21,7 @@ public class Game {
int iteration = 0; // tests only
// String[] testMoves = Test.getOnlyPawnsOpening();
String[] testMoves = Test.getOnlyPawnsOpening();
// String[] testMoves = Test.getWrongPawnMoves();
// String[] testMoves = Test.takeQueenOut();
// String[] testMoves = Test.takeBishopOut();
@ -36,16 +29,15 @@ public class Game {
// String[] testMoves = Test.takeRookOut();
// String[] testMoves = Test.takeKingOut();
// String[] testMoves = Test.getCastlingMoves();
String[] testMoves = Test.getPawnPromotion();
// String[] testMoves = Test.getPawnPromotion();
// String[] testMoves = Test.getOpeningMoves();
while(getIsGameOn()) {
board.printBoard();
Color playerColor = this.getIsWhiteTurn() ? Color.WHITE : Color.BLACK;
System.out.println("Enter " + playerColor.toString() + " next move: ");
System.out.println("Enter " + playerColor + " next move: ");
Test.delay(300);
@ -73,7 +65,7 @@ public class Game {
int toRow = currPlayer.getLastMoveToRow().getNum();
int toCol = currPlayer.getLastMoveToCol().getNum();
Test.displayCurrentMove(currPlayer.getRole().formatName(), moves[0], moves[1]);
// 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)");
@ -87,6 +79,8 @@ public class Game {
this.switchTurn();
currPlayer.switchRole();
}
// 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
@ -47,8 +47,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);
@ -29,13 +29,10 @@ public class King extends Basic {
int dRow = Math.abs(toRow - row);
int dCol = Math.abs(toCol - col);
// king can move only by one tile unless its a castling
// 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)) {
// Check for castling
if (isCastlingMove(toRow, toCol, board)) {
return true;
}
return false;
// castling check
return isCastlingMove(toRow, toCol, board);
}
// check if destination is empty or with opponent
@ -43,15 +40,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 +58,17 @@ 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

@ -24,8 +24,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,19 +35,16 @@ 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) {
@ -63,31 +60,23 @@ public class Pawn extends Basic {
// 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) {
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) {
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

@ -27,8 +27,8 @@ public class Queen extends Basic {
}
// 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 +46,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);
@ -49,12 +48,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());
}
}