diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 6eb056e..7df2d00 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -7,17 +7,14 @@
-
-
-
-
+
@@ -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"
}
}]]>
@@ -126,6 +126,10 @@
+
+
+
+
diff --git a/src/com/jarocki/stanislaw/chess/Board/Board.java b/src/com/jarocki/stanislaw/chess/Board/Board.java
index 8457bf0..6528b57 100644
--- a/src/com/jarocki/stanislaw/chess/Board/Board.java
+++ b/src/com/jarocki/stanislaw/chess/Board/Board.java
@@ -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;
}
-
}
diff --git a/src/com/jarocki/stanislaw/chess/Game.java b/src/com/jarocki/stanislaw/chess/Game.java
index 7b55fa5..2dc8888 100644
--- a/src/com/jarocki/stanislaw/chess/Game.java
+++ b/src/com/jarocki/stanislaw/chess/Game.java
@@ -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();
}
diff --git a/src/com/jarocki/stanislaw/chess/Piece/Bishop.java b/src/com/jarocki/stanislaw/chess/Piece/Bishop.java
index 5867adf..cd479f2 100644
--- a/src/com/jarocki/stanislaw/chess/Piece/Bishop.java
+++ b/src/com/jarocki/stanislaw/chess/Piece/Bishop.java
@@ -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;
}
diff --git a/src/com/jarocki/stanislaw/chess/Piece/King.java b/src/com/jarocki/stanislaw/chess/Piece/King.java
index 31be7fe..a7b5862 100644
--- a/src/com/jarocki/stanislaw/chess/Piece/King.java
+++ b/src/com/jarocki/stanislaw/chess/Piece/King.java
@@ -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) {
diff --git a/src/com/jarocki/stanislaw/chess/Piece/Knight.java b/src/com/jarocki/stanislaw/chess/Piece/Knight.java
index ae89b0d..d245e12 100644
--- a/src/com/jarocki/stanislaw/chess/Piece/Knight.java
+++ b/src/com/jarocki/stanislaw/chess/Piece/Knight.java
@@ -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;
}
diff --git a/src/com/jarocki/stanislaw/chess/Piece/Pawn.java b/src/com/jarocki/stanislaw/chess/Piece/Pawn.java
index 5edba3e..6d8e531 100644
--- a/src/com/jarocki/stanislaw/chess/Piece/Pawn.java
+++ b/src/com/jarocki/stanislaw/chess/Piece/Pawn.java
@@ -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;
}
diff --git a/src/com/jarocki/stanislaw/chess/Piece/Queen.java b/src/com/jarocki/stanislaw/chess/Piece/Queen.java
index e580787..e90781c 100644
--- a/src/com/jarocki/stanislaw/chess/Piece/Queen.java
+++ b/src/com/jarocki/stanislaw/chess/Piece/Queen.java
@@ -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;
}
diff --git a/src/com/jarocki/stanislaw/chess/Piece/Rook.java b/src/com/jarocki/stanislaw/chess/Piece/Rook.java
index 28ead37..b0d6cb0 100644
--- a/src/com/jarocki/stanislaw/chess/Piece/Rook.java
+++ b/src/com/jarocki/stanislaw/chess/Piece/Rook.java
@@ -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;
}
diff --git a/src/com/jarocki/stanislaw/chess/Test.java b/src/com/jarocki/stanislaw/chess/Test.java
index 8b35e10..a12ef58 100644
--- a/src/com/jarocki/stanislaw/chess/Test.java
+++ b/src/com/jarocki/stanislaw/chess/Test.java
@@ -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",