diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..d655412 --- /dev/null +++ b/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..a132403 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + chess-system + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..a54bb93 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=10 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=10 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=10 diff --git a/src/application/Program.java b/src/application/Program.java new file mode 100644 index 0000000..b1ac2ad --- /dev/null +++ b/src/application/Program.java @@ -0,0 +1,55 @@ +package application; + +import java.util.ArrayList; +import java.util.InputMismatchException; +import java.util.List; +import java.util.Scanner; + +import chess.ChessException; +import chess.ChessMatch; +import chess.ChessPiece; +import chess.ChessPosition; + +public class Program { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + ChessMatch chessMatch = new ChessMatch(); + List captured = new ArrayList<>(); + + while (!chessMatch.getCheckMate()) { + try { + UI.clearScreen(); + UI.printMatch(chessMatch, captured); + System.out.println(); + System.out.print("Source: "); + ChessPosition source = UI.readChessPosition(sc); + + boolean[][] possibleMoves = chessMatch.possibleMoves(source); + UI.clearScreen(); + UI.printBoard(chessMatch.getPieces(), possibleMoves); + + System.out.println(); + System.out.print("Target: "); + ChessPosition target = UI.readChessPosition(sc); + + ChessPiece capturedPiece = chessMatch.performChessMove(source, target); + + if (capturedPiece != null) { + captured.add(capturedPiece); + } + } catch (ChessException e) { + System.out.println(e.getMessage()); + sc.nextLine(); + } catch (InputMismatchException e) { + System.out.println(e.getMessage()); + sc.nextLine(); + } + + } + UI.clearScreen(); + UI.printMatch(chessMatch, captured); + + } +} \ No newline at end of file diff --git a/src/application/UI.java b/src/application/UI.java new file mode 100644 index 0000000..688e1fd --- /dev/null +++ b/src/application/UI.java @@ -0,0 +1,125 @@ +package application; + +import java.util.Arrays; +import java.util.InputMismatchException; +import java.util.List; +import java.util.Scanner; +import java.util.stream.Collectors; + +import chess.ChessMatch; +import chess.ChessPiece; +import chess.ChessPosition; +import chess.Color; + +public class UI { + + public static final String ANSI_RESET = "\u001B[0m"; + public static final String ANSI_BLACK = "\u001B[30m"; + public static final String ANSI_RED = "\u001B[31m"; + public static final String ANSI_GREEN = "\u001B[32m"; + public static final String ANSI_YELLOW = "\u001B[33m"; + public static final String ANSI_BLUE = "\u001B[34m"; + public static final String ANSI_PURPLE = "\u001B[35m"; + public static final String ANSI_CYAN = "\u001B[36m"; + public static final String ANSI_WHITE = "\u001B[37m"; + + public static final String ANSI_BLACK_BACKGROUND = "\u001B[40m"; + public static final String ANSI_RED_BACKGROUND = "\u001B[41m"; + public static final String ANSI_GREEN_BACKGROUND = "\u001B[42m"; + public static final String ANSI_YELLOW_BACKGROUND = "\u001B[43m"; + public static final String ANSI_BLUE_BACKGROUND = "\u001B[44m"; + public static final String ANSI_PURPLE_BACKGROUND = "\u001B[45m"; + public static final String ANSI_CYAN_BACKGROUND = "\u001B[46m"; + public static final String ANSI_WHITE_BACKGROUND = "\u001B[47m"; + + // https://stackoverflow.com/questions/2979383/java-clear-the-console + public static void clearScreen() { + System.out.print("\033[H\033[2J"); + System.out.flush(); + } + + public static ChessPosition readChessPosition(Scanner sc) { + try { + String s = sc.nextLine(); + char column = s.charAt(0); + int row = Integer.parseInt(s.substring(1)); + return new ChessPosition(column, row); + } catch (RuntimeException e) { + throw new InputMismatchException("Error reading ChessPosition. Valid values are from a1 to h8"); + } + } + + public static void printMatch(ChessMatch chessMatch, List captured) { + printBoard(chessMatch.getPieces()); + System.out.println(); + printCapturedPieces(captured); + System.out.println(); + System.out.println("turn : " + chessMatch.getTurn()); + if (!chessMatch.getCheckMate()) { + System.out.println("Waiting player: " + chessMatch.getCurrentPlayer()); + if (chessMatch.getCheck()) { + System.out.println("CHECK!"); + } + } else { + System.out.println("CHECKMATE!"); + System.out.println("Winner: " + chessMatch.getCurrentPlayer()); + } + } + + public static void printBoard(ChessPiece[][] pieces) { + for (int i = 0; i < pieces.length; i++) { + System.out.print((8 - i + " ")); + for (int j = 0; j < pieces.length; j++) { + printPiece(pieces[i][j], false); + } + System.out.println(); + } + System.out.print(" a b c d e f g h "); + } + + public static void printBoard(ChessPiece[][] pieces, boolean[][] possibleMoves) { + for (int i = 0; i < pieces.length; i++) { + System.out.print((8 - i + " ")); + for (int j = 0; j < pieces.length; j++) { + printPiece(pieces[i][j], possibleMoves[i][j]); + } + System.out.println(); + } + System.out.print(" a b c d e f g h "); + } + + private static void printPiece(ChessPiece piece, boolean background) { + if (background) { + System.out.print(ANSI_BLUE_BACKGROUND); + + } + if (piece == null) { + System.out.print("-" + ANSI_RESET); + } else { + if (piece.getColor() == Color.WHITE) { + System.out.print(ANSI_WHITE + piece + ANSI_RESET); + } else { + System.out.print(ANSI_YELLOW + piece + ANSI_RESET); + } + } + System.out.print(" "); + } + + private static void printCapturedPieces(List captured) { + List white = captured.stream().filter(x -> x.getColor() == Color.WHITE) + .collect(Collectors.toList()); + List black = captured.stream().filter(x -> x.getColor() == Color.BLACK) + .collect(Collectors.toList()); + System.out.println("Captured piece:"); + System.out.print("White: " + white); + System.out.println(ANSI_WHITE); + System.out.println(Arrays.toString(white.toArray())); + System.out.println(ANSI_RESET); + System.out.print("Black:" + black); + System.out.println(ANSI_BLACK); + System.out.println(Arrays.toString(black.toArray())); + System.out.println(ANSI_RESET); + + } + +} diff --git a/src/boardgame/Board.java b/src/boardgame/Board.java new file mode 100644 index 0000000..9123feb --- /dev/null +++ b/src/boardgame/Board.java @@ -0,0 +1,75 @@ +package boardgame; + +public class Board { + private int rows; + private int columns; + private Piece[][] pieces; + + public Board(int rows, int columns) { + if (rows < 1 || columns < 1) { + throw new BoardException("Error creating board there must be at least 1 row and 1 column"); + } + this.rows = rows; + this.columns = columns; + pieces = new Piece[rows][columns]; + } + + public int getRows() { + return rows; + } + + public int getColumns() { + return columns; + } + + public Piece piece(int row, int column) { + if (!positionExists(row, column)) { + throw new BoardException("Position not on the board"); + } + return pieces[row][column]; + } + + public Piece piece(Position position) { + if (!positionExists(position)) { + throw new BoardException("Position not on the board"); + } + return pieces[position.getRow()][position.getColumn()]; + } + + public void placePiece(Piece piece, Position position) { + if (thereIsAPiece(position)) { + throw new BoardException("there is already a piece on position " + position); + } + pieces[position.getRow()][position.getColumn()] = piece; + piece.position = position; + } + + public Piece removePiece (Position position) { + if (!positionExists(position)) { + throw new BoardException("Position not on the board"); + } + if (piece(position) == null) { + return null; + } + Piece aux = piece(position); + aux.position = null; + pieces[position.getRow()][position.getColumn()] = null; + return aux; + } + + private boolean positionExists(int row, int column) { + return row >= 0 && row < rows && column >= 0 && column < columns; + } + + public boolean positionExists(Position position) { + return positionExists(position.getColumn(), position.getColumn()); + } + + public boolean thereIsAPiece(Position position) { + if (!positionExists(position)) { + throw new BoardException("Position not on the board"); + } + return piece(position) != null; + } + +} diff --git a/src/boardgame/BoardException.java b/src/boardgame/BoardException.java new file mode 100644 index 0000000..de8e428 --- /dev/null +++ b/src/boardgame/BoardException.java @@ -0,0 +1,11 @@ +package boardgame; + +public class BoardException extends RuntimeException { + private static final long serialVersionUID = 1L; + + public BoardException(String msg) { + super(msg); + } + + +} diff --git a/src/boardgame/Piece.java b/src/boardgame/Piece.java new file mode 100644 index 0000000..9de6779 --- /dev/null +++ b/src/boardgame/Piece.java @@ -0,0 +1,33 @@ +package boardgame; + +public abstract class Piece { + protected Position position; + private Board board; + + public Piece(Board board) { + this.board = board; + } + + protected Board getBoard() { + return board; + } + + public abstract boolean[][] possibleMoves(); + + public boolean possibleMove(Position position) { + return possibleMoves()[position.getRow()][position.getColumn()]; + } + + public boolean isThereAnyPossibleMove() { + boolean[][] mat = possibleMoves(); + for (int i=0; i piecesOnTheBoard = new ArrayList<>(); + private List capturedPieces = new ArrayList<>(); + + + public ChessMatch() { + board = new Board (8, 8); + turn = 1; + currentPlayer = Color.WHITE; + initialSetup(); + } + + public int getTurn() { + return turn; + } + public Color getCurrentPlayer() { + return currentPlayer; + } + + public boolean getCheck() { + return check; + } + public boolean getCheckMate() { + return checkMate; + } + + public ChessPiece[][] getPieces(){ + ChessPiece[][] mat = new ChessPiece[board.getRows()][board.getColumns()]; + for (int i=0; i list = piecesOnTheBoard.stream().filter(x -> ((ChessPiece)x).getColor() == color).collect(Collectors.toList()); + for (Piece p : list) { + if (p instanceof King) { + return (ChessPiece)p; + } + } + throw new IllegalStateException("Thre is no" + color + " King on the board"); + } + + private boolean testCheck(Color color) { + Position KingPosition = King(color).getChessPosition().toPosition(); + List opponentPieces = piecesOnTheBoard.stream().filter(x -> ((ChessPiece)x).getColor() == opponent(color)).collect(Collectors.toList()); + for (Piece p : opponentPieces) { + boolean[][] mat = p.possibleMoves(); + if (mat[KingPosition.getRow()][KingPosition.getColumn()]){ + return true; + } + } + return false; + } + + private boolean testCheckMate(Color color) { + if(!testCheck(color)) { + return false; + } + + List list = piecesOnTheBoard.stream().filter(x -> ((ChessPiece)x).getColor() == color).collect(Collectors.toList()); + for(Piece p : list) { + boolean[][] mat = p.possibleMoves(); + for(int i=0; i 'h' || row < 1 || row > 8) { + throw new ChessException("Error instantiating ChessPosition. Valid values are from a1 to h8 "); + } + this.column = column; + this.row = row; + + } + + + public char getColumn() { + return column; + } + + public int getRow() { + return row; + } + + protected Position toPosition() { + return new Position(8 - row, column - 'a'); + } + + protected static ChessPosition fromPosition(Position position) { + return new ChessPosition ((char)('a' + position.getColumn()),8 - position.getRow()); + } + @Override + public String toString() { + return " " + column + row; + } +} diff --git a/src/chess/Color.java b/src/chess/Color.java new file mode 100644 index 0000000..a5bfc3b --- /dev/null +++ b/src/chess/Color.java @@ -0,0 +1,6 @@ +package chess; + +public enum Color { + BLACK, + WHITE; +} diff --git a/src/chess/pieces/Bishop.java b/src/chess/pieces/Bishop.java new file mode 100644 index 0000000..45127a1 --- /dev/null +++ b/src/chess/pieces/Bishop.java @@ -0,0 +1,84 @@ +package chess.pieces; + +import boardgame.Board; +import boardgame.Position; +import chess.ChessPiece; +import chess.Color; + +public class Bishop extends ChessPiece { + + public Bishop(Board board, Color color) { + super(board, color); + // TODO Auto-generated constructor stub + } + + @Override + public String toString() { + return "B"; + } + + @Override + public boolean[][] possibleMoves() { + boolean[][] mat = new boolean[getBoard().getRows()][getBoard().getColumns()]; + + Position p = new Position(0, 0); + + // nw + + p.setValues(position.getRow() - 1, position.getColumn() - 1); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setValues(p.getRow() - 1, p.getColumn() - 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + // ne + + p.setValues(position.getRow() - 1, position.getColumn() + 1); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setValues(p.getRow() - 1, p.getColumn() + 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + // se + + p.setValues(position.getRow() + 1, position.getColumn() + 1); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setValues(p.getRow() + 1, p.getColumn() + 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + // sw + + p.setValues(position.getRow() + 1, position.getColumn() - 1); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setValues(p.getRow() + 1, p.getColumn() - 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + return mat; + } +} diff --git a/src/chess/pieces/King.java b/src/chess/pieces/King.java new file mode 100644 index 0000000..fee47cf --- /dev/null +++ b/src/chess/pieces/King.java @@ -0,0 +1,83 @@ +package chess.pieces; + +import boardgame.Board; +import boardgame.Position; +import chess.ChessPiece; +import chess.Color; + +public class King extends ChessPiece { + + public King(Board board, Color color) { + super(board, color); + // TODO Auto-generated constructor stub + } + + @Override + public String toString() { + return "K"; + } + + private boolean canMove(Position position) { + ChessPiece p = (ChessPiece) getBoard().piece(position); + return p == null || p.getColor() != getColor(); + + } + + @Override + public boolean[][] possibleMoves() { + boolean[][] mat = new boolean[getBoard().getRows()][getBoard().getColumns()]; + + Position p = new Position(0, 0); + + // above + p.setValues(position.getRow() - 1, position.getColumn()); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + // bellow + p.setValues(position.getRow() + 1, position.getColumn()); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + // left + p.setValues(position.getRow(), position.getColumn() - 1); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + // right + p.setValues(position.getRow(), position.getColumn() + 1); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + // nw + p.setValues(position.getRow() - 1, position.getColumn() - 1); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + // ne + p.setValues(position.getRow() - 1, position.getColumn() + 1); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + // sw + p.setValues(position.getRow() + 1, position.getColumn() - 1); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + // se + p.setValues(position.getRow() + 1, position.getColumn() + 1); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + return mat; + } + +} diff --git a/src/chess/pieces/Knight.java b/src/chess/pieces/Knight.java new file mode 100644 index 0000000..452dace --- /dev/null +++ b/src/chess/pieces/Knight.java @@ -0,0 +1,83 @@ +package chess.pieces; + +import boardgame.Board; +import boardgame.Position; +import chess.ChessPiece; +import chess.Color; + +public class Knight extends ChessPiece { + + public Knight(Board board, Color color) { + super(board, color); + // TODO Auto-generated constructor stub + } + + @Override + public String toString() { + return "N"; + } + + private boolean canMove(Position position) { + ChessPiece p = (ChessPiece) getBoard().piece(position); + return p == null || p.getColor() != getColor(); + + } + + @Override + public boolean[][] possibleMoves() { + boolean[][] mat = new boolean[getBoard().getRows()][getBoard().getColumns()]; + + Position p = new Position(0, 0); + + + p.setValues(position.getRow() - 1, position.getColumn() - 2); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + + p.setValues(position.getRow() - 2, position.getColumn() - 1); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + + p.setValues(position.getRow(), position.getColumn() - 1); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + + p.setValues(position.getRow(), position.getColumn() + 1); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + + p.setValues(position.getRow() - 1, position.getColumn() - 1); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + + p.setValues(position.getRow() - 1, position.getColumn() + 1); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + + p.setValues(position.getRow() + 1, position.getColumn() - 1); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + + p.setValues(position.getRow() + 1, position.getColumn() + 1); + if (getBoard().positionExists(p) && canMove(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + return mat; + } + +} diff --git a/src/chess/pieces/Pawn.java b/src/chess/pieces/Pawn.java new file mode 100644 index 0000000..10fbbfb --- /dev/null +++ b/src/chess/pieces/Pawn.java @@ -0,0 +1,68 @@ +package chess.pieces; + +import boardgame.Board; +import boardgame.Position; +import chess.ChessPiece; +import chess.Color; + +public class Pawn extends ChessPiece { + + public Pawn(Board board, Color color) { + super(board, color); + } + + @Override + public boolean[][] possibleMoves() { + boolean[][] mat = new boolean[getBoard().getRows()][getBoard().getColumns()]; + + Position p = new Position(0, 0); + + if (getColor() == Color.WHITE) { + p.setValues(position.getRow() - 1, position.getColumn()); + if (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + p.setValues(position.getRow() - 2, position.getColumn()); + Position p2 = new Position(position.getRow() - 1, position.getColumn()); + if (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)&& getBoard().positionExists(p2) && !getBoard().thereIsAPiece(p2) && getMoveCount() == 0 ) { + mat[p.getRow()][p.getColumn()] = true; + } + p.setValues(position.getRow() - 1, position.getColumn() - 1); + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + p.setValues(position.getRow() - 1, position.getColumn() + 1); + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + } + else { + p.setValues(position.getRow() + 1, position.getColumn()); + if (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + p.setValues(position.getRow() + 2, position.getColumn()); + Position p2 = new Position(position.getRow() + 1, position.getColumn()); + if (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)&& getBoard().positionExists(p2) && !getBoard().thereIsAPiece(p2) && getMoveCount() == 0 ) { + mat[p.getRow()][p.getColumn()] = true; + } + p.setValues(position.getRow() + 1, position.getColumn() - 1); + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + p.setValues(position.getRow() + 1, position.getColumn() + 1); + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + } + + } + return mat; + } + + @Override + public String toString() { + return "p"; + } +} diff --git a/src/chess/pieces/Queen.java b/src/chess/pieces/Queen.java new file mode 100644 index 0000000..1005310 --- /dev/null +++ b/src/chess/pieces/Queen.java @@ -0,0 +1,140 @@ +package chess.pieces; + +import boardgame.Board; +import boardgame.Position; +import chess.ChessPiece; +import chess.Color; + +public class Queen extends ChessPiece { + + public Queen(Board board, Color color) { + super(board, color); + // TODO Auto-generated constructor stub + } + + @Override + public String toString() { + return "Q"; + } + + @Override + public boolean[][] possibleMoves() { + boolean[][] mat = new boolean[getBoard().getRows()][getBoard().getColumns()]; + + Position p = new Position(0, 0); + + // above + + p.setValues(position.getRow() - 1, position.getColumn()); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setRow(p.getRow() - 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + // left + + p.setValues(position.getRow(), position.getColumn() - 1); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setColumn(p.getColumn() - 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + // right + + p.setValues(position.getRow(), position.getColumn() + 1); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setColumn(p.getColumn() + 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + // below + + p.setValues(position.getRow() + 1, position.getColumn()); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setRow(p.getRow() + 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + // nw + + p.setValues(position.getRow() - 1, position.getColumn() - 1); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setValues(p.getRow() - 1, p.getColumn() - 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + // ne + + p.setValues(position.getRow() - 1, position.getColumn() + 1); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setValues(p.getRow() - 1, p.getColumn() + 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + // se + + p.setValues(position.getRow() + 1, position.getColumn() + 1); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setValues(p.getRow() + 1, p.getColumn() + 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + // sw + + p.setValues(position.getRow() + 1, position.getColumn() - 1); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setValues(p.getRow() + 1, p.getColumn() - 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + return mat; + } +} diff --git a/src/chess/pieces/Rook.java b/src/chess/pieces/Rook.java new file mode 100644 index 0000000..54d77dc --- /dev/null +++ b/src/chess/pieces/Rook.java @@ -0,0 +1,84 @@ +package chess.pieces; + +import boardgame.Board; +import boardgame.Position; +import chess.ChessPiece; +import chess.Color; + +public class Rook extends ChessPiece { + + public Rook(Board board, Color color) { + super(board, color); + // TODO Auto-generated constructor stub + } + + @Override + public String toString() { + return "R"; + } + + @Override + public boolean[][] possibleMoves() { + boolean[][] mat = new boolean[getBoard().getRows()][getBoard().getColumns()]; + + Position p = new Position(0, 0); + + // above + + p.setValues(position.getRow() - 1, position.getColumn()); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setRow(p.getRow() - 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + // left + + p.setValues(position.getRow(), position.getColumn() - 1); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setColumn(p.getColumn() - 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + // right + + p.setValues(position.getRow(), position.getColumn() + 1); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setColumn(p.getColumn() + 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + // below + + p.setValues(position.getRow() + 1, position.getColumn()); + while (getBoard().positionExists(p) && !getBoard().thereIsAPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + p.setRow(p.getRow() + 1); + + } + + if (getBoard().positionExists(p) && isThereOpponentPiece(p)) { + mat[p.getRow()][p.getColumn()] = true; + + } + + return mat; + } +}