refactor: refactor and remove unnecessary code
This commit is contained in:
parent
fc76c43754
commit
c19b9a5a0a
@ -7,10 +7,17 @@
|
|||||||
<list default="true" id="594d24b9-cb67-479c-bd03-0c89ab1bafab" name="Changes" comment="">
|
<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$/.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/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/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/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/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>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
@ -55,14 +62,14 @@
|
|||||||
<option name="hideEmptyMiddlePackages" value="true" />
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
<option name="showLibraryContents" value="true" />
|
<option name="showLibraryContents" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PropertiesComponent">{
|
<component name="PropertiesComponent"><![CDATA[{
|
||||||
"keyToString": {
|
"keyToString": {
|
||||||
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
"last_opened_file_path": "/home/stanlee77/code/chessEngine/src/com/jarocki/stanislaw/chess/Piece",
|
"last_opened_file_path": "/home/stanlee77/code/stanchezz",
|
||||||
"settings.editor.selected.configurable": "reference.settings.project.maven.runner"
|
"settings.editor.selected.configurable": "reference.settings.project.maven.runner"
|
||||||
}
|
}
|
||||||
}</component>
|
}]]></component>
|
||||||
<component name="RecentsManager">
|
<component name="RecentsManager">
|
||||||
<key name="CopyFile.RECENT_KEYS">
|
<key name="CopyFile.RECENT_KEYS">
|
||||||
<recent name="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece" />
|
<recent name="$PROJECT_DIR$/src/com/jarocki/stanislaw/chess/Piece" />
|
||||||
|
@ -5,8 +5,6 @@ import com.jarocki.stanislaw.chess.Coordinate.Row;
|
|||||||
import com.jarocki.stanislaw.chess.Piece.*;
|
import com.jarocki.stanislaw.chess.Piece.*;
|
||||||
import com.jarocki.stanislaw.chess.Piece.Color;
|
import com.jarocki.stanislaw.chess.Piece.Color;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public class Board {
|
public class Board {
|
||||||
private final Basic[][] fields;
|
private final Basic[][] fields;
|
||||||
private boolean hasWhiteKingMoved;
|
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);
|
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() {
|
public void printBoard() {
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
@ -116,17 +104,13 @@ public class Board {
|
|||||||
|
|
||||||
// remove en passant captured pawn from board
|
// remove en passant captured pawn from board
|
||||||
if (piece.getSymbol().equals(Symbol.PAWN)) {
|
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) {
|
if (Math.abs(fromRow - toRow) == 1 && Math.abs(fromCol - toCol) == 1 && fields[toRow][toCol] == null) {
|
||||||
int capturedPawnRow = fromRow;
|
removePiece(fromRow, toCol);
|
||||||
int capturedPawnCol = toCol;
|
|
||||||
removePiece(capturedPawnRow, capturedPawnCol);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// castling check
|
// castling check
|
||||||
if (piece.getSymbol().equals(Symbol.KING)) {
|
if (piece.getSymbol().equals(Symbol.KING)) {
|
||||||
King king = new King(piece.getColor(), piece.getRow(), piece.getColumn());
|
|
||||||
if (Math.abs(fromCol - toCol) == 2) {
|
if (Math.abs(fromCol - toCol) == 2) {
|
||||||
// short castling
|
// short castling
|
||||||
if (toCol > fromCol) {
|
if (toCol > fromCol) {
|
||||||
@ -208,7 +192,7 @@ public class Board {
|
|||||||
System.out.println("Move invalid for a particular piece. (Board)");
|
System.out.println("Move invalid for a particular piece. (Board)");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// make sure the move doesnt lead to check
|
// make sure the move doesn't lead to check
|
||||||
Board tempBoard = copy();
|
Board tempBoard = copy();
|
||||||
|
|
||||||
tempBoard.makeMove(fromRow, fromCol, toRow, toCol);
|
tempBoard.makeMove(fromRow, fromCol, toRow, toCol);
|
||||||
@ -228,7 +212,7 @@ public class Board {
|
|||||||
copy.removePiece(row, col);
|
copy.removePiece(row, col);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// copyt board state
|
// copy board state
|
||||||
for (int row = 0; row < 8; row++) {
|
for (int row = 0; row < 8; row++) {
|
||||||
for (int col = 0; col < 8; col++) {
|
for (int col = 0; col < 8; col++) {
|
||||||
Basic piece = fields[row][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 row = 0; row < 8; row++) {
|
||||||
for (int col = 0; col < 8; col++) {
|
for (int col = 0; col < 8; col++) {
|
||||||
Basic piece = fields[row][col];
|
Basic piece = fields[row][col];
|
||||||
if (piece != null && piece.getColor() != kingColor) {
|
if (piece != null && piece.getColor() != kingColor) {
|
||||||
Basic particularPiece = piece;
|
Basic particularPiece = piece;
|
||||||
if(piece.getSymbol().equals(Symbol.QUEEN)){
|
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)){
|
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)){
|
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)){
|
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)){
|
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)){
|
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)) {
|
if (particularPiece.isMoveValid(kingRow, kingCol, this)) {
|
||||||
|
@ -15,25 +15,16 @@ public enum Column {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Column getColByNum(String number) {
|
public static Column getColByNum(String number) {
|
||||||
switch (number) {
|
return switch (number) {
|
||||||
case "1":
|
case "1" -> Column.A;
|
||||||
return Column.A;
|
case "2" -> Column.B;
|
||||||
case "2":
|
case "3" -> Column.C;
|
||||||
return Column.B;
|
case "4" -> Column.D;
|
||||||
case "3":
|
case "5" -> Column.E;
|
||||||
return Column.C;
|
case "6" -> Column.F;
|
||||||
case "4":
|
case "7" -> Column.G;
|
||||||
return Column.D;
|
case "8" -> Column.H;
|
||||||
case "5":
|
default -> throw new IllegalArgumentException("Invalid column number: " + number);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
|
||||||
// }
|
|
||||||
//}
|
|
@ -11,26 +11,17 @@ public enum Row {
|
|||||||
EIGHT;
|
EIGHT;
|
||||||
|
|
||||||
public static Row getRowByNum(String number) {
|
public static Row getRowByNum(String number) {
|
||||||
switch (number) {
|
return switch (number) {
|
||||||
case "1":
|
case "1" -> Row.ONE;
|
||||||
return Row.ONE;
|
case "2" -> Row.TWO;
|
||||||
case "2":
|
case "3" -> Row.THREE;
|
||||||
return Row.TWO;
|
case "4" -> Row.FOUR;
|
||||||
case "3":
|
case "5" -> Row.FIVE;
|
||||||
return Row.THREE;
|
case "6" -> Row.SIX;
|
||||||
case "4":
|
case "7" -> Row.SEVEN;
|
||||||
return Row.FOUR;
|
case "8" -> Row.EIGHT;
|
||||||
case "5":
|
default -> throw new IllegalArgumentException("Invalid row number: " + number);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getNum() {
|
public int getNum() {
|
||||||
|
@ -8,16 +8,9 @@ import com.jarocki.stanislaw.chess.Player.Role;
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Game {
|
public class Game {
|
||||||
private Board board;
|
|
||||||
|
|
||||||
private boolean isGameOn;
|
private boolean isGameOn;
|
||||||
|
|
||||||
private boolean isWhiteTurn = true;
|
private boolean isWhiteTurn = true;
|
||||||
|
|
||||||
public Game() {
|
|
||||||
board = new Board();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void play() {
|
public void play() {
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
this.setIsGameOn(true);
|
this.setIsGameOn(true);
|
||||||
@ -28,7 +21,7 @@ public class Game {
|
|||||||
|
|
||||||
int iteration = 0; // tests only
|
int iteration = 0; // tests only
|
||||||
|
|
||||||
// String[] testMoves = Test.getOnlyPawnsOpening();
|
String[] testMoves = Test.getOnlyPawnsOpening();
|
||||||
// String[] testMoves = Test.getWrongPawnMoves();
|
// String[] testMoves = Test.getWrongPawnMoves();
|
||||||
// String[] testMoves = Test.takeQueenOut();
|
// String[] testMoves = Test.takeQueenOut();
|
||||||
// String[] testMoves = Test.takeBishopOut();
|
// String[] testMoves = Test.takeBishopOut();
|
||||||
@ -36,16 +29,15 @@ public class Game {
|
|||||||
// String[] testMoves = Test.takeRookOut();
|
// String[] testMoves = Test.takeRookOut();
|
||||||
// String[] testMoves = Test.takeKingOut();
|
// String[] testMoves = Test.takeKingOut();
|
||||||
// String[] testMoves = Test.getCastlingMoves();
|
// String[] testMoves = Test.getCastlingMoves();
|
||||||
String[] testMoves = Test.getPawnPromotion();
|
// String[] testMoves = Test.getPawnPromotion();
|
||||||
// String[] testMoves = Test.getOpeningMoves();
|
// String[] testMoves = Test.getOpeningMoves();
|
||||||
|
|
||||||
|
|
||||||
while(getIsGameOn()) {
|
while(getIsGameOn()) {
|
||||||
board.printBoard();
|
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: ");
|
||||||
|
|
||||||
Test.delay(300);
|
Test.delay(300);
|
||||||
|
|
||||||
@ -73,7 +65,7 @@ public class Game {
|
|||||||
int toRow = currPlayer.getLastMoveToRow().getNum();
|
int toRow = currPlayer.getLastMoveToRow().getNum();
|
||||||
int toCol = currPlayer.getLastMoveToCol().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)) {
|
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)");
|
||||||
@ -87,6 +79,8 @@ public class Game {
|
|||||||
this.switchTurn();
|
this.switchTurn();
|
||||||
currPlayer.switchRole();
|
currPlayer.switchRole();
|
||||||
}
|
}
|
||||||
|
// implement check win
|
||||||
|
System.out.println("Congratulations, check-mate!");
|
||||||
scanner.close();
|
scanner.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import com.jarocki.stanislaw.chess.Coordinate.Row;
|
|||||||
public class Bishop extends Basic {
|
public class Bishop extends Basic {
|
||||||
public Bishop(Color color, Row row, Column col) {
|
public Bishop(Color color, Row row, Column col) {
|
||||||
super(color, row, col);
|
super(color, row, col);
|
||||||
setSymbol(getSymbol().BISHOP);
|
setSymbol(Symbol.BISHOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -47,8 +47,4 @@ public class Bishop extends Basic {
|
|||||||
Basic destinationPiece = board.getPiece(toRow, toCol);
|
Basic destinationPiece = board.getPiece(toRow, toCol);
|
||||||
return destinationPiece == null || destinationPiece.getColor() != this.getColor();
|
return destinationPiece == null || destinationPiece.getColor() != this.getColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Basic copy() {
|
|
||||||
return new Bishop(this.getColor(), this.getRow(), this.getColumn());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,6 @@ public enum Color {
|
|||||||
BLACK;
|
BLACK;
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return this.name().substring(0,1) + this.name().substring(1).toLowerCase();
|
return this.name().charAt(0) + this.name().substring(1).toLowerCase();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import com.jarocki.stanislaw.chess.Coordinate.Column;
|
|||||||
import com.jarocki.stanislaw.chess.Coordinate.Row;
|
import com.jarocki.stanislaw.chess.Coordinate.Row;
|
||||||
|
|
||||||
public class King extends Basic {
|
public class King extends Basic {
|
||||||
private boolean hasMoved;
|
private final boolean hasMoved;
|
||||||
|
|
||||||
public King(Color color, Row row, Column col) {
|
public King(Color color, Row row, Column col) {
|
||||||
super(color, row, col);
|
super(color, row, col);
|
||||||
@ -29,13 +29,10 @@ public class King extends Basic {
|
|||||||
int dRow = Math.abs(toRow - row);
|
int dRow = Math.abs(toRow - row);
|
||||||
int dCol = Math.abs(toCol - col);
|
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)) {
|
if ((dRow != 1 || dCol != 0) && (dRow != 0 || dCol != 1) && (dRow != 1 || dCol != 1)) {
|
||||||
// Check for castling
|
// castling check
|
||||||
if (isCastlingMove(toRow, toCol, board)) {
|
return isCastlingMove(toRow, toCol, board);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if destination is empty or with opponent
|
// check if destination is empty or with opponent
|
||||||
@ -43,15 +40,8 @@ public class King extends Basic {
|
|||||||
return destinationPiece == null || destinationPiece.getColor() != this.getColor();
|
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) {
|
private boolean isCastlingMove(int toRow, int toCol, Board board) {
|
||||||
int fromRow = this.getRow().getNum();
|
int fromRow = this.getRow().getNum();
|
||||||
int fromCol = this.getColumn().getNum();
|
|
||||||
|
|
||||||
// check if king moved
|
// check if king moved
|
||||||
if (hasMoved()) {
|
if (hasMoved()) {
|
||||||
@ -68,19 +58,17 @@ public class King extends Basic {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
int rookCol = (toCol == 2) ? 0 : 7;
|
int rookCol = (toCol == 2) ? 0 : 7;
|
||||||
Rook rook = (Rook) board.getPiece(fromRow, rookCol);
|
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;
|
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 startCol = (toCol == 2) ? 1 : 5;
|
||||||
int endCol = (toCol == 2) ? 3 : 6;
|
int endCol = (toCol == 2) ? 3 : 6;
|
||||||
int row = fromRow;
|
|
||||||
for (int col = startCol; col <= endCol; col++) {
|
for (int col = startCol; col <= endCol; col++) {
|
||||||
Basic piece = board.getPiece(row, col);
|
Basic piece = board.getPiece(fromRow, col);
|
||||||
if (piece != null) {
|
if (piece != null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -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
|
// 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);
|
return (dRow == 2 && dCol == 1) || (dRow == 1 && dCol == 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Basic copy() {
|
|
||||||
return new Knight(this.getColor(), this.getRow(), this.getColumn());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ public class Pawn extends Basic {
|
|||||||
if (targetRow - getRow().getNum() == 1) {
|
if (targetRow - getRow().getNum() == 1) {
|
||||||
return true;
|
return true;
|
||||||
} else if (isStartingPosition(getRow(), getColor()) && targetRow - getRow().getNum() == 2) {
|
} 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) {
|
if (board.getPiece(getRow().getNum() + 1, targetColumn) == null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -35,19 +35,16 @@ public class Pawn extends Basic {
|
|||||||
if (targetRow - getRow().getNum() == -1) {
|
if (targetRow - getRow().getNum() == -1) {
|
||||||
return true;
|
return true;
|
||||||
} else if (isStartingPosition(getRow(), getColor()) && targetRow - getRow().getNum() == -2) {
|
} 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) {
|
if (board.getPiece(getRow().getNum() - 1, targetColumn) == null) {
|
||||||
return true;
|
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) {
|
private boolean isValidCapture(int targetRow, int targetColumn, Board board) {
|
||||||
@ -63,31 +60,23 @@ public class Pawn extends Basic {
|
|||||||
// check target tile
|
// check target tile
|
||||||
if (targetRow == getRow().getNum() + (getColor() == Color.WHITE ? 1 : -1)) {
|
if (targetRow == getRow().getNum() + (getColor() == Color.WHITE ? 1 : -1)) {
|
||||||
Pawn lastMovedPawn = board.getLastMovedPawn();
|
Pawn lastMovedPawn = board.getLastMovedPawn();
|
||||||
if (lastMovedPawn != null && lastMovedPawn.getRow() == getRow() &&
|
// correct en passant
|
||||||
lastMovedPawn.getColumn().getNum() == targetColumn) {
|
return lastMovedPawn != null && lastMovedPawn.getRow() == getRow() &&
|
||||||
// correct en passant
|
lastMovedPawn.getColumn().getNum() == targetColumn;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int col = getColumn().getNum();
|
|
||||||
if (targetColumn == getColumn().getNum() - 1) {
|
if (targetColumn == getColumn().getNum() - 1) {
|
||||||
// diagonal to top left
|
// 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;
|
return true;
|
||||||
} else if (getColor() == Color.BLACK && targetRow == getRow().getNum() - 1) {
|
} else return getColor() == Color.BLACK && targetRow == getRow().getNum() - 1;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} else if (targetColumn == getColumn().getNum() + 1) {
|
} else if (targetColumn == getColumn().getNum() + 1) {
|
||||||
// one tile to top right
|
// one tile to top right
|
||||||
if (getColor() == Color.WHITE && targetRow == getRow().getNum() + 1) {
|
if (getColor() == Color.WHITE && targetRow == getRow().getNum() + 1) {
|
||||||
return true;
|
return true;
|
||||||
} else if (getColor() == Color.BLACK && targetRow == getRow().getNum() - 1) {
|
} else return getColor() == Color.BLACK && targetRow == getRow().getNum() - 1;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -27,8 +27,8 @@ public class Queen extends Basic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// check if there's sth between her and the destination
|
// check if there's sth between her and the destination
|
||||||
int stepRow = dRow == 0 ? 0 : (dRow > 0 ? 1 : -1);
|
int stepRow = Integer.compare(dRow, 0);
|
||||||
int stepCol = dCol == 0 ? 0 : (dCol > 0 ? 1 : -1);
|
int stepCol = Integer.compare(dCol, 0);
|
||||||
|
|
||||||
int currentRow = row + stepRow;
|
int currentRow = row + stepRow;
|
||||||
int currentCol = col + stepCol;
|
int currentCol = col + stepCol;
|
||||||
@ -46,8 +46,4 @@ public class Queen extends Basic {
|
|||||||
Basic destinationPiece = board.getPiece(toRow, toCol);
|
Basic destinationPiece = board.getPiece(toRow, toCol);
|
||||||
return destinationPiece == null || destinationPiece.getColor() != this.getColor();
|
return destinationPiece == null || destinationPiece.getColor() != this.getColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Basic copy() {
|
|
||||||
return new Queen(this.getColor(), this.getRow(), this.getColumn());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import com.jarocki.stanislaw.chess.Coordinate.Column;
|
|||||||
import com.jarocki.stanislaw.chess.Coordinate.Row;
|
import com.jarocki.stanislaw.chess.Coordinate.Row;
|
||||||
|
|
||||||
public class Rook extends Basic {
|
public class Rook extends Basic {
|
||||||
private boolean hasMoved = false;
|
|
||||||
public Rook(Color color, Row row, Column col) {
|
public Rook(Color color, Row row, Column col) {
|
||||||
super(color, row, col);
|
super(color, row, col);
|
||||||
setSymbol(Symbol.ROOK);
|
setSymbol(Symbol.ROOK);
|
||||||
@ -49,12 +48,4 @@ public class Rook extends Basic {
|
|||||||
Basic destinationPiece = board.getPiece(toRow, toCol);
|
Basic destinationPiece = board.getPiece(toRow, toCol);
|
||||||
return destinationPiece == null || destinationPiece.getColor() != this.getColor();
|
return destinationPiece == null || destinationPiece.getColor() != this.getColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHasMoved() {
|
|
||||||
hasMoved = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Basic copy() {
|
|
||||||
return new Rook(this.getColor(), this.getRow(), this.getColumn());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user