From e71940074276ec8054ed2b368921c3b992de0f96 Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Fri, 17 Dec 2021 17:07:24 -0300 Subject: [PATCH 01/20] Firs class: Position --- .classpath | 10 ++++++++++ .project | 17 +++++++++++++++++ .settings/org.eclipse.jdt.core.prefs | 12 ++++++++++++ src/application/Program.java | 10 ++++++++++ 4 files changed, 49 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 src/application/Program.java 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..654b7d6 --- /dev/null +++ b/src/application/Program.java @@ -0,0 +1,10 @@ +package application; + +public class Program { + + public static void main(String[] args) { + System.out.println("Hello world"); + + } + +} From 6c1694d3f8e9d37cfe2fb88bde05f71281aba369 Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Thu, 6 Jan 2022 10:03:08 -0300 Subject: [PATCH 02/20] Chess layer and printing the board --- src/application/Program.java | 8 +++++- src/application/UI.java | 26 +++++++++++++++++++ src/boardgame/Board.java | 39 ++++++++++++++++++++++++++++ src/boardgame/Piece.java | 18 +++++++++++++ src/boardgame/Position.java | 49 ++++++++++++++++++++++++++++++++++++ src/chess/ChessMatch.java | 20 +++++++++++++++ src/chess/ChessPiece.java | 21 ++++++++++++++++ src/chess/Color.java | 6 +++++ 8 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 src/application/UI.java create mode 100644 src/boardgame/Board.java create mode 100644 src/boardgame/Piece.java create mode 100644 src/boardgame/Position.java create mode 100644 src/chess/ChessMatch.java create mode 100644 src/chess/ChessPiece.java create mode 100644 src/chess/Color.java diff --git a/src/application/Program.java b/src/application/Program.java index 654b7d6..a2aeba9 100644 --- a/src/application/Program.java +++ b/src/application/Program.java @@ -1,9 +1,15 @@ package application; +import chess.ChessMatch; + public class Program { public static void main(String[] args) { - System.out.println("Hello world"); + + ChessMatch chessMatch = new ChessMatch(); + UI.printBoard(chessMatch.getPieces()); + + } diff --git a/src/application/UI.java b/src/application/UI.java new file mode 100644 index 0000000..b947d45 --- /dev/null +++ b/src/application/UI.java @@ -0,0 +1,26 @@ +package application; + +import chess.ChessPiece; + +public class UI { + 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]); + } + System.out.println(); + } + System.out.print(" a b c d e f g h "); + } + + private static void printPiece(ChessPiece piece) { + if (piece == null) { + System.out.print("-"); + } else { + System.out.print(piece); + } + + System.out.print(" "); + } +} diff --git a/src/boardgame/Board.java b/src/boardgame/Board.java new file mode 100644 index 0000000..d2f8455 --- /dev/null +++ b/src/boardgame/Board.java @@ -0,0 +1,39 @@ +package boardgame; + +public class Board { + private int rows; + private int columns; + private Piece[][] pieces; + + + public Board(int rows, int columns) { + this.rows = rows; + this.columns = columns; + pieces = new Piece[rows][columns]; + } + + public int getRows() { + return rows; + } + + public void setRows(int rows) { + this.rows = rows; + } + + public int getColumns() { + return columns; + } + + public void setColumns(int columns) { + this.columns = columns; + } + + public Piece piece(int row, int column) { + return pieces[row][column]; + } + public Piece piece(Position position) { + return pieces[position.getRow()][position.getColumn()]; + } + +} + diff --git a/src/boardgame/Piece.java b/src/boardgame/Piece.java new file mode 100644 index 0000000..81eed0b --- /dev/null +++ b/src/boardgame/Piece.java @@ -0,0 +1,18 @@ +package boardgame; + +public class Piece { + protected Position position; + private Board board; + + + public Piece(Board board) { + this.board = board; + } + + + protected Board getBoard() { + return board; + } + + +} diff --git a/src/boardgame/Position.java b/src/boardgame/Position.java new file mode 100644 index 0000000..7e302ee --- /dev/null +++ b/src/boardgame/Position.java @@ -0,0 +1,49 @@ +package boardgame; + +public class Position { + private int row; + private int column; + + + public Position() { + + } + + public Position(int row, int column) { + this.row = row; + this.column = column; + + } + + + + + + + public int getRow() { + return row; + } + + + public void setRow(int row) { + this.row = row; + } + + + public int getColumn() { + return column; + } + + + public void setColumn(int column) { + this.column = column; + } + + @Override + public String toString() { + return row + ", " + column; + + } + + +} diff --git a/src/chess/ChessMatch.java b/src/chess/ChessMatch.java new file mode 100644 index 0000000..473d311 --- /dev/null +++ b/src/chess/ChessMatch.java @@ -0,0 +1,20 @@ +package chess; + +import boardgame.Board; + +public class ChessMatch { + private Board board; + + public ChessMatch() { + board = new Board (8, 8); + } + public ChessPiece[][] getPieces(){ + ChessPiece[][] mat = new ChessPiece[board.getRows()][board.getColumns()]; + for (int i=0; i Date: Thu, 6 Jan 2022 10:46:36 -0300 Subject: [PATCH 03/20] Placing piesces on the board --- src/boardgame/Board.java | 5 +++++ src/chess/ChessMatch.java | 9 +++++++++ src/chess/pieces/King.java | 18 ++++++++++++++++++ src/chess/pieces/Rook.java | 17 +++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 src/chess/pieces/King.java create mode 100644 src/chess/pieces/Rook.java diff --git a/src/boardgame/Board.java b/src/boardgame/Board.java index d2f8455..ea6bb72 100644 --- a/src/boardgame/Board.java +++ b/src/boardgame/Board.java @@ -35,5 +35,10 @@ public Piece piece(Position position) { return pieces[position.getRow()][position.getColumn()]; } + public void placePiece(Piece piece, Position position) { + pieces [position.getRow()][position.getColumn()] = piece; + piece.position = position; + } + } diff --git a/src/chess/ChessMatch.java b/src/chess/ChessMatch.java index 473d311..82437f5 100644 --- a/src/chess/ChessMatch.java +++ b/src/chess/ChessMatch.java @@ -1,12 +1,16 @@ package chess; import boardgame.Board; +import boardgame.Position; +import chess.pieces.King; +import chess.pieces.Rook; public class ChessMatch { private Board board; public ChessMatch() { board = new Board (8, 8); + initialSetup(); } public ChessPiece[][] getPieces(){ ChessPiece[][] mat = new ChessPiece[board.getRows()][board.getColumns()]; @@ -17,4 +21,9 @@ public ChessPiece[][] getPieces(){ } return mat; } + + private void initialSetup() { + board.placePiece(new Rook(board, Color.WHITE), new Position(2,1)); + board.placePiece(new King(board, Color.BLACK), new Position(0,4)); + } } diff --git a/src/chess/pieces/King.java b/src/chess/pieces/King.java new file mode 100644 index 0000000..de4871a --- /dev/null +++ b/src/chess/pieces/King.java @@ -0,0 +1,18 @@ +package chess.pieces; + +import boardgame.Board; +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"; + } + +} diff --git a/src/chess/pieces/Rook.java b/src/chess/pieces/Rook.java new file mode 100644 index 0000000..94f1e43 --- /dev/null +++ b/src/chess/pieces/Rook.java @@ -0,0 +1,17 @@ +package chess.pieces; + +import boardgame.Board; +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"; + } +} From 9f701aa45104b5e43dc1a131b5021a83a4040dd4 Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Thu, 6 Jan 2022 16:47:15 -0300 Subject: [PATCH 04/20] BoardException and defensive programming --- src/boardgame/Board.java | 44 ++++++++++++++++++++++--------- src/boardgame/BoardException.java | 11 ++++++++ 2 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 src/boardgame/BoardException.java diff --git a/src/boardgame/Board.java b/src/boardgame/Board.java index ea6bb72..c5a906f 100644 --- a/src/boardgame/Board.java +++ b/src/boardgame/Board.java @@ -5,8 +5,10 @@ public class Board { 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]; @@ -16,29 +18,45 @@ public int getRows() { return rows; } - public void setRows(int rows) { - this.rows = rows; - } - public int getColumns() { return columns; } - public void setColumns(int columns) { - this.columns = columns; - } - public Piece piece(int row, int column) { - return pieces[row][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) { - pieces [position.getRow()][position.getColumn()] = piece; + if (thereIsAPiece(position)) { + throw new BoardException("there is already a piece on position " + position); + } + pieces[position.getRow()][position.getColumn()] = piece; piece.position = position; } - -} + 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); + } + + +} From c333157d712c3ad703042ee0e0466ff4f298ed21 Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Thu, 6 Jan 2022 17:46:07 -0300 Subject: [PATCH 05/20] ChessException and ChessPosition --- src/chess/ChessException.java | 10 +++++++++ src/chess/ChessMatch.java | 9 +++++--- src/chess/ChessPosition.java | 40 +++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 src/chess/ChessException.java create mode 100644 src/chess/ChessPosition.java diff --git a/src/chess/ChessException.java b/src/chess/ChessException.java new file mode 100644 index 0000000..1c52668 --- /dev/null +++ b/src/chess/ChessException.java @@ -0,0 +1,10 @@ +package chess; + +public class ChessException extends RuntimeException{ + private static final long serialVersionUID = 1L; + + public ChessException(String msg){ + super(msg); + } + +} diff --git a/src/chess/ChessMatch.java b/src/chess/ChessMatch.java index 82437f5..17248dc 100644 --- a/src/chess/ChessMatch.java +++ b/src/chess/ChessMatch.java @@ -1,7 +1,6 @@ package chess; import boardgame.Board; -import boardgame.Position; import chess.pieces.King; import chess.pieces.Rook; @@ -22,8 +21,12 @@ public ChessPiece[][] getPieces(){ return mat; } + private void placeNewPiece(char column, int row, ChessPiece piece) { + board.placePiece(piece, new ChessPosition(column, row).toPosition()); + } + private void initialSetup() { - board.placePiece(new Rook(board, Color.WHITE), new Position(2,1)); - board.placePiece(new King(board, Color.BLACK), new Position(0,4)); + placeNewPiece('b', 6 , new Rook(board, Color.WHITE)); + placeNewPiece('e', 8,new King(board, Color.BLACK)); } } diff --git a/src/chess/ChessPosition.java b/src/chess/ChessPosition.java new file mode 100644 index 0000000..cd00947 --- /dev/null +++ b/src/chess/ChessPosition.java @@ -0,0 +1,40 @@ +package chess; + +import boardgame.Position; + +public class ChessPosition { + + private char column; + private int row; + + + public ChessPosition(char column, int row) { + if (column < 'a' || column > '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; + } +} From ca1b4cf51e2c86da29f3d128ba11f73a9771eb3b Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Thu, 6 Jan 2022 18:23:09 -0300 Subject: [PATCH 06/20] Little improvement in board printing --- src/application/UI.java | 30 +++++++++++++++++++++++++++--- src/chess/ChessMatch.java | 16 ++++++++++++++-- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/application/UI.java b/src/application/UI.java index b947d45..9986d9c 100644 --- a/src/application/UI.java +++ b/src/application/UI.java @@ -1,10 +1,31 @@ package application; import chess.ChessPiece; +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"; + public static void printBoard(ChessPiece[][] pieces) { - for (int i=0; i < pieces.length; i++) { + 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]); @@ -18,9 +39,12 @@ private static void printPiece(ChessPiece piece) { if (piece == null) { System.out.print("-"); } else { - System.out.print(piece); + 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(" "); } } diff --git a/src/chess/ChessMatch.java b/src/chess/ChessMatch.java index 17248dc..9416e43 100644 --- a/src/chess/ChessMatch.java +++ b/src/chess/ChessMatch.java @@ -26,7 +26,19 @@ private void placeNewPiece(char column, int row, ChessPiece piece) { } private void initialSetup() { - placeNewPiece('b', 6 , new Rook(board, Color.WHITE)); - placeNewPiece('e', 8,new King(board, Color.BLACK)); + placeNewPiece('c', 1, new Rook(board, Color.WHITE)); + placeNewPiece('c', 2, new Rook(board, Color.WHITE)); + placeNewPiece('d', 2, new Rook(board, Color.WHITE)); + placeNewPiece('e', 2, new Rook(board, Color.WHITE)); + placeNewPiece('e', 1, new Rook(board, Color.WHITE)); + placeNewPiece('d', 1, new King(board, Color.WHITE)); + + placeNewPiece('c', 7, new Rook(board, Color.BLACK)); + placeNewPiece('c', 8, new Rook(board, Color.BLACK)); + placeNewPiece('d', 7, new Rook(board, Color.BLACK)); + placeNewPiece('e', 7, new Rook(board, Color.BLACK)); + placeNewPiece('e', 8, new Rook(board, Color.BLACK)); + placeNewPiece('d', 8, new King(board, Color.BLACK)); + } } From 363357b04d2caef37c4658a7d7ce10c949bc784b Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Fri, 7 Jan 2022 11:24:59 -0300 Subject: [PATCH 07/20] Moving Pieces --- src/application/Program.java | 20 +++++++++++++++++--- src/application/UI.java | 15 +++++++++++++++ src/boardgame/Board.java | 13 +++++++++++++ src/chess/ChessException.java | 4 +++- src/chess/ChessMatch.java | 23 +++++++++++++++++++++++ src/chess/ChessPosition.java | 2 +- 6 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/application/Program.java b/src/application/Program.java index a2aeba9..e90d29b 100644 --- a/src/application/Program.java +++ b/src/application/Program.java @@ -1,16 +1,30 @@ package application; +import java.util.Scanner; + 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(); - UI.printBoard(chessMatch.getPieces()); - - + while (true) { + UI.printBoard(chessMatch.getPieces()); + System.out.println(); + System.out.print("Source: "); + ChessPosition source = UI.readChessPosition(sc); + + System.out.println(); + System.out.print("Target: "); + ChessPosition target = UI.readChessPosition(sc); + + ChessPiece capturedPiece = chessMatch.performChessMove(source, target); + } } } diff --git a/src/application/UI.java b/src/application/UI.java index 9986d9c..0b92c78 100644 --- a/src/application/UI.java +++ b/src/application/UI.java @@ -1,6 +1,10 @@ package application; +import java.util.InputMismatchException; +import java.util.Scanner; + import chess.ChessPiece; +import chess.ChessPosition; import chess.Color; public class UI { @@ -24,6 +28,17 @@ public class UI { public static final String ANSI_CYAN_BACKGROUND = "\u001B[46m"; public static final String ANSI_WHITE_BACKGROUND = "\u001B[47m"; + 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 printBoard(ChessPiece[][] pieces) { for (int i = 0; i < pieces.length; i++) { System.out.print((8 - i + " ")); diff --git a/src/boardgame/Board.java b/src/boardgame/Board.java index c5a906f..9123feb 100644 --- a/src/boardgame/Board.java +++ b/src/boardgame/Board.java @@ -43,6 +43,19 @@ public void placePiece(Piece piece, 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; diff --git a/src/chess/ChessException.java b/src/chess/ChessException.java index 1c52668..bb73f29 100644 --- a/src/chess/ChessException.java +++ b/src/chess/ChessException.java @@ -1,6 +1,8 @@ package chess; -public class ChessException extends RuntimeException{ +import boardgame.BoardException; + +public class ChessException extends BoardException{ private static final long serialVersionUID = 1L; public ChessException(String msg){ diff --git a/src/chess/ChessMatch.java b/src/chess/ChessMatch.java index 9416e43..995c2e0 100644 --- a/src/chess/ChessMatch.java +++ b/src/chess/ChessMatch.java @@ -1,6 +1,8 @@ package chess; import boardgame.Board; +import boardgame.Piece; +import boardgame.Position; import chess.pieces.King; import chess.pieces.Rook; @@ -21,6 +23,27 @@ public ChessPiece[][] getPieces(){ return mat; } + public ChessPiece performChessMove(ChessPosition sourcePosition, ChessPosition targetPosition) { + Position source = sourcePosition.toPosition(); + Position target = targetPosition.toPosition(); + validateSourcePosition(source); + Piece capturedPiece = makeMove(source, target); + return (ChessPiece)capturedPiece; + } + + private Piece makeMove(Position source, Position target) { + Piece p = board.removePiece(source); + Piece capturedPiece = board.removePiece(target); + board.placePiece(p, target); + return capturedPiece; + } + + private void validateSourcePosition(Position position) { + if (!board.thereIsAPiece(position)) { + throw new ChessException("There is no piece on source position"); + } + } + private void placeNewPiece(char column, int row, ChessPiece piece) { board.placePiece(piece, new ChessPosition(column, row).toPosition()); } diff --git a/src/chess/ChessPosition.java b/src/chess/ChessPosition.java index cd00947..122b38f 100644 --- a/src/chess/ChessPosition.java +++ b/src/chess/ChessPosition.java @@ -35,6 +35,6 @@ protected static ChessPosition fromPosition(Position position) { } @Override public String toString() { - return "" + column + row; + return " " + column + row; } } From a3ae95131eaf56f26a85ee1b382559c4fd5b5326 Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Fri, 7 Jan 2022 14:20:02 -0300 Subject: [PATCH 08/20] Handling exceptions and clearing screen --- src/application/Program.java | 36 +++++++++++++++++++++++++----------- src/application/UI.java | 8 +++++++- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/application/Program.java b/src/application/Program.java index e90d29b..efc5d64 100644 --- a/src/application/Program.java +++ b/src/application/Program.java @@ -1,7 +1,9 @@ package application; +import java.util.InputMismatchException; import java.util.Scanner; +import chess.ChessException; import chess.ChessMatch; import chess.ChessPiece; import chess.ChessPosition; @@ -14,17 +16,29 @@ public static void main(String[] args) { ChessMatch chessMatch = new ChessMatch(); while (true) { - UI.printBoard(chessMatch.getPieces()); - System.out.println(); - System.out.print("Source: "); - ChessPosition source = UI.readChessPosition(sc); - - System.out.println(); - System.out.print("Target: "); - ChessPosition target = UI.readChessPosition(sc); - - ChessPiece capturedPiece = chessMatch.performChessMove(source, target); - } + try { + UI.clearScreen(); + UI.printBoard(chessMatch.getPieces()); + System.out.println(); + System.out.print("Source: "); + ChessPosition source = UI.readChessPosition(sc); + + System.out.println(); + System.out.print("Target: "); + ChessPosition target = UI.readChessPosition(sc); + + ChessPiece capturedPiece = chessMatch.performChessMove(source, target); + + } + catch(ChessException e) { + System.out.println(e.getMessage()); + sc.nextLine(); + } + catch(InputMismatchException e) { + System.out.println(e.getMessage()); + sc.nextLine(); + } } } +} \ No newline at end of file diff --git a/src/application/UI.java b/src/application/UI.java index 0b92c78..764f43b 100644 --- a/src/application/UI.java +++ b/src/application/UI.java @@ -28,6 +28,12 @@ public class UI { 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(); @@ -47,7 +53,7 @@ public static void printBoard(ChessPiece[][] pieces) { } System.out.println(); } - System.out.print(" a b c d e f g h "); + System.out.print(" a b c d e f g h "); } private static void printPiece(ChessPiece piece) { From 647804c2328dcbdd3aebbf4cc5bd1a183d10a0cc Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Fri, 7 Jan 2022 16:22:32 -0300 Subject: [PATCH 09/20] Possible moves of a piece --- src/boardgame/Piece.java | 23 +++++++++++++++++++---- src/chess/ChessMatch.java | 3 +++ src/chess/ChessPiece.java | 2 +- src/chess/pieces/King.java | 5 +++++ src/chess/pieces/Rook.java | 5 +++++ 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/boardgame/Piece.java b/src/boardgame/Piece.java index 81eed0b..9de6779 100644 --- a/src/boardgame/Piece.java +++ b/src/boardgame/Piece.java @@ -1,18 +1,33 @@ package boardgame; -public class Piece { +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 Date: Tue, 11 Jan 2022 16:17:08 -0300 Subject: [PATCH 10/20] Printing possible moves and implementing rook moves --- src/application/Program.java | 4 +++ src/application/UI.java | 21 ++++++++++-- src/boardgame/Position.java | 28 +++++++--------- src/chess/ChessMatch.java | 12 +++++++ src/chess/ChessPiece.java | 11 +++++-- src/chess/pieces/Rook.java | 64 +++++++++++++++++++++++++++++++++++- 6 files changed, 117 insertions(+), 23 deletions(-) diff --git a/src/application/Program.java b/src/application/Program.java index efc5d64..e2ef0db 100644 --- a/src/application/Program.java +++ b/src/application/Program.java @@ -23,6 +23,10 @@ public static void main(String[] args) { 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); diff --git a/src/application/UI.java b/src/application/UI.java index 764f43b..291aa61 100644 --- a/src/application/UI.java +++ b/src/application/UI.java @@ -49,16 +49,31 @@ 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]); + printPiece(pieces[i][j], false); } System.out.println(); } System.out.print(" a b c d e f g h "); } - private static void printPiece(ChessPiece piece) { + 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("-"); + System.out.print("-" + ANSI_RESET ); } else { if (piece.getColor() == Color.WHITE) { System.out.print(ANSI_WHITE + piece + ANSI_RESET); diff --git a/src/boardgame/Position.java b/src/boardgame/Position.java index 7e302ee..22a8f20 100644 --- a/src/boardgame/Position.java +++ b/src/boardgame/Position.java @@ -3,47 +3,43 @@ public class Position { private int row; private int column; - - + public Position() { - + } - + public Position(int row, int column) { this.row = row; this.column = column; - - } - - - - + } public int getRow() { return row; } - public void setRow(int row) { this.row = row; } - public int getColumn() { return column; } - public void setColumn(int column) { this.column = column; } - + + public void setValues(int row, int column) { + this.row = row; + this.column = column; + + } + @Override public String toString() { return row + ", " + column; - + } - } diff --git a/src/chess/ChessMatch.java b/src/chess/ChessMatch.java index 9f6e2b5..afbe2e7 100644 --- a/src/chess/ChessMatch.java +++ b/src/chess/ChessMatch.java @@ -23,6 +23,12 @@ public ChessPiece[][] getPieces(){ return mat; } + public boolean[][] possibleMoves(ChessPosition sourcePosition){ + Position position = sourcePosition.toPosition(); + validateSourcePosition(position); + return board.piece(position).possibleMoves(); + } + public ChessPiece performChessMove(ChessPosition sourcePosition, ChessPosition targetPosition) { Position source = sourcePosition.toPosition(); Position target = targetPosition.toPosition(); @@ -47,6 +53,12 @@ private void validateSourcePosition(Position position) { } } + private void validateTargetPosition(Position source, Position target) { + if (!board.piece(source).possibleMove(target)){ + throw new ChessException ("the chosen piece can't move to target position"); + } + } + private void placeNewPiece(char column, int row, ChessPiece piece) { board.placePiece(piece, new ChessPosition(column, row).toPosition()); } diff --git a/src/chess/ChessPiece.java b/src/chess/ChessPiece.java index 1aa030f..19599bf 100644 --- a/src/chess/ChessPiece.java +++ b/src/chess/ChessPiece.java @@ -2,9 +2,10 @@ import boardgame.Board; import boardgame.Piece; +import boardgame.Position; + +public abstract class ChessPiece extends Piece { -public abstract class ChessPiece extends Piece{ - private Color color; public ChessPiece(Board board, Color color) { @@ -16,6 +17,10 @@ public Color getColor() { return color; } - + protected boolean isThereOpponentPiece(Position position) { + ChessPiece p = (ChessPiece)getBoard().piece(position); + return p != null && p.getColor() != color; + + } } diff --git a/src/chess/pieces/Rook.java b/src/chess/pieces/Rook.java index 7a76376..54d77dc 100644 --- a/src/chess/pieces/Rook.java +++ b/src/chess/pieces/Rook.java @@ -1,22 +1,84 @@ package chess.pieces; import boardgame.Board; +import boardgame.Position; import chess.ChessPiece; import chess.Color; -public class Rook extends ChessPiece{ +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; } } From 8545504f4be5ffd433b6eb4a84c0708ef2f338f8 Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Tue, 11 Jan 2022 17:11:03 -0300 Subject: [PATCH 11/20] king moves --- src/chess/pieces/King.java | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/chess/pieces/King.java b/src/chess/pieces/King.java index 7401f6c..fee47cf 100644 --- a/src/chess/pieces/King.java +++ b/src/chess/pieces/King.java @@ -1,6 +1,7 @@ package chess.pieces; import boardgame.Board; +import boardgame.Position; import chess.ChessPiece; import chess.Color; @@ -10,13 +11,72 @@ 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; } From be1522a5b355e1931a914196a00328866d8c0dcd Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Wed, 12 Jan 2022 14:14:37 -0300 Subject: [PATCH 12/20] Switching player each turn --- src/application/Program.java | 2 +- src/application/UI.java | 11 ++++++++++- src/chess/ChessMatch.java | 22 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/application/Program.java b/src/application/Program.java index e2ef0db..c09b4a8 100644 --- a/src/application/Program.java +++ b/src/application/Program.java @@ -18,7 +18,7 @@ public static void main(String[] args) { while (true) { try { UI.clearScreen(); - UI.printBoard(chessMatch.getPieces()); + UI.printMatch(chessMatch); System.out.println(); System.out.print("Source: "); ChessPosition source = UI.readChessPosition(sc); diff --git a/src/application/UI.java b/src/application/UI.java index 291aa61..b973324 100644 --- a/src/application/UI.java +++ b/src/application/UI.java @@ -3,6 +3,7 @@ import java.util.InputMismatchException; import java.util.Scanner; +import chess.ChessMatch; import chess.ChessPiece; import chess.ChessPosition; import chess.Color; @@ -45,6 +46,14 @@ public static ChessPosition readChessPosition(Scanner sc) { throw new InputMismatchException("Error reading ChessPosition. Valid values are from a1 to h8"); } } + + public static void printMatch (ChessMatch chessMatch) { + printBoard(chessMatch.getPieces()); + System.out.println(); + System.out.println("turn : " + chessMatch.getTurn()); + System.out.println("Waiting player: " + chessMatch.getCurrentPlayer()); + } + public static void printBoard(ChessPiece[][] pieces) { for (int i = 0; i < pieces.length; i++) { System.out.print((8 - i + " ")); @@ -55,7 +64,7 @@ public static void printBoard(ChessPiece[][] pieces) { } 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 + " ")); diff --git a/src/chess/ChessMatch.java b/src/chess/ChessMatch.java index afbe2e7..35bc260 100644 --- a/src/chess/ChessMatch.java +++ b/src/chess/ChessMatch.java @@ -7,12 +7,25 @@ import chess.pieces.Rook; public class ChessMatch { + private int turn; + private Color currentPlayer; private Board board; public ChessMatch() { board = new Board (8, 8); + turn = 1; + currentPlayer = Color.WHITE; initialSetup(); } + + public int getTurn() { + return turn; + } + public Color getCurrentPlayer() { + return currentPlayer; + } + + public ChessPiece[][] getPieces(){ ChessPiece[][] mat = new ChessPiece[board.getRows()][board.getColumns()]; for (int i=0; i Date: Thu, 27 Jan 2022 13:57:34 -0300 Subject: [PATCH 13/20] Handling captured pieces --- ...03\243o Orientada a Objetos +Projetos.url" | 2 + src/application/Program.java | 8 ++- src/application/UI.java | 56 +++++++++++++------ src/chess/ChessMatch.java | 15 +++++ 4 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 "src/Java COMPLETO Programa\303\247\303\243o Orientada a Objetos +Projetos.url" diff --git "a/src/Java COMPLETO Programa\303\247\303\243o Orientada a Objetos +Projetos.url" "b/src/Java COMPLETO Programa\303\247\303\243o Orientada a Objetos +Projetos.url" new file mode 100644 index 0000000..d65bf94 --- /dev/null +++ "b/src/Java COMPLETO Programa\303\247\303\243o Orientada a Objetos +Projetos.url" @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://www.udemy.com/course/java-curso-completo/ diff --git a/src/application/Program.java b/src/application/Program.java index c09b4a8..b4c6e6b 100644 --- a/src/application/Program.java +++ b/src/application/Program.java @@ -1,6 +1,8 @@ package application; +import java.util.ArrayList; import java.util.InputMismatchException; +import java.util.List; import java.util.Scanner; import chess.ChessException; @@ -14,11 +16,12 @@ public static void main(String[] args) { Scanner sc = new Scanner(System.in); ChessMatch chessMatch = new ChessMatch(); + List captured = new ArrayList<>(); while (true) { try { UI.clearScreen(); - UI.printMatch(chessMatch); + UI.printMatch(chessMatch, captured); System.out.println(); System.out.print("Source: "); ChessPosition source = UI.readChessPosition(sc); @@ -33,6 +36,9 @@ public static void main(String[] args) { ChessPiece capturedPiece = chessMatch.performChessMove(source, target); + if (capturedPiece != null) { + captured.add(capturedPiece); + } } catch(ChessException e) { System.out.println(e.getMessage()); diff --git a/src/application/UI.java b/src/application/UI.java index b973324..e42e8f7 100644 --- a/src/application/UI.java +++ b/src/application/UI.java @@ -1,7 +1,10 @@ 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; @@ -31,29 +34,30 @@ public class UI { // https://stackoverflow.com/questions/2979383/java-clear-the-console public static void clearScreen() { - System.out.print("\033[H\033[2J"); - System.out.flush(); + 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) { + 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) { + + 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()); System.out.println("Waiting player: " + chessMatch.getCurrentPlayer()); } - + public static void printBoard(ChessPiece[][] pieces) { for (int i = 0; i < pieces.length; i++) { System.out.print((8 - i + " ")); @@ -64,8 +68,8 @@ public static void printBoard(ChessPiece[][] pieces) { } System.out.print(" a b c d e f g h "); } - - public static void printBoard(ChessPiece[][] pieces,boolean[][] possibleMoves) { + + 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++) { @@ -75,14 +79,14 @@ public static void printBoard(ChessPiece[][] pieces,boolean[][] possibleMoves) { } System.out.print(" a b c d e f g h "); } - + private static void printPiece(ChessPiece piece, boolean background) { - if(background) { + if (background) { System.out.print(ANSI_BLUE_BACKGROUND); - + } if (piece == null) { - System.out.print("-" + ANSI_RESET ); + System.out.print("-" + ANSI_RESET); } else { if (piece.getColor() == Color.WHITE) { System.out.print(ANSI_WHITE + piece + ANSI_RESET); @@ -92,4 +96,20 @@ private static void printPiece(ChessPiece piece, boolean background) { } 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/chess/ChessMatch.java b/src/chess/ChessMatch.java index 35bc260..edaf6b8 100644 --- a/src/chess/ChessMatch.java +++ b/src/chess/ChessMatch.java @@ -1,5 +1,8 @@ package chess; +import java.util.ArrayList; +import java.util.List; + import boardgame.Board; import boardgame.Piece; import boardgame.Position; @@ -11,6 +14,10 @@ public class ChessMatch { private Color currentPlayer; private Board board; + private List piecesOnTheBoard = new ArrayList<>(); + private List capturedPieces = new ArrayList<>(); + + public ChessMatch() { board = new Board (8, 8); turn = 1; @@ -55,6 +62,13 @@ private Piece makeMove(Position source, Position target) { Piece p = board.removePiece(source); Piece capturedPiece = board.removePiece(target); board.placePiece(p, target); + + if(capturedPiece != null) { + piecesOnTheBoard.remove(capturedPiece); + capturedPieces.add(capturedPiece); + } + + return capturedPiece; } @@ -83,6 +97,7 @@ private void nextTurn() { private void placeNewPiece(char column, int row, ChessPiece piece) { board.placePiece(piece, new ChessPosition(column, row).toPosition()); + piecesOnTheBoard.add(piece); } private void initialSetup() { From 9b5ae99ef230724e8c34ca8c4da2dfe558296ec9 Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Thu, 27 Jan 2022 17:01:34 -0300 Subject: [PATCH 14/20] Check logic --- src/application/UI.java | 3 +++ src/chess/ChessMatch.java | 52 ++++++++++++++++++++++++++++++++++++ src/chess/ChessPiece.java | 4 +++ src/chess/ChessPosition.java | 2 +- 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/application/UI.java b/src/application/UI.java index e42e8f7..9fddd16 100644 --- a/src/application/UI.java +++ b/src/application/UI.java @@ -56,6 +56,9 @@ public static void printMatch(ChessMatch chessMatch, List captured) System.out.println(); System.out.println("turn : " + chessMatch.getTurn()); System.out.println("Waiting player: " + chessMatch.getCurrentPlayer()); + if(chessMatch.getCheck()) { + System.out.println("CHECK!"); + } } public static void printBoard(ChessPiece[][] pieces) { diff --git a/src/chess/ChessMatch.java b/src/chess/ChessMatch.java index edaf6b8..04100db 100644 --- a/src/chess/ChessMatch.java +++ b/src/chess/ChessMatch.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import boardgame.Board; import boardgame.Piece; @@ -13,6 +14,7 @@ public class ChessMatch { private int turn; private Color currentPlayer; private Board board; + private boolean check; private List piecesOnTheBoard = new ArrayList<>(); private List capturedPieces = new ArrayList<>(); @@ -32,6 +34,9 @@ public Color getCurrentPlayer() { return currentPlayer; } + public boolean getCheck() { + return check; + } public ChessPiece[][] getPieces(){ ChessPiece[][] mat = new ChessPiece[board.getRows()][board.getColumns()]; @@ -54,6 +59,14 @@ public ChessPiece performChessMove(ChessPosition sourcePosition, ChessPosition t Position target = targetPosition.toPosition(); validateSourcePosition(source); Piece capturedPiece = makeMove(source, target); + + if(testCheck(currentPlayer)) { + undoMove(source, target, capturedPiece); + throw new ChessException("You can't put yourself in check"); + } + + check = (testCheck(opponent(currentPlayer))) ? true : false; + nextTurn(); return (ChessPiece)capturedPiece; } @@ -72,6 +85,17 @@ private Piece makeMove(Position source, Position target) { return capturedPiece; } + private void undoMove(Position source, Position target, Piece capturedPiece) { + Piece p = board.removePiece(target); + board.placePiece(p, source); + + if(capturedPiece != null) { + board.placePiece(capturedPiece, target); + capturedPieces.remove(capturedPiece); + piecesOnTheBoard.add(capturedPiece); + } + } + private void validateSourcePosition(Position position) { if (!board.thereIsAPiece(position)) { throw new ChessException("There is no piece on source position"); @@ -95,6 +119,34 @@ private void nextTurn() { currentPlayer = (currentPlayer == Color.WHITE) ? Color.BLACK : Color.WHITE; } + private Color opponent(Color color ) { + return (color == color.WHITE) ? Color.BLACK : Color.WHITE; + } + + private ChessPiece King(Color color) { + List 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 void placeNewPiece(char column, int row, ChessPiece piece) { board.placePiece(piece, new ChessPosition(column, row).toPosition()); piecesOnTheBoard.add(piece); diff --git a/src/chess/ChessPiece.java b/src/chess/ChessPiece.java index 19599bf..bda156a 100644 --- a/src/chess/ChessPiece.java +++ b/src/chess/ChessPiece.java @@ -16,6 +16,10 @@ public ChessPiece(Board board, Color color) { public Color getColor() { return color; } + + public ChessPosition getChessPosition() { + return ChessPosition.fromPosition(position); + } protected boolean isThereOpponentPiece(Position position) { ChessPiece p = (ChessPiece)getBoard().piece(position); diff --git a/src/chess/ChessPosition.java b/src/chess/ChessPosition.java index 122b38f..8788481 100644 --- a/src/chess/ChessPosition.java +++ b/src/chess/ChessPosition.java @@ -31,7 +31,7 @@ protected Position toPosition() { } protected static ChessPosition fromPosition(Position position) { - return new ChessPosition ((char)('a' - position.getColumn()),8 - position.getRow()); + return new ChessPosition ((char)('a' + position.getColumn()),8 - position.getRow()); } @Override public String toString() { From 6e32fb1839127ca5d07e86796ff2a5272481063e Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Wed, 9 Feb 2022 17:49:06 -0300 Subject: [PATCH 15/20] Checkmate logic --- src/application/Program.java | 29 ++++++++++--------- src/application/UI.java | 25 ++++++++++------ src/chess/ChessMatch.java | 56 ++++++++++++++++++++++++++---------- 3 files changed, 72 insertions(+), 38 deletions(-) diff --git a/src/application/Program.java b/src/application/Program.java index b4c6e6b..b1ac2ad 100644 --- a/src/application/Program.java +++ b/src/application/Program.java @@ -13,42 +13,43 @@ public class Program { public static void main(String[] args) { - + Scanner sc = new Scanner(System.in); ChessMatch chessMatch = new ChessMatch(); List captured = new ArrayList<>(); - - while (true) { + + 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) { + } catch (ChessException e) { System.out.println(e.getMessage()); sc.nextLine(); - } - catch(InputMismatchException e) { + } 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 index 9fddd16..688e1fd 100644 --- a/src/application/UI.java +++ b/src/application/UI.java @@ -55,9 +55,14 @@ public static void printMatch(ChessMatch chessMatch, List captured) printCapturedPieces(captured); System.out.println(); System.out.println("turn : " + chessMatch.getTurn()); - System.out.println("Waiting player: " + chessMatch.getCurrentPlayer()); - if(chessMatch.getCheck()) { - System.out.println("CHECK!"); + 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()); } } @@ -99,20 +104,22 @@ private static void printPiece(ChessPiece piece, boolean background) { } 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()); + 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.print("Black:" + black); System.out.println(ANSI_BLACK); System.out.println(Arrays.toString(black.toArray())); System.out.println(ANSI_RESET); - + } - + } diff --git a/src/chess/ChessMatch.java b/src/chess/ChessMatch.java index 04100db..c88c9fa 100644 --- a/src/chess/ChessMatch.java +++ b/src/chess/ChessMatch.java @@ -15,6 +15,7 @@ public class ChessMatch { private Color currentPlayer; private Board board; private boolean check; + private boolean checkMate; private List piecesOnTheBoard = new ArrayList<>(); private List capturedPieces = new ArrayList<>(); @@ -37,6 +38,9 @@ public Color getCurrentPlayer() { public boolean getCheck() { return check; } + public boolean getCheckMate() { + return checkMate; + } public ChessPiece[][] getPieces(){ ChessPiece[][] mat = new ChessPiece[board.getRows()][board.getColumns()]; @@ -67,7 +71,12 @@ public ChessPiece performChessMove(ChessPosition sourcePosition, ChessPosition t check = (testCheck(opponent(currentPlayer))) ? true : false; - nextTurn(); + if(testCheckMate(opponent(currentPlayer))) { + checkMate = true; + }else { + nextTurn(); + } + return (ChessPiece)capturedPiece; } @@ -145,6 +154,31 @@ private boolean testCheck(Color color) { 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 Date: Wed, 9 Feb 2022 18:03:51 -0300 Subject: [PATCH 16/20] Piece Move count --- src/chess/ChessMatch.java | 6 ++++-- src/chess/ChessPiece.java | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/chess/ChessMatch.java b/src/chess/ChessMatch.java index c88c9fa..6d98558 100644 --- a/src/chess/ChessMatch.java +++ b/src/chess/ChessMatch.java @@ -81,7 +81,8 @@ public ChessPiece performChessMove(ChessPosition sourcePosition, ChessPosition t } private Piece makeMove(Position source, Position target) { - Piece p = board.removePiece(source); + ChessPiece p = (ChessPiece)board.removePiece(source); + p.increaseMoveCount(); Piece capturedPiece = board.removePiece(target); board.placePiece(p, target); @@ -95,7 +96,8 @@ private Piece makeMove(Position source, Position target) { } private void undoMove(Position source, Position target, Piece capturedPiece) { - Piece p = board.removePiece(target); + ChessPiece p = (ChessPiece)board.removePiece(target); + p.decreaseMoveCount(); board.placePiece(p, source); if(capturedPiece != null) { diff --git a/src/chess/ChessPiece.java b/src/chess/ChessPiece.java index bda156a..673f2f9 100644 --- a/src/chess/ChessPiece.java +++ b/src/chess/ChessPiece.java @@ -7,6 +7,7 @@ public abstract class ChessPiece extends Piece { private Color color; + private int moveCount; public ChessPiece(Board board, Color color) { super(board); @@ -16,6 +17,17 @@ public ChessPiece(Board board, Color color) { public Color getColor() { return color; } + public int getMoveCount() { + return moveCount; + } + + public void increaseMoveCount() { + moveCount++; + } + + public void decreaseMoveCount() { + moveCount--; + } public ChessPosition getChessPosition() { return ChessPosition.fromPosition(position); From 03fe5b518e5352d6dacc565859aace70e9499322 Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Wed, 9 Feb 2022 20:01:43 -0300 Subject: [PATCH 17/20] Pawn --- ...03\243o Orientada a Objetos +Projetos.url" | 2 - src/chess/ChessMatch.java | 26 ++++++- src/chess/pieces/Pawn.java | 68 +++++++++++++++++++ 3 files changed, 91 insertions(+), 5 deletions(-) delete mode 100644 "src/Java COMPLETO Programa\303\247\303\243o Orientada a Objetos +Projetos.url" create mode 100644 src/chess/pieces/Pawn.java diff --git "a/src/Java COMPLETO Programa\303\247\303\243o Orientada a Objetos +Projetos.url" "b/src/Java COMPLETO Programa\303\247\303\243o Orientada a Objetos +Projetos.url" deleted file mode 100644 index d65bf94..0000000 --- "a/src/Java COMPLETO Programa\303\247\303\243o Orientada a Objetos +Projetos.url" +++ /dev/null @@ -1,2 +0,0 @@ -[InternetShortcut] -URL=https://www.udemy.com/course/java-curso-completo/ diff --git a/src/chess/ChessMatch.java b/src/chess/ChessMatch.java index 6d98558..d5ff5f2 100644 --- a/src/chess/ChessMatch.java +++ b/src/chess/ChessMatch.java @@ -8,6 +8,7 @@ import boardgame.Piece; import boardgame.Position; import chess.pieces.King; +import chess.pieces.Pawn; import chess.pieces.Rook; public class ChessMatch { @@ -189,11 +190,30 @@ private void placeNewPiece(char column, int row, ChessPiece piece) { } private void initialSetup() { - placeNewPiece('h', 7, new Rook(board, Color.WHITE)); - placeNewPiece('d', 1, new Rook(board, Color.WHITE)); + placeNewPiece('a', 1, new Rook(board, Color.WHITE)); placeNewPiece('e', 1, new King(board, Color.WHITE)); + placeNewPiece('h', 1, new Rook(board, Color.WHITE)); + placeNewPiece('a', 2, new Pawn(board, Color.WHITE)); + placeNewPiece('b', 2, new Pawn(board, Color.WHITE)); + placeNewPiece('c', 2, new Pawn(board, Color.WHITE)); + placeNewPiece('d', 2, new Pawn(board, Color.WHITE)); + placeNewPiece('e', 2, new Pawn(board, Color.WHITE)); + placeNewPiece('f', 2, new Pawn(board, Color.WHITE)); + placeNewPiece('g', 2, new Pawn(board, Color.WHITE)); + placeNewPiece('h', 2, new Pawn(board, Color.WHITE)); + + - placeNewPiece('b', 8, new Rook(board, Color.BLACK)); placeNewPiece('a', 8, new Rook(board, Color.BLACK)); + placeNewPiece('e', 8, new King(board, Color.BLACK)); + placeNewPiece('h', 8, new Rook(board, Color.BLACK)); + placeNewPiece('a', 7, new Pawn(board, Color.BLACK)); + placeNewPiece('b', 7, new Pawn(board, Color.BLACK)); + placeNewPiece('c', 7, new Pawn(board, Color.BLACK)); + placeNewPiece('d', 7, new Pawn(board, Color.BLACK)); + placeNewPiece('e', 7, new Pawn(board, Color.BLACK)); + placeNewPiece('f', 7, new Pawn(board, Color.BLACK)); + placeNewPiece('g', 7, new Pawn(board, Color.BLACK)); + placeNewPiece('h', 7, new Pawn(board, Color.BLACK)); } } 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"; + } +} From ba2e37a54b25e7b816bda84e39efabef3ba87db1 Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Mon, 14 Feb 2022 17:06:26 -0300 Subject: [PATCH 18/20] Bishop --- src/chess/ChessMatch.java | 5 +++ src/chess/pieces/Bishop.java | 84 ++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/chess/pieces/Bishop.java diff --git a/src/chess/ChessMatch.java b/src/chess/ChessMatch.java index d5ff5f2..caa2fe8 100644 --- a/src/chess/ChessMatch.java +++ b/src/chess/ChessMatch.java @@ -7,6 +7,7 @@ import boardgame.Board; import boardgame.Piece; import boardgame.Position; +import chess.pieces.Bishop; import chess.pieces.King; import chess.pieces.Pawn; import chess.pieces.Rook; @@ -191,8 +192,10 @@ private void placeNewPiece(char column, int row, ChessPiece piece) { private void initialSetup() { placeNewPiece('a', 1, new Rook(board, Color.WHITE)); + placeNewPiece('c', 1, new Bishop(board, Color.WHITE)); placeNewPiece('e', 1, new King(board, Color.WHITE)); placeNewPiece('h', 1, new Rook(board, Color.WHITE)); + placeNewPiece('f', 1, new Bishop(board, Color.WHITE)); placeNewPiece('a', 2, new Pawn(board, Color.WHITE)); placeNewPiece('b', 2, new Pawn(board, Color.WHITE)); placeNewPiece('c', 2, new Pawn(board, Color.WHITE)); @@ -205,7 +208,9 @@ private void initialSetup() { placeNewPiece('a', 8, new Rook(board, Color.BLACK)); + placeNewPiece('c', 8, new Bishop(board, Color.BLACK)); placeNewPiece('e', 8, new King(board, Color.BLACK)); + placeNewPiece('f', 8, new Bishop(board, Color.BLACK)); placeNewPiece('h', 8, new Rook(board, Color.BLACK)); placeNewPiece('a', 7, new Pawn(board, Color.BLACK)); placeNewPiece('b', 7, new Pawn(board, Color.BLACK)); 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; + } +} From 92c1b2cf13b283ea76c01ea83717835293207131 Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Mon, 14 Feb 2022 17:32:06 -0300 Subject: [PATCH 19/20] Knight --- src/chess/ChessMatch.java | 7 ++- src/chess/pieces/Knight.java | 83 ++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/chess/pieces/Knight.java diff --git a/src/chess/ChessMatch.java b/src/chess/ChessMatch.java index caa2fe8..16909c0 100644 --- a/src/chess/ChessMatch.java +++ b/src/chess/ChessMatch.java @@ -9,6 +9,7 @@ import boardgame.Position; import chess.pieces.Bishop; import chess.pieces.King; +import chess.pieces.Knight; import chess.pieces.Pawn; import chess.pieces.Rook; @@ -192,10 +193,12 @@ private void placeNewPiece(char column, int row, ChessPiece piece) { private void initialSetup() { placeNewPiece('a', 1, new Rook(board, Color.WHITE)); + placeNewPiece('b', 1, new Knight(board, Color.WHITE)); placeNewPiece('c', 1, new Bishop(board, Color.WHITE)); placeNewPiece('e', 1, new King(board, Color.WHITE)); - placeNewPiece('h', 1, new Rook(board, Color.WHITE)); placeNewPiece('f', 1, new Bishop(board, Color.WHITE)); + placeNewPiece('g', 1, new Knight(board, Color.WHITE)); + placeNewPiece('h', 1, new Rook(board, Color.WHITE)); placeNewPiece('a', 2, new Pawn(board, Color.WHITE)); placeNewPiece('b', 2, new Pawn(board, Color.WHITE)); placeNewPiece('c', 2, new Pawn(board, Color.WHITE)); @@ -208,9 +211,11 @@ private void initialSetup() { placeNewPiece('a', 8, new Rook(board, Color.BLACK)); + placeNewPiece('b', 8, new Knight(board, Color.BLACK)); placeNewPiece('c', 8, new Bishop(board, Color.BLACK)); placeNewPiece('e', 8, new King(board, Color.BLACK)); placeNewPiece('f', 8, new Bishop(board, Color.BLACK)); + placeNewPiece('g', 8, new Knight(board, Color.BLACK)); placeNewPiece('h', 8, new Rook(board, Color.BLACK)); placeNewPiece('a', 7, new Pawn(board, Color.BLACK)); placeNewPiece('b', 7, new Pawn(board, Color.BLACK)); 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; + } + +} From dfd0320c0fdcf400184443da35dcd93750c53386 Mon Sep 17 00:00:00 2001 From: Gabriel Cardoso Date: Mon, 14 Feb 2022 17:44:56 -0300 Subject: [PATCH 20/20] Queen --- src/chess/ChessMatch.java | 3 + src/chess/pieces/Queen.java | 140 ++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 src/chess/pieces/Queen.java diff --git a/src/chess/ChessMatch.java b/src/chess/ChessMatch.java index 16909c0..ddfa642 100644 --- a/src/chess/ChessMatch.java +++ b/src/chess/ChessMatch.java @@ -11,6 +11,7 @@ import chess.pieces.King; import chess.pieces.Knight; import chess.pieces.Pawn; +import chess.pieces.Queen; import chess.pieces.Rook; public class ChessMatch { @@ -195,6 +196,7 @@ private void initialSetup() { placeNewPiece('a', 1, new Rook(board, Color.WHITE)); placeNewPiece('b', 1, new Knight(board, Color.WHITE)); placeNewPiece('c', 1, new Bishop(board, Color.WHITE)); + placeNewPiece('d', 1, new Queen(board, Color.WHITE)); placeNewPiece('e', 1, new King(board, Color.WHITE)); placeNewPiece('f', 1, new Bishop(board, Color.WHITE)); placeNewPiece('g', 1, new Knight(board, Color.WHITE)); @@ -213,6 +215,7 @@ private void initialSetup() { placeNewPiece('a', 8, new Rook(board, Color.BLACK)); placeNewPiece('b', 8, new Knight(board, Color.BLACK)); placeNewPiece('c', 8, new Bishop(board, Color.BLACK)); + placeNewPiece('d', 8, new Queen(board, Color.BLACK)); placeNewPiece('e', 8, new King(board, Color.BLACK)); placeNewPiece('f', 8, new Bishop(board, Color.BLACK)); placeNewPiece('g', 8, new Knight(board, Color.BLACK)); 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; + } +}