From 8612b7e3477dfac31961c98b8927f43861ddaa34 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Sat, 29 Jan 2022 19:28:30 +0100 Subject: [PATCH 01/23] Added ChessFigure class with getSymbol method and test --- src/main/java/Game/ChessObj/ChessFigure.java | 64 +++++++++++++++++++ .../java/Game/ChessObj/ChessFigureTest.java | 34 ++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/main/java/Game/ChessObj/ChessFigure.java create mode 100644 src/test/java/Game/ChessObj/ChessFigureTest.java diff --git a/src/main/java/Game/ChessObj/ChessFigure.java b/src/main/java/Game/ChessObj/ChessFigure.java new file mode 100644 index 0000000..4b097c2 --- /dev/null +++ b/src/main/java/Game/ChessObj/ChessFigure.java @@ -0,0 +1,64 @@ +package Game.ChessObj; + +public class ChessFigure { + + public enum Type { + KING, + QUEEN, + CASTLE, + BISHOP, + KNIGHT, + PAWN + } + + public enum Team { + WHITE, + BLACK + } + + private final Type type; + private final Team team; + + public ChessFigure(Type type, Team team) { + this.type = type; + this.team = team; + } + + public Type getType() { + return this.type; + } + + public Team getTeam() { + return this.team; + } + + public String getSymbol() { + String symbol = ""; + switch(getType()) { + case KING: + symbol = "K"; + break; + case QUEEN: + symbol = "Q"; + break; + case CASTLE: + symbol = "T"; + break; + case BISHOP: + symbol = "I"; + break; + case KNIGHT: + symbol = "Z"; + break; + case PAWN: + symbol = "o"; + break; + default: + } + + symbol = ((this.getTeam()==Team.WHITE)?" ":"|") + symbol + ((this.getTeam()==Team.WHITE)?" ":"|"); + + return symbol; + } + +} diff --git a/src/test/java/Game/ChessObj/ChessFigureTest.java b/src/test/java/Game/ChessObj/ChessFigureTest.java new file mode 100644 index 0000000..384d3f4 --- /dev/null +++ b/src/test/java/Game/ChessObj/ChessFigureTest.java @@ -0,0 +1,34 @@ +package Game.ChessObj; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ChessFigureTest { + + @BeforeEach + void setUp() { + } + + @AfterEach + void tearDown() { + } + + @Test + void getSymbol() { + assertEquals(" K ", new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE).getSymbol()); + assertEquals(" Q ", new ChessFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.WHITE).getSymbol()); + assertEquals(" I ", new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE).getSymbol()); + assertEquals(" T ", new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE).getSymbol()); + assertEquals(" Z ", new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE).getSymbol()); + assertEquals(" o ", new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE).getSymbol()); + assertEquals("|K|", new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK).getSymbol()); + assertEquals("|Q|", new ChessFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.BLACK).getSymbol()); + assertEquals("|I|", new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.BLACK).getSymbol()); + assertEquals("|T|", new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.BLACK).getSymbol()); + assertEquals("|Z|", new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.BLACK).getSymbol()); + assertEquals("|o|", new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK).getSymbol()); + } +} \ No newline at end of file From c6c9940d8d27c91d73365f2a757096c2f45c7043 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Sat, 29 Jan 2022 19:54:00 +0100 Subject: [PATCH 02/23] Added equals method to ChessFigure with test --- src/main/java/Game/ChessObj/ChessFigure.java | 19 ++++++++++++++++--- .../java/Game/ChessObj/ChessFigureTest.java | 12 ++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/Game/ChessObj/ChessFigure.java b/src/main/java/Game/ChessObj/ChessFigure.java index 4b097c2..efa4d6e 100644 --- a/src/main/java/Game/ChessObj/ChessFigure.java +++ b/src/main/java/Game/ChessObj/ChessFigure.java @@ -34,7 +34,7 @@ public class ChessFigure { public String getSymbol() { String symbol = ""; - switch(getType()) { + switch (getType()) { case KING: symbol = "K"; break; @@ -56,9 +56,22 @@ public class ChessFigure { default: } - symbol = ((this.getTeam()==Team.WHITE)?" ":"|") + symbol + ((this.getTeam()==Team.WHITE)?" ":"|"); + symbol = ((this.getTeam() == Team.WHITE) ? " " : "|") + symbol + ((this.getTeam() == Team.WHITE) ? " " : "|"); - return symbol; + return symbol; + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof ChessFigure)) { + return false; + } + + ChessFigure x = (ChessFigure) o; + if (this.getType() != x.getType() || this.getTeam() != x.getTeam()) + return false; + + return true; } } diff --git a/src/test/java/Game/ChessObj/ChessFigureTest.java b/src/test/java/Game/ChessObj/ChessFigureTest.java index 384d3f4..866eb12 100644 --- a/src/test/java/Game/ChessObj/ChessFigureTest.java +++ b/src/test/java/Game/ChessObj/ChessFigureTest.java @@ -31,4 +31,16 @@ class ChessFigureTest { assertEquals("|Z|", new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.BLACK).getSymbol()); assertEquals("|o|", new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK).getSymbol()); } + + @Test + void testEquals() { + ChessFigure kw1 = new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE); + ChessFigure kw2 = new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE); + ChessFigure kb1 = new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK); + ChessFigure pw1 = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE); + + assertEquals(kw1, kw2); + assertNotEquals(kw2, kb1); + assertNotEquals(pw1, kb1); + } } \ No newline at end of file From 51baf2b06e96358cc735eff436ee324afd1851ff Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Sat, 29 Jan 2022 20:07:21 +0100 Subject: [PATCH 03/23] Added initBoard for ChessFigure placement and test --- src/main/java/Game/ChessObj/ChessBoard.java | 30 +++++++++++ .../java/Game/ChessObj/ChessBoardTest.java | 54 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/main/java/Game/ChessObj/ChessBoard.java create mode 100644 src/test/java/Game/ChessObj/ChessBoardTest.java diff --git a/src/main/java/Game/ChessObj/ChessBoard.java b/src/main/java/Game/ChessObj/ChessBoard.java new file mode 100644 index 0000000..4a6abef --- /dev/null +++ b/src/main/java/Game/ChessObj/ChessBoard.java @@ -0,0 +1,30 @@ +package Game.ChessObj; + +public class ChessBoard { + + private ChessFigure[][] board; + + public ChessBoard() { + board = new ChessFigure[8][8]; + initBoard(); + } + + protected void initBoard() { + ChessFigure.Type[] order = { + ChessFigure.Type.CASTLE, ChessFigure.Type.KNIGHT, ChessFigure.Type.BISHOP, ChessFigure.Type.QUEEN, ChessFigure.Type.KING, ChessFigure.Type.BISHOP, ChessFigure.Type.KNIGHT, ChessFigure.Type.CASTLE + }; + for (int i = 0; i < board.length; i++) { + //sets all pawns + board[i][1] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK); + board[i][6] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE); + + //sets all others + board[i][0] = new ChessFigure(order[i], ChessFigure.Team.BLACK); + board[i][7] = new ChessFigure(order[i], ChessFigure.Team.WHITE); + } + } + + public ChessFigure[][] getBoard() { + return this.board; + } +} diff --git a/src/test/java/Game/ChessObj/ChessBoardTest.java b/src/test/java/Game/ChessObj/ChessBoardTest.java new file mode 100644 index 0000000..ca744d5 --- /dev/null +++ b/src/test/java/Game/ChessObj/ChessBoardTest.java @@ -0,0 +1,54 @@ +package Game.ChessObj; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ChessBoardTest { + + ChessFigure.Type[] startOrder = { + ChessFigure.Type.CASTLE, ChessFigure.Type.KNIGHT, ChessFigure.Type.BISHOP, ChessFigure.Type.QUEEN, ChessFigure.Type.KING, ChessFigure.Type.BISHOP, ChessFigure.Type.KNIGHT, ChessFigure.Type.CASTLE + }; + + ChessBoard chessBoard; + + @BeforeEach + void setUp() { + chessBoard = new ChessBoard(); + } + + @AfterEach + void tearDown() { + } + + @Test + void initBoard() { + ChessFigure[][] board = chessBoard.getBoard(); + for (int x = 0; x < board.length; x++) { + for (int y = 0; y < board[0].length; y++) { + //test pawns + if (y == 1) { + assertEquals(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK), board[x][y]); + continue; + } + if(y == 6) { + assertEquals(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE), board[x][y]); + continue; + } + + if(y == 0){ + assertEquals(new ChessFigure(startOrder[x], ChessFigure.Team.BLACK), board[x][y]); + continue; + } + if(y == 7){ + assertEquals(new ChessFigure(startOrder[x], ChessFigure.Team.WHITE), board[x][y]); + continue; + } + //rest should be empty(null) + assertNull(board[x][y]); + } + } + } +} \ No newline at end of file From d76ba0e64b1b33e568f005c5fac5ad7a38d15501 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Sun, 30 Jan 2022 01:21:58 +0100 Subject: [PATCH 04/23] Refactored initBoard x,y switch for better use --- src/main/java/Game/ChessObj/ChessBoard.java | 10 +++++----- src/test/java/Game/ChessObj/ChessBoardTest.java | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/Game/ChessObj/ChessBoard.java b/src/main/java/Game/ChessObj/ChessBoard.java index 4a6abef..0b327c7 100644 --- a/src/main/java/Game/ChessObj/ChessBoard.java +++ b/src/main/java/Game/ChessObj/ChessBoard.java @@ -13,14 +13,14 @@ public class ChessBoard { ChessFigure.Type[] order = { ChessFigure.Type.CASTLE, ChessFigure.Type.KNIGHT, ChessFigure.Type.BISHOP, ChessFigure.Type.QUEEN, ChessFigure.Type.KING, ChessFigure.Type.BISHOP, ChessFigure.Type.KNIGHT, ChessFigure.Type.CASTLE }; - for (int i = 0; i < board.length; i++) { + for (int x = 0; x < board[0].length; x++) { //sets all pawns - board[i][1] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK); - board[i][6] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE); + board[1][x] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK); + board[6][x] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE); //sets all others - board[i][0] = new ChessFigure(order[i], ChessFigure.Team.BLACK); - board[i][7] = new ChessFigure(order[i], ChessFigure.Team.WHITE); + board[0][x] = new ChessFigure(order[x], ChessFigure.Team.BLACK); + board[7][x] = new ChessFigure(order[x], ChessFigure.Team.WHITE); } } diff --git a/src/test/java/Game/ChessObj/ChessBoardTest.java b/src/test/java/Game/ChessObj/ChessBoardTest.java index ca744d5..8c90997 100644 --- a/src/test/java/Game/ChessObj/ChessBoardTest.java +++ b/src/test/java/Game/ChessObj/ChessBoardTest.java @@ -26,28 +26,28 @@ class ChessBoardTest { @Test void initBoard() { ChessFigure[][] board = chessBoard.getBoard(); - for (int x = 0; x < board.length; x++) { - for (int y = 0; y < board[0].length; y++) { + for (int x = 0; x < board[0].length; x++) { + for (int y = 0; y < board.length; y++) { //test pawns if (y == 1) { - assertEquals(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK), board[x][y]); + assertEquals(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK), board[y][x]); continue; } if(y == 6) { - assertEquals(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE), board[x][y]); + assertEquals(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE), board[y][x]); continue; } if(y == 0){ - assertEquals(new ChessFigure(startOrder[x], ChessFigure.Team.BLACK), board[x][y]); + assertEquals(new ChessFigure(startOrder[x], ChessFigure.Team.BLACK), board[y][x]); continue; } if(y == 7){ - assertEquals(new ChessFigure(startOrder[x], ChessFigure.Team.WHITE), board[x][y]); + assertEquals(new ChessFigure(startOrder[x], ChessFigure.Team.WHITE), board[y][x]); continue; } //rest should be empty(null) - assertNull(board[x][y]); + assertNull(board[y][x]); } } } From 0707f2d628646fbdec651013404926d9724e2cec Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Sun, 30 Jan 2022 01:45:05 +0100 Subject: [PATCH 05/23] Added getOutput method and helper method getCellSymbols with test --- src/main/java/Game/ChessObj/ChessBoard.java | 52 +++++++++++++++++++ .../java/Game/ChessObj/ChessBoardTest.java | 15 ++++++ 2 files changed, 67 insertions(+) diff --git a/src/main/java/Game/ChessObj/ChessBoard.java b/src/main/java/Game/ChessObj/ChessBoard.java index 0b327c7..0099da5 100644 --- a/src/main/java/Game/ChessObj/ChessBoard.java +++ b/src/main/java/Game/ChessObj/ChessBoard.java @@ -1,5 +1,7 @@ package Game.ChessObj; +import java.util.ArrayList; + public class ChessBoard { private ChessFigure[][] board; @@ -24,6 +26,56 @@ public class ChessBoard { } } + /* + a b c d e f g h + ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ + 1 │ |T| │ |Z| │ |I| │ |Q| │ |K| │ |I| │ |Z| │ |T| │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 2 │ |o| │ |o| │ |o| │ |o| │ |o| │ |o| │ |o| │ |o| │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 3 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 4 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 5 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 6 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 7 │ o │ o │ o │ o │ o │ o │ o │ o │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 8 │ T │ Z │ I │ Q │ K │ I │ Z │ T │ + └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘ + */ + + public ArrayList getOutputBoard() { + ArrayList outputBoard = new ArrayList<>(); + String[][] cellSymbol = getCellSymbols(); + outputBoard.add(" a b c d e f g h"); + outputBoard.add(" ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐"); + for (int y = 0; y < board.length; y++) { + String line = ""; + line += " " + (y + 1) + " │ "; + for (int x = 0; x < board[0].length; x++) { + line += (cellSymbol[y][x] + " │ "); + } + outputBoard.add(line); + if (y < 7) + outputBoard.add(" ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤"); + } + outputBoard.add(" └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘"); + return outputBoard; + } + + public String[][] getCellSymbols() { + String[][] cellSymbol = new String[8][8]; + for (int y = 0; y < board.length; y++) { + for (int x = 0; x < board[0].length; x++) { + cellSymbol[y][x] = (board[y][x] == null)?" ": board[y][x].getSymbol(); + } + } + return cellSymbol; + } + public ChessFigure[][] getBoard() { return this.board; } diff --git a/src/test/java/Game/ChessObj/ChessBoardTest.java b/src/test/java/Game/ChessObj/ChessBoardTest.java index 8c90997..d39bb27 100644 --- a/src/test/java/Game/ChessObj/ChessBoardTest.java +++ b/src/test/java/Game/ChessObj/ChessBoardTest.java @@ -51,4 +51,19 @@ class ChessBoardTest { } } } + + @Test + void getCellSymbols() { + String[][] expectedArray = { + {"|T|","|Z|","|I|","|Q|","|K|","|I|","|Z|","|T|"}, + {"|o|","|o|","|o|","|o|","|o|","|o|","|o|","|o|"}, + {" "," "," "," "," "," "," "," "}, + {" "," "," "," "," "," "," "," "}, + {" "," "," "," "," "," "," "," "}, + {" "," "," "," "," "," "," "," "}, + {" o "," o "," o "," o "," o "," o "," o "," o "}, + {" T "," Z "," I "," Q "," K "," I "," Z "," T "} + }; + assertArrayEquals(expectedArray, chessBoard.getCellSymbols()); + } } \ No newline at end of file From 161cf9bb5b07eceb30c0d6acd37c3045f087e399 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Sun, 30 Jan 2022 12:28:42 +0100 Subject: [PATCH 06/23] Added isRelativeMoveValid for checking theoretical movement of figures --- src/main/java/Game/ChessObj/ChessFigure.java | 38 +++++++++++ .../java/Game/ChessObj/ChessFigureTest.java | 65 +++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/src/main/java/Game/ChessObj/ChessFigure.java b/src/main/java/Game/ChessObj/ChessFigure.java index efa4d6e..878c66e 100644 --- a/src/main/java/Game/ChessObj/ChessFigure.java +++ b/src/main/java/Game/ChessObj/ChessFigure.java @@ -61,6 +61,44 @@ public class ChessFigure { return symbol; } + public boolean isRelativeMoveValid(int dx, int dy) { + if (dx == 0 && dy == 0) + return false; + switch (getType()) { + case KING: + if (Math.abs(dx) == 1 && Math.abs(dy) == 1) + return true; + break; + case QUEEN: + if ((dx == dy) || (dx == 0 ^ dy == 0)) + return true; + break; + case CASTLE: + if (dx == 0 ^ dy == 0) + return true; + break; + case BISHOP: + if (dx == dy) + return true; + break; + case KNIGHT: + if ((dy == 2 && (dx == -1 || dx == 1)) || (dy == -2 && (dx == -1 || dx == 1)) || (dx == 2 && (dy == -1 || dy == 1)) || (dx == -2 && (dy == -1 || dy == 1))) + return true; + break; + case PAWN: + if (dx == 0) + return false; + if (getTeam() == Team.WHITE && (dy == 1)) + return true; + if (getTeam() == Team.BLACK && (dy == -1)) + return true; + break; + default: + } + + return false; + } + @Override public boolean equals(Object o) { if (!(o instanceof ChessFigure)) { diff --git a/src/test/java/Game/ChessObj/ChessFigureTest.java b/src/test/java/Game/ChessObj/ChessFigureTest.java index 866eb12..8e5235a 100644 --- a/src/test/java/Game/ChessObj/ChessFigureTest.java +++ b/src/test/java/Game/ChessObj/ChessFigureTest.java @@ -4,6 +4,8 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.ArrayList; + import static org.junit.jupiter.api.Assertions.*; class ChessFigureTest { @@ -43,4 +45,67 @@ class ChessFigureTest { assertNotEquals(kw2, kb1); assertNotEquals(pw1, kb1); } + + @Test + void isRelativeMoveValid() { + ArrayList array = new ArrayList<>(); + for (ChessFigure.Type type : ChessFigure.Type.values()) + for (ChessFigure.Team team : ChessFigure.Team.values()) + array.add(new ChessFigure(type, team)); + + for (ChessFigure figure : array) { + for (int x = -4; x <= 4; x++) { + for (int y = -4; y <= 4; y++) { + if (x == 0 && y == 0) { + assertFalse(figure.isRelativeMoveValid(x, y)); + continue; + } + switch (figure.getType()) { + case KING: + if (x * x == 1 && y * y == 1) { + assertTrue(figure.isRelativeMoveValid(x, y)); + continue; + } + break; + case QUEEN: + if ((x == y) || (x == 0 ^ y == 0)) { + assertTrue(figure.isRelativeMoveValid(x, y)); + continue; + } + break; + case CASTLE: + if (x == 0 ^ y == 0) { + assertTrue(figure.isRelativeMoveValid(x, y)); + continue; + } + break; + case BISHOP: + if (x == y) { + assertTrue(figure.isRelativeMoveValid(x, y)); + continue; + } + break; + case KNIGHT: + if ((y == 2 && (x == -1 || x == 1)) || (y == -2 && (x == -1 || x == 1)) || (x == 2 && (y == -1 || y == 1)) || (x == -2 && (y == -1 || y == 1))) { + assertTrue(figure.isRelativeMoveValid(x, y)); + continue; + } + break; + case PAWN: + if (figure.getTeam() == ChessFigure.Team.WHITE && (y == 1) && x != 0) { + assertTrue(figure.isRelativeMoveValid(x, y)); + continue; + } + if (figure.getTeam() == ChessFigure.Team.BLACK && (y == -1) && x != 0) { + assertTrue(figure.isRelativeMoveValid(x, y)); + continue; + } + break; + default: + } + assertFalse(figure.isRelativeMoveValid(x, y), "Type: " + figure.getType() + " X: " + x + " Y: " + y + " should be false"); + } + } + } + } } \ No newline at end of file From b7125715f3473378fb83ed936df83f122422ce5e Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Sun, 30 Jan 2022 12:41:43 +0100 Subject: [PATCH 07/23] Added isCellInBoard for checking positions with test --- src/main/java/Game/ChessObj/ChessBoard.java | 8 +++++++- src/test/java/Game/ChessObj/ChessBoardTest.java | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/Game/ChessObj/ChessBoard.java b/src/main/java/Game/ChessObj/ChessBoard.java index 0099da5..8b32b3c 100644 --- a/src/main/java/Game/ChessObj/ChessBoard.java +++ b/src/main/java/Game/ChessObj/ChessBoard.java @@ -70,12 +70,18 @@ public class ChessBoard { String[][] cellSymbol = new String[8][8]; for (int y = 0; y < board.length; y++) { for (int x = 0; x < board[0].length; x++) { - cellSymbol[y][x] = (board[y][x] == null)?" ": board[y][x].getSymbol(); + cellSymbol[y][x] = (board[y][x] == null) ? " " : board[y][x].getSymbol(); } } return cellSymbol; } + public boolean isCellInBoard(int x, int y){ + if(x >= 0 && x < board[0].length && y >= 0 && y < board.length) + return true; + return false; + } + public ChessFigure[][] getBoard() { return this.board; } diff --git a/src/test/java/Game/ChessObj/ChessBoardTest.java b/src/test/java/Game/ChessObj/ChessBoardTest.java index d39bb27..0053e9f 100644 --- a/src/test/java/Game/ChessObj/ChessBoardTest.java +++ b/src/test/java/Game/ChessObj/ChessBoardTest.java @@ -66,4 +66,15 @@ class ChessBoardTest { }; assertArrayEquals(expectedArray, chessBoard.getCellSymbols()); } + + @Test + void isCellInBoard() { + assertTrue(chessBoard.isCellInBoard(0, 0)); + assertTrue(chessBoard.isCellInBoard(7, 7)); + assertTrue(chessBoard.isCellInBoard(2, 1)); + assertTrue(chessBoard.isCellInBoard(5, 7)); + assertFalse(chessBoard.isCellInBoard(-1, 0)); + assertFalse(chessBoard.isCellInBoard(4, 8)); + assertFalse(chessBoard.isCellInBoard(10, 20)); + } } \ No newline at end of file From 244e633ad39d03f2d5b432a426bacefa7f2fb3cb Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Sun, 30 Jan 2022 18:19:00 +0100 Subject: [PATCH 08/23] Added validateCleanPath for ChessBoard class with test --- src/main/java/Game/ChessObj/ChessBoard.java | 41 +++++++++++++++++- .../java/Game/ChessObj/ChessBoardTest.java | 42 ++++++++++++++----- 2 files changed, 70 insertions(+), 13 deletions(-) diff --git a/src/main/java/Game/ChessObj/ChessBoard.java b/src/main/java/Game/ChessObj/ChessBoard.java index 8b32b3c..8477a4b 100644 --- a/src/main/java/Game/ChessObj/ChessBoard.java +++ b/src/main/java/Game/ChessObj/ChessBoard.java @@ -76,13 +76,50 @@ public class ChessBoard { return cellSymbol; } - public boolean isCellInBoard(int x, int y){ - if(x >= 0 && x < board[0].length && y >= 0 && y < board.length) + public boolean isCellInBoard(int x, int y) { + if (x >= 0 && x < board[0].length && y >= 0 && y < board.length) return true; return false; } + protected boolean validateCleanPath(int sourceX, int sourceY, final int destX, final int destY) { + int deltaX = destX - sourceX; + int deltaY = destY - sourceY; + + int stepX = getStepWidth(deltaX); + int stepY = getStepWidth(deltaY); + sourceX += stepX; + sourceY += stepY; + if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) + return false; + while (!(sourceX == destX && sourceY == destY)) { + if (board[sourceY][sourceX] != null) + return false; + sourceX += stepX; + sourceY += stepY; + if (sourceX == destX && sourceY == destY) + return true; + if (sourceX > 7 || sourceY > 7) + return false; + } + + return false; + } + + private int getStepWidth(int delta) { + if (delta == 0) + return 0; + else if (delta > 0) + return 1; + else + return -1; + } + public ChessFigure[][] getBoard() { return this.board; } + + protected void setChessBoard(ChessFigure[][] board) { + this.board = board; + } } diff --git a/src/test/java/Game/ChessObj/ChessBoardTest.java b/src/test/java/Game/ChessObj/ChessBoardTest.java index 0053e9f..459b95a 100644 --- a/src/test/java/Game/ChessObj/ChessBoardTest.java +++ b/src/test/java/Game/ChessObj/ChessBoardTest.java @@ -5,6 +5,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; class ChessBoardTest { @@ -33,16 +34,16 @@ class ChessBoardTest { assertEquals(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK), board[y][x]); continue; } - if(y == 6) { + if (y == 6) { assertEquals(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE), board[y][x]); continue; } - if(y == 0){ + if (y == 0) { assertEquals(new ChessFigure(startOrder[x], ChessFigure.Team.BLACK), board[y][x]); continue; } - if(y == 7){ + if (y == 7) { assertEquals(new ChessFigure(startOrder[x], ChessFigure.Team.WHITE), board[y][x]); continue; } @@ -55,14 +56,14 @@ class ChessBoardTest { @Test void getCellSymbols() { String[][] expectedArray = { - {"|T|","|Z|","|I|","|Q|","|K|","|I|","|Z|","|T|"}, - {"|o|","|o|","|o|","|o|","|o|","|o|","|o|","|o|"}, - {" "," "," "," "," "," "," "," "}, - {" "," "," "," "," "," "," "," "}, - {" "," "," "," "," "," "," "," "}, - {" "," "," "," "," "," "," "," "}, - {" o "," o "," o "," o "," o "," o "," o "," o "}, - {" T "," Z "," I "," Q "," K "," I "," Z "," T "} + {"|T|", "|Z|", "|I|", "|Q|", "|K|", "|I|", "|Z|", "|T|"}, + {"|o|", "|o|", "|o|", "|o|", "|o|", "|o|", "|o|", "|o|"}, + {" ", " ", " ", " ", " ", " ", " ", " "}, + {" ", " ", " ", " ", " ", " ", " ", " "}, + {" ", " ", " ", " ", " ", " ", " ", " "}, + {" ", " ", " ", " ", " ", " ", " ", " "}, + {" o ", " o ", " o ", " o ", " o ", " o ", " o ", " o "}, + {" T ", " Z ", " I ", " Q ", " K ", " I ", " Z ", " T "} }; assertArrayEquals(expectedArray, chessBoard.getCellSymbols()); } @@ -77,4 +78,23 @@ class ChessBoardTest { assertFalse(chessBoard.isCellInBoard(4, 8)); assertFalse(chessBoard.isCellInBoard(10, 20)); } + + @Test + void validateCleanPath() { + ChessFigure[][] tempBoard = chessBoard.getBoard(); + + tempBoard[3][0] = mock(ChessFigure.class); + tempBoard[3][5] = mock(ChessFigure.class); + chessBoard.setChessBoard(tempBoard); + + assertFalse(chessBoard.validateCleanPath(-2, 0, 1, 4)); + assertTrue(chessBoard.validateCleanPath(0, 3, 5, 3)); + assertFalse(chessBoard.validateCleanPath(0, 3, 6, 3)); + assertFalse(chessBoard.validateCleanPath(0, 3, 7, 3)); + assertFalse(chessBoard.validateCleanPath(0, 3, 5, 5)); + assertFalse(chessBoard.validateCleanPath(0, 3, 4, 7)); + assertTrue(chessBoard.validateCleanPath(0, 3, 2, 5)); + assertTrue(chessBoard.validateCleanPath(7, 3, 5, 5)); + assertFalse(chessBoard.validateCleanPath(7, 3, 6, 6)); + } } \ No newline at end of file From 9bbf82c4a54c484fa174d50c1308c76936fff158 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Sun, 30 Jan 2022 23:47:06 +0100 Subject: [PATCH 09/23] Changed test because of bug --- src/main/java/Game/ChessObj/ChessBoard.java | 4 +++- src/main/java/Game/ChessObj/ChessFigure.java | 2 +- src/test/java/Game/ChessObj/ChessFigureTest.java | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/Game/ChessObj/ChessBoard.java b/src/main/java/Game/ChessObj/ChessBoard.java index 8477a4b..f7f0f3b 100644 --- a/src/main/java/Game/ChessObj/ChessBoard.java +++ b/src/main/java/Game/ChessObj/ChessBoard.java @@ -92,6 +92,8 @@ public class ChessBoard { sourceY += stepY; if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) return false; + if(Math.abs(deltaX) <= 1 && Math.abs(deltaY) <= 1) + return true; while (!(sourceX == destX && sourceY == destY)) { if (board[sourceY][sourceX] != null) return false; @@ -119,7 +121,7 @@ public class ChessBoard { return this.board; } - protected void setChessBoard(ChessFigure[][] board) { + public void setChessBoard(ChessFigure[][] board) { this.board = board; } } diff --git a/src/main/java/Game/ChessObj/ChessFigure.java b/src/main/java/Game/ChessObj/ChessFigure.java index 878c66e..0b06f00 100644 --- a/src/main/java/Game/ChessObj/ChessFigure.java +++ b/src/main/java/Game/ChessObj/ChessFigure.java @@ -86,7 +86,7 @@ public class ChessFigure { return true; break; case PAWN: - if (dx == 0) + if (dx != 0) return false; if (getTeam() == Team.WHITE && (dy == 1)) return true; diff --git a/src/test/java/Game/ChessObj/ChessFigureTest.java b/src/test/java/Game/ChessObj/ChessFigureTest.java index 8e5235a..1542ef4 100644 --- a/src/test/java/Game/ChessObj/ChessFigureTest.java +++ b/src/test/java/Game/ChessObj/ChessFigureTest.java @@ -92,11 +92,11 @@ class ChessFigureTest { } break; case PAWN: - if (figure.getTeam() == ChessFigure.Team.WHITE && (y == 1) && x != 0) { + if (figure.getTeam() == ChessFigure.Team.WHITE && (y == 1) && x == 0) { assertTrue(figure.isRelativeMoveValid(x, y)); continue; } - if (figure.getTeam() == ChessFigure.Team.BLACK && (y == -1) && x != 0) { + if (figure.getTeam() == ChessFigure.Team.BLACK && (y == -1) && x == 0) { assertTrue(figure.isRelativeMoveValid(x, y)); continue; } From f60515c6444b37882c33f88e743c3061195a7bf7 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Sun, 30 Jan 2022 23:48:21 +0100 Subject: [PATCH 10/23] Added validateMove method to ChessBoard with test for validating every move on the board --- src/main/java/Game/ChessObj/ChessBoard.java | 34 ++++++++++ .../java/Game/ChessObj/ChessBoardTest.java | 64 +++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/src/main/java/Game/ChessObj/ChessBoard.java b/src/main/java/Game/ChessObj/ChessBoard.java index f7f0f3b..f46138c 100644 --- a/src/main/java/Game/ChessObj/ChessBoard.java +++ b/src/main/java/Game/ChessObj/ChessBoard.java @@ -82,6 +82,40 @@ public class ChessBoard { return false; } + public boolean validateMove(int sourceX, int sourceY, int destX, int destY) { + //Destination not in board range + if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) + return false; + ChessFigure figure = board[sourceY][sourceX]; + //Destination has same Team + if(board[destY][destX] != null) + if(board[destY][destX].getTeam() == figure.getTeam()) + return false; + int deltaX = destX - sourceX; + int deltaY = sourceY - destY; + //Pawn special case + if(figure.getType() == ChessFigure.Type.PAWN){ + if(board[destY][destX] != null) { + if (board[destY][destX].getTeam() == figure.getTeam()) + return false; + if (figure.getTeam() == ChessFigure.Team.WHITE && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == 1) + return true; + if (figure.getTeam() == ChessFigure.Team.BLACK && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == -1) + return true; + } + if(figure.isRelativeMoveValid(deltaX, deltaY)) + return true; + } + if(figure.isRelativeMoveValid(deltaX, deltaY)){ + if(figure.getType() == ChessFigure.Type.KNIGHT) + return true; + if(validateCleanPath(sourceX, sourceY, destX, destY)) + return true; + } + + return false; + } + protected boolean validateCleanPath(int sourceX, int sourceY, final int destX, final int destY) { int deltaX = destX - sourceX; int deltaY = destY - sourceY; diff --git a/src/test/java/Game/ChessObj/ChessBoardTest.java b/src/test/java/Game/ChessObj/ChessBoardTest.java index 459b95a..489f0df 100644 --- a/src/test/java/Game/ChessObj/ChessBoardTest.java +++ b/src/test/java/Game/ChessObj/ChessBoardTest.java @@ -97,4 +97,68 @@ class ChessBoardTest { assertTrue(chessBoard.validateCleanPath(7, 3, 5, 5)); assertFalse(chessBoard.validateCleanPath(7, 3, 6, 6)); } + + /* + 0 1 2 3 4 5 6 7 + ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ + 0 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 1 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 2 │ │ │ │ │ o │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 3 │ │ │ │ |o| │ │ |o| │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 4 │ │ │ o │ │ o │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 5 │ │ │ │ |o| │ │ │ |T| │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 6 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 7 │ │ │ T │ │ B │ │ │ │ + └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘ + */ + @Test + void validateMove() { + ChessFigure[][] tempBoard = new ChessFigure[8][8]; + + tempBoard[4][2] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE); + tempBoard[4][4] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE); + tempBoard[2][4] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE); + tempBoard[3][3] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK); + tempBoard[3][5] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK); + tempBoard[5][3] = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK); + tempBoard[7][2] = new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE); + tempBoard[7][4] = new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE); + tempBoard[5][6] = new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.BLACK); + + chessBoard.setChessBoard(tempBoard); + + //White pawn normal move + assertTrue(chessBoard.validateMove(4, 2, 4, 1)); + assertFalse(chessBoard.validateMove(4, 2, 4, 3)); + assertFalse(chessBoard.validateMove(4, 2, 3, 1)); + assertFalse(chessBoard.validateMove(4, 2, 5, 1)); + //White pawn beats Black + assertTrue(chessBoard.validateMove(4, 4, 3, 3)); + assertTrue(chessBoard.validateMove(4, 4, 5, 3)); + //Black pawn normal move + assertTrue(chessBoard.validateMove(3, 5, 3, 6)); + assertFalse(chessBoard.validateMove(3, 5, 3, 4)); + assertFalse(chessBoard.validateMove(3, 5, 2, 6)); + assertFalse(chessBoard.validateMove(3, 5, 6, 6)); + //Black pawn beats White + assertTrue(chessBoard.validateMove(3, 3, 2, 4)); + assertTrue(chessBoard.validateMove(3, 3, 4, 4)); + + //CastleMoves for testing global rules + assertTrue(chessBoard.validateMove(2, 7, 2, 5)); + assertFalse(chessBoard.validateMove(2, 7, 2, 4)); + assertFalse(chessBoard.validateMove(2, 7, 2, 2)); + + //Bishop Moves for testing global moves + assertTrue(chessBoard.validateMove(4, 7, 5, 6)); + assertTrue(chessBoard.validateMove(4, 7, 6, 5)); + assertFalse(chessBoard.validateMove(4, 7, 7, 4)); + } } \ No newline at end of file From 82aa161806384da6783ade80ed43519d9645bd95 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Mon, 31 Jan 2022 11:39:27 +0100 Subject: [PATCH 11/23] Changed Bishop relative move because of a bug(negative coords didnt get validated) --- src/main/java/Game/ChessObj/ChessFigure.java | 2 +- src/test/java/Game/ChessObj/ChessFigureTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/Game/ChessObj/ChessFigure.java b/src/main/java/Game/ChessObj/ChessFigure.java index 0b06f00..029b233 100644 --- a/src/main/java/Game/ChessObj/ChessFigure.java +++ b/src/main/java/Game/ChessObj/ChessFigure.java @@ -78,7 +78,7 @@ public class ChessFigure { return true; break; case BISHOP: - if (dx == dy) + if (Math.abs(dx) == Math.abs(dy)) return true; break; case KNIGHT: diff --git a/src/test/java/Game/ChessObj/ChessFigureTest.java b/src/test/java/Game/ChessObj/ChessFigureTest.java index 1542ef4..2232596 100644 --- a/src/test/java/Game/ChessObj/ChessFigureTest.java +++ b/src/test/java/Game/ChessObj/ChessFigureTest.java @@ -80,7 +80,7 @@ class ChessFigureTest { } break; case BISHOP: - if (x == y) { + if (Math.abs(x) == Math.abs(y)) { assertTrue(figure.isRelativeMoveValid(x, y)); continue; } From 10a339000c0f444e1322d17ddaf026cbc90e8ba9 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Wed, 2 Feb 2022 10:52:14 +0100 Subject: [PATCH 12/23] Added moveFigure method to ChessBoard with test --- src/main/java/Game/ChessObj/ChessBoard.java | 41 ++++++++++++---- .../java/Game/ChessObj/ChessBoardTest.java | 47 +++++++++++++++++++ 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/src/main/java/Game/ChessObj/ChessBoard.java b/src/main/java/Game/ChessObj/ChessBoard.java index f46138c..f5dd704 100644 --- a/src/main/java/Game/ChessObj/ChessBoard.java +++ b/src/main/java/Game/ChessObj/ChessBoard.java @@ -4,6 +4,13 @@ import java.util.ArrayList; public class ChessBoard { + public enum MoveFeedback { + ENEMYBEATEN, + MOVE, + OUTSIDEOFBOARD, + INVALID + } + private ChessFigure[][] board; public ChessBoard() { @@ -88,14 +95,14 @@ public class ChessBoard { return false; ChessFigure figure = board[sourceY][sourceX]; //Destination has same Team - if(board[destY][destX] != null) - if(board[destY][destX].getTeam() == figure.getTeam()) + if (board[destY][destX] != null) + if (board[destY][destX].getTeam() == figure.getTeam()) return false; int deltaX = destX - sourceX; int deltaY = sourceY - destY; //Pawn special case - if(figure.getType() == ChessFigure.Type.PAWN){ - if(board[destY][destX] != null) { + if (figure.getType() == ChessFigure.Type.PAWN) { + if (board[destY][destX] != null) { if (board[destY][destX].getTeam() == figure.getTeam()) return false; if (figure.getTeam() == ChessFigure.Team.WHITE && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == 1) @@ -103,13 +110,13 @@ public class ChessBoard { if (figure.getTeam() == ChessFigure.Team.BLACK && board[destY][destX].getTeam() != figure.getTeam() && (deltaX == -1 || deltaX == 1) && deltaY == -1) return true; } - if(figure.isRelativeMoveValid(deltaX, deltaY)) + if (figure.isRelativeMoveValid(deltaX, deltaY)) return true; } - if(figure.isRelativeMoveValid(deltaX, deltaY)){ - if(figure.getType() == ChessFigure.Type.KNIGHT) + if (figure.isRelativeMoveValid(deltaX, deltaY)) { + if (figure.getType() == ChessFigure.Type.KNIGHT) return true; - if(validateCleanPath(sourceX, sourceY, destX, destY)) + if (validateCleanPath(sourceX, sourceY, destX, destY)) return true; } @@ -126,7 +133,7 @@ public class ChessBoard { sourceY += stepY; if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) return false; - if(Math.abs(deltaX) <= 1 && Math.abs(deltaY) <= 1) + if (Math.abs(deltaX) <= 1 && Math.abs(deltaY) <= 1) return true; while (!(sourceX == destX && sourceY == destY)) { if (board[sourceY][sourceX] != null) @@ -142,6 +149,22 @@ public class ChessBoard { return false; } + public MoveFeedback moveFigure(int sourceX, int sourceY, final int destX, final int destY) { + if (!isCellInBoard(sourceX, sourceY) || !isCellInBoard(destX, destY)) + return MoveFeedback.OUTSIDEOFBOARD; + if (validateMove(sourceX, sourceY, destX, destY)) { + MoveFeedback feedback = MoveFeedback.INVALID; + if (board[destY][destX] == null) + feedback = MoveFeedback.MOVE; + else + feedback = MoveFeedback.ENEMYBEATEN; + board[destY][destX] = board[sourceY][sourceX]; + board[sourceY][sourceX] = null; + return feedback; + } + return MoveFeedback.INVALID; + } + private int getStepWidth(int delta) { if (delta == 0) return 0; diff --git a/src/test/java/Game/ChessObj/ChessBoardTest.java b/src/test/java/Game/ChessObj/ChessBoardTest.java index 489f0df..4793902 100644 --- a/src/test/java/Game/ChessObj/ChessBoardTest.java +++ b/src/test/java/Game/ChessObj/ChessBoardTest.java @@ -161,4 +161,51 @@ class ChessBoardTest { assertTrue(chessBoard.validateMove(4, 7, 6, 5)); assertFalse(chessBoard.validateMove(4, 7, 7, 4)); } + + /* + 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 + ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ + 0 │ │ │ │ │ │ │ │ │ 0 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 1 │ │ │ │ T │ │ │ │ K │ 1 │ │ │ │ │ │ │ │ K │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 2 │ │ │ │ │ │ │ │ │ 2 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 3 │ │ │ │ │ │ Z │ │ │ 3 │ │ │ │ │ │ Z │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ --> ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 4 │ │ │ │ │ │ │ │ │ 4 │ │ │ │ │ B │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 5 │ │ │ |Q| │ │ │ │ │ │ 5 │ │ │ T │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 6 │ │ │ │ │ │ │ B │ │ 6 │ │ │ │ │ │ │ │ │ + ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤ + 7 │ │ │ │ │ │ │ │ │ 7 │ │ │ │ │ │ │ │ │ + └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘ └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘ + */ + @Test + void moveFigure() { + ChessFigure[][] tempBoard = new ChessFigure[8][8]; + ChessFigure[][] resultBoard = new ChessFigure[8][8]; + + tempBoard[1][3] = new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE); + tempBoard[1][7] = new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE); + tempBoard[6][6] = new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE); + tempBoard[3][5] = new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE); + tempBoard[5][2] = new ChessFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.BLACK); + + resultBoard[5][2] = new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE); + resultBoard[1][7] = new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE); + resultBoard[4][4] = new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE); + resultBoard[3][5] = new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE); + + chessBoard.setChessBoard(tempBoard); + + assertEquals(chessBoard.moveFigure(3, 1, 2, 1), ChessBoard.MoveFeedback.MOVE); + assertEquals(chessBoard.moveFigure(2, 1, 2, 5), ChessBoard.MoveFeedback.ENEMYBEATEN); + assertEquals(chessBoard.moveFigure(6, 6, 4, 4), ChessBoard.MoveFeedback.MOVE); + assertEquals(chessBoard.moveFigure(7, 1, 8, 1), ChessBoard.MoveFeedback.OUTSIDEOFBOARD); + assertEquals(chessBoard.moveFigure(5, 3, 4, 2), ChessBoard.MoveFeedback.INVALID); + + assertArrayEquals(resultBoard, chessBoard.getBoard()); + } } \ No newline at end of file From 84be9179430efe2f5abdb49d4f627482056b277c Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Wed, 2 Feb 2022 12:19:20 +0100 Subject: [PATCH 13/23] Created Chess class with convertInput method with test --- src/main/java/Game/Chess.java | 53 +++++++++++++++++++++++++++++++ src/test/java/Game/ChessTest.java | 43 +++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/main/java/Game/Chess.java create mode 100644 src/test/java/Game/ChessTest.java diff --git a/src/main/java/Game/Chess.java b/src/main/java/Game/Chess.java new file mode 100644 index 0000000..26a0c51 --- /dev/null +++ b/src/main/java/Game/Chess.java @@ -0,0 +1,53 @@ +package Game; + +import Game.ChessObj.ChessBoard; + +public class Chess extends Game { + + ChessBoard chessBoard; + + public Chess() { + init(); + } + + private void init() { + chessBoard = new ChessBoard(); + outputBuffer.addAll(chessBoard.getOutputBoard()); + } + + @Override + public void update(String input) { + + } + + //int[0] = x, int[1] = y; + public int[] convertInput(String input) { + int[] temp = new int[2]; + + //input is not 2 chars big + if (input.length() != 2) + return null; + char[] symbols = input.toCharArray(); + int counterDigit = 0; + int counterChar = 0; + //input contains one number and one char + for (char x : symbols) { + if (Character.isDigit(x)) { + counterDigit++; + temp[1] = Character.getNumericValue(x) - 1; + } + if (Character.isAlphabetic(x)) { + counterChar++; + temp[0] = x; + } + } + if (counterChar != 1 || counterDigit != 1) + return null; + + temp[0] = Character.toLowerCase(temp[0]) - 97; + if (!chessBoard.isCellInBoard(temp[0], temp[1])) + return null; + else + return temp; + } +} diff --git a/src/test/java/Game/ChessTest.java b/src/test/java/Game/ChessTest.java new file mode 100644 index 0000000..8dc691c --- /dev/null +++ b/src/test/java/Game/ChessTest.java @@ -0,0 +1,43 @@ +package Game; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +class ChessTest { + + Chess chess; + + @BeforeEach + void setUp() { + chess = new Chess(); + } + + @AfterEach + void tearDown() { + } + + @Test + void convertInput() { + int[] test1 = {0, 0}; + int[] test2 = {7, 7}; + int[] test3 = {6, 2}; + + assertNull(chess.convertInput("0g2")); + assertNull(chess.convertInput("25")); + assertNull(chess.convertInput("bg")); + assertNull(chess.convertInput("9b")); + assertNull(chess.convertInput("2i")); + + assertArrayEquals(chess.convertInput("1a"), test1); + assertArrayEquals((chess.convertInput("a1")), test1); + assertArrayEquals((chess.convertInput("8h")), test2); + assertArrayEquals((chess.convertInput("h8")), test2); + + assertArrayEquals((chess.convertInput("3G")), test3); + assertArrayEquals((chess.convertInput("G3")), test3); + } +} \ No newline at end of file From b4a3055a79d532f2329c886e66a7d1fd4bc46624 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Wed, 2 Feb 2022 17:37:55 +0100 Subject: [PATCH 14/23] Added getSidebarFigures method for displaying ChessFigures as a List with test --- src/main/java/Game/Chess.java | 27 +++++++++++++++++++++++ src/test/java/Game/ChessTest.java | 36 +++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/main/java/Game/Chess.java b/src/main/java/Game/Chess.java index 26a0c51..76922f1 100644 --- a/src/main/java/Game/Chess.java +++ b/src/main/java/Game/Chess.java @@ -1,6 +1,9 @@ package Game; import Game.ChessObj.ChessBoard; +import Game.ChessObj.ChessFigure; + +import java.util.ArrayList; public class Chess extends Game { @@ -50,4 +53,28 @@ public class Chess extends Game { else return temp; } + + public ArrayList getSidebarFigures(ArrayList chessFigureArrayList, int maxPerLine) { + ArrayList result = new ArrayList<>(); + String line = ""; + int counter = 0; + + for(int i = 0; i < chessFigureArrayList.size(); i++){ + if(i == chessFigureArrayList.size() - 1) { + line += chessFigureArrayList.get(i).getSymbol() + ""; + result.add(line); + return result; + } + line += chessFigureArrayList.get(i).getSymbol() + ","; + counter++; + if(counter >= maxPerLine) { + result.add(line); + line = ""; + counter = 0; + } + } + + return result; + } + } diff --git a/src/test/java/Game/ChessTest.java b/src/test/java/Game/ChessTest.java index 8dc691c..bf6ff9b 100644 --- a/src/test/java/Game/ChessTest.java +++ b/src/test/java/Game/ChessTest.java @@ -1,11 +1,13 @@ package Game; +import Game.ChessObj.ChessFigure; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertNull; +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.*; class ChessTest { @@ -40,4 +42,34 @@ class ChessTest { assertArrayEquals((chess.convertInput("3G")), test3); assertArrayEquals((chess.convertInput("G3")), test3); } + + @Test + void getSidebarFigures() { + ArrayList array1 = new ArrayList<>(); + ArrayList array2 = new ArrayList<>(); + ArrayList array3 = new ArrayList<>(); + ArrayList expectedArray2 = new ArrayList<>(); + ArrayList expectedArray3 = new ArrayList<>(); + + array2.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE)); + array2.add(new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE)); + array2.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE)); + array2.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE)); + array2.add(new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE)); + array2.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE)); + + expectedArray2.add(" o , I , o , o , T ,"); + expectedArray2.add(" o "); + + array3.add(new ChessFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.BLACK)); + array3.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK)); + array3.add(new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK)); + array3.add(new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK)); + + expectedArray3.add("|Q|,|o|,|K|,|o|"); + + assertEquals(0, chess.getSidebarFigures(array1, 5).size()); + assertEquals(expectedArray2, chess.getSidebarFigures(array2, 5)); + assertEquals(expectedArray3, chess.getSidebarFigures(array3, 5)); + } } \ No newline at end of file From fd4d717e6d6f4edc0ae1b20a514d592a60d6bd70 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Wed, 2 Feb 2022 18:57:52 +0100 Subject: [PATCH 15/23] Added scan For OccurringFigure method to ChessBoard with test --- src/main/java/Game/ChessObj/ChessBoard.java | 11 +++++++++++ src/test/java/Game/ChessObj/ChessBoardTest.java | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/main/java/Game/ChessObj/ChessBoard.java b/src/main/java/Game/ChessObj/ChessBoard.java index f5dd704..7fc06a4 100644 --- a/src/main/java/Game/ChessObj/ChessBoard.java +++ b/src/main/java/Game/ChessObj/ChessBoard.java @@ -174,6 +174,17 @@ public class ChessBoard { return -1; } + public int scanForOccurringFigure(ChessFigure.Type type, ChessFigure.Team team){ + int count = 0; + ChessFigure template = new ChessFigure(type, team); + + for(int y = 0; y < board.length; y++) + for(int x = 0; x < board[0].length; x++) + count += (template.equals(board[y][x]))?1:0; + + return count; + } + public ChessFigure[][] getBoard() { return this.board; } diff --git a/src/test/java/Game/ChessObj/ChessBoardTest.java b/src/test/java/Game/ChessObj/ChessBoardTest.java index 4793902..f0bca8b 100644 --- a/src/test/java/Game/ChessObj/ChessBoardTest.java +++ b/src/test/java/Game/ChessObj/ChessBoardTest.java @@ -208,4 +208,21 @@ class ChessBoardTest { assertArrayEquals(resultBoard, chessBoard.getBoard()); } + + @Test + void scanForOccurringFigure() { + assertEquals(8, chessBoard.scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE)); + assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE)); + assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE)); + assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE)); + assertEquals(1, chessBoard.scanForOccurringFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.WHITE)); + assertEquals(1, chessBoard.scanForOccurringFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE)); + + assertEquals(8, chessBoard.scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK)); + assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.BLACK)); + assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.BLACK)); + assertEquals(2, chessBoard.scanForOccurringFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.BLACK)); + assertEquals(1, chessBoard.scanForOccurringFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.BLACK)); + assertEquals(1, chessBoard.scanForOccurringFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK)); + } } \ No newline at end of file From 8dfe543e92bbbd907930bab9ec89c4fd48de4dde Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Wed, 2 Feb 2022 19:37:29 +0100 Subject: [PATCH 16/23] Added makeMove method to Chess for moving a figure and getting the corresponding output with test --- src/main/java/Game/Chess.java | 55 ++++++++++++++++++++++++++++--- src/test/java/Game/ChessTest.java | 13 ++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/main/java/Game/Chess.java b/src/main/java/Game/Chess.java index 76922f1..d6fef15 100644 --- a/src/main/java/Game/Chess.java +++ b/src/main/java/Game/Chess.java @@ -7,7 +7,8 @@ import java.util.ArrayList; public class Chess extends Game { - ChessBoard chessBoard; + private ChessFigure.Team currentTeam; + private ChessBoard chessBoard; public Chess() { init(); @@ -15,6 +16,7 @@ public class Chess extends Game { private void init() { chessBoard = new ChessBoard(); + currentTeam = ChessFigure.Team.WHITE; outputBuffer.addAll(chessBoard.getOutputBoard()); } @@ -54,20 +56,57 @@ public class Chess extends Game { return temp; } + public ArrayList makeMove(int[] source, int[] target) { + ArrayList result = new ArrayList<>(); + ChessFigure sourceFigure = chessBoard.getBoard()[source[1]][source[0]]; + ChessFigure targetFigure = chessBoard.getBoard()[target[1]][target[0]]; + String sourceFigureName = ""; + String targetFigureName = ""; + String sourceCoords = (char) (source[0] + 97) + "" + (source[1] + 1); + String targetCoords = (char) (target[0] + 97) + "" + (target[1] + 1); + + ChessBoard.MoveFeedback moveFeedback = chessBoard.moveFigure(source[0], source[1], target[0], target[1]); + + if (sourceFigure != null) + sourceFigureName = sourceFigure.getTeam().name().toCharArray()[0] + sourceFigure.getTeam().name().substring(1).toLowerCase() + " " + sourceFigure.getType().name().toLowerCase(); + if (targetFigure != null) + targetFigureName = targetFigure.getTeam().name().toLowerCase() + " " + targetFigure.getType().name().toLowerCase(); + + if (moveFeedback == ChessBoard.MoveFeedback.MOVE) { + result.add("Successfully moved " + sourceFigureName.toLowerCase() + " from " + sourceCoords + " to " + targetCoords); + } else if (moveFeedback == ChessBoard.MoveFeedback.ENEMYBEATEN) { + result.add(sourceFigureName + " successfully beat " + targetFigureName.toLowerCase() + " at " + targetCoords); + } else { + result.add("Invalid input!"); + switch (moveFeedback) { + case INVALID: + if (chessBoard.getBoard()[target[0]][target[1]].getTeam() == getCurrentTeam()) + result.add("You are on the same Team! [" + getCurrentTeam().name() + "]"); + break; + case OUTSIDEOFBOARD: + result.add("Input is not inside the board!"); + break; + default: + } + } + + return result; + } + public ArrayList getSidebarFigures(ArrayList chessFigureArrayList, int maxPerLine) { ArrayList result = new ArrayList<>(); String line = ""; int counter = 0; - for(int i = 0; i < chessFigureArrayList.size(); i++){ - if(i == chessFigureArrayList.size() - 1) { + for (int i = 0; i < chessFigureArrayList.size(); i++) { + if (i == chessFigureArrayList.size() - 1) { line += chessFigureArrayList.get(i).getSymbol() + ""; result.add(line); return result; } line += chessFigureArrayList.get(i).getSymbol() + ","; counter++; - if(counter >= maxPerLine) { + if (counter >= maxPerLine) { result.add(line); line = ""; counter = 0; @@ -77,4 +116,12 @@ public class Chess extends Game { return result; } + public ChessBoard getChessBoard() { + return this.chessBoard; + } + + public ChessFigure.Team getCurrentTeam() { + return this.currentTeam; + } + } diff --git a/src/test/java/Game/ChessTest.java b/src/test/java/Game/ChessTest.java index bf6ff9b..9f298ea 100644 --- a/src/test/java/Game/ChessTest.java +++ b/src/test/java/Game/ChessTest.java @@ -72,4 +72,17 @@ class ChessTest { assertEquals(expectedArray2, chess.getSidebarFigures(array2, 5)); assertEquals(expectedArray3, chess.getSidebarFigures(array3, 5)); } + + @Test + void makeMove() { + int countBlackPawns = chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK); + int countWhiteBishops = chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE); + chess.makeMove(chess.convertInput("a2"), chess.convertInput("a3")); + chess.makeMove(chess.convertInput("e7"), chess.convertInput("e6")); + chess.makeMove(chess.convertInput("f8"), chess.convertInput("a3")); + chess.makeMove(chess.convertInput("b2"), chess.convertInput("a3")); + + assertEquals(countBlackPawns - 1, chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK)); + assertEquals(countWhiteBishops - 1, chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE)); + } } \ No newline at end of file From 8eeeb843019b07be3f1fa79e8f24d1e4dc90fbd6 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Wed, 2 Feb 2022 19:43:27 +0100 Subject: [PATCH 17/23] Added getUpdatedOutputBoard --- src/main/java/Game/Chess.java | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/main/java/Game/Chess.java b/src/main/java/Game/Chess.java index d6fef15..91fdf86 100644 --- a/src/main/java/Game/Chess.java +++ b/src/main/java/Game/Chess.java @@ -8,6 +8,10 @@ import java.util.ArrayList; public class Chess extends Game { private ChessFigure.Team currentTeam; + + private ArrayList destroyedWhiteFigures; + private ArrayList destroyedBlackFigures; + private ChessBoard chessBoard; public Chess() { @@ -17,6 +21,8 @@ public class Chess extends Game { private void init() { chessBoard = new ChessBoard(); currentTeam = ChessFigure.Team.WHITE; + destroyedWhiteFigures = new ArrayList<>(); + destroyedBlackFigures = new ArrayList<>(); outputBuffer.addAll(chessBoard.getOutputBoard()); } @@ -76,6 +82,10 @@ public class Chess extends Game { result.add("Successfully moved " + sourceFigureName.toLowerCase() + " from " + sourceCoords + " to " + targetCoords); } else if (moveFeedback == ChessBoard.MoveFeedback.ENEMYBEATEN) { result.add(sourceFigureName + " successfully beat " + targetFigureName.toLowerCase() + " at " + targetCoords); + if (targetFigure.getTeam() == ChessFigure.Team.WHITE) + getDestroyedWhiteFigures().add(targetFigure); + else + getDestroyedBlackFigures().add(targetFigure); } else { result.add("Invalid input!"); switch (moveFeedback) { @@ -93,6 +103,25 @@ public class Chess extends Game { return result; } + private ArrayList getUpdatedOutputBoard(ArrayList whiteFigures, ArrayList blackFigures) { + ArrayList updatedOutputBoard = chessBoard.getOutputBoard(); + ArrayList sidebarWhiteFigures = getSidebarFigures(whiteFigures, 5); + ArrayList sidebarBlackFigures = getSidebarFigures(blackFigures, 5); + + for (int i = 0; i < sidebarWhiteFigures.size(); i++) { + int index = i + 3; + updatedOutputBoard.set(index, updatedOutputBoard.get(index) + sidebarWhiteFigures.get(i)); + } + + for (int i = 0; i < sidebarBlackFigures.size(); i++) { + int index = updatedOutputBoard.size() - (i + 3); + updatedOutputBoard.set(index, updatedOutputBoard.get(index) + sidebarBlackFigures.get(i)); + } + + + return updatedOutputBoard; + } + public ArrayList getSidebarFigures(ArrayList chessFigureArrayList, int maxPerLine) { ArrayList result = new ArrayList<>(); String line = ""; @@ -124,4 +153,12 @@ public class Chess extends Game { return this.currentTeam; } + public ArrayList getDestroyedWhiteFigures() { + return destroyedWhiteFigures; + } + + public ArrayList getDestroyedBlackFigures() { + return destroyedBlackFigures; + } + } From 48e989cbc2f0cbbf2e19917ef6283553ccc477f6 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Wed, 2 Feb 2022 21:34:46 +0100 Subject: [PATCH 18/23] Added lastFeedback functionality for later use --- src/main/java/Game/Chess.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/Game/Chess.java b/src/main/java/Game/Chess.java index 91fdf86..52c88cb 100644 --- a/src/main/java/Game/Chess.java +++ b/src/main/java/Game/Chess.java @@ -12,6 +12,8 @@ public class Chess extends Game { private ArrayList destroyedWhiteFigures; private ArrayList destroyedBlackFigures; + private ChessBoard.MoveFeedback lastFeedback; + private ChessBoard chessBoard; public Chess() { @@ -72,6 +74,7 @@ public class Chess extends Game { String targetCoords = (char) (target[0] + 97) + "" + (target[1] + 1); ChessBoard.MoveFeedback moveFeedback = chessBoard.moveFigure(source[0], source[1], target[0], target[1]); + lastFeedback = moveFeedback; if (sourceFigure != null) sourceFigureName = sourceFigure.getTeam().name().toCharArray()[0] + sourceFigure.getTeam().name().substring(1).toLowerCase() + " " + sourceFigure.getType().name().toLowerCase(); @@ -90,8 +93,9 @@ public class Chess extends Game { result.add("Invalid input!"); switch (moveFeedback) { case INVALID: - if (chessBoard.getBoard()[target[0]][target[1]].getTeam() == getCurrentTeam()) - result.add("You are on the same Team! [" + getCurrentTeam().name() + "]"); + if (targetFigure != null) + if (targetFigure.getTeam() == sourceFigure.getTeam()) + result.add("You are on the same Team! [" + getCurrentTeam().name() + "]"); break; case OUTSIDEOFBOARD: result.add("Input is not inside the board!"); From 2eebbbd4cec3b894507595de81e34f6ab3e22553 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Thu, 3 Feb 2022 14:57:57 +0100 Subject: [PATCH 19/23] Added resetGame for resetting whole game --- src/main/java/Game/Chess.java | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/java/Game/Chess.java b/src/main/java/Game/Chess.java index 52c88cb..5c4e7e4 100644 --- a/src/main/java/Game/Chess.java +++ b/src/main/java/Game/Chess.java @@ -21,11 +21,7 @@ public class Chess extends Game { } private void init() { - chessBoard = new ChessBoard(); - currentTeam = ChessFigure.Team.WHITE; - destroyedWhiteFigures = new ArrayList<>(); - destroyedBlackFigures = new ArrayList<>(); - outputBuffer.addAll(chessBoard.getOutputBoard()); + resetGame(); } @Override @@ -33,6 +29,19 @@ public class Chess extends Game { } + private ArrayList getHeader() { + ArrayList header = new ArrayList<>(); + + header.add("Welcome to the good old game Chess!"); + header.add("First select one cell like: 'e7', than confirm it with an enter."); + header.add("After that, select a second cell and also confirm it with an enter."); + header.add("The Goal is to defeat the other teams King(K)."); + header.add(""); + header.add("It's your Turn " + currentTeam.name().toLowerCase() + "."); + + return header; + } + //int[0] = x, int[1] = y; public int[] convertInput(String input) { int[] temp = new int[2]; @@ -149,6 +158,16 @@ public class Chess extends Game { return result; } + public void resetGame() { + chessBoard = new ChessBoard(); + currentTeam = ChessFigure.Team.WHITE; + destroyedWhiteFigures = new ArrayList<>(); + destroyedBlackFigures = new ArrayList<>(); + outputBuffer.clear(); + outputBuffer.addAll(getHeader()); + outputBuffer.addAll(chessBoard.getOutputBoard()); + } + public ChessBoard getChessBoard() { return this.chessBoard; } From 17a1d255431facac2cefc86d4cea0a6c031227f5 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Thu, 3 Feb 2022 15:46:46 +0100 Subject: [PATCH 20/23] Added whole method with test, Combines all separate units of the Chess class --- src/main/java/Game/Chess.java | 60 +++++++++++++++++++++++++++++++ src/test/java/Game/ChessTest.java | 44 +++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/src/main/java/Game/Chess.java b/src/main/java/Game/Chess.java index 5c4e7e4..471db47 100644 --- a/src/main/java/Game/Chess.java +++ b/src/main/java/Game/Chess.java @@ -7,6 +7,7 @@ import java.util.ArrayList; public class Chess extends Game { + private int[] firstTurn; private ChessFigure.Team currentTeam; private ArrayList destroyedWhiteFigures; @@ -26,6 +27,56 @@ public class Chess extends Game { @Override public void update(String input) { + outputBuffer.clear(); + ArrayList footer = new ArrayList<>(); + + if (isFinished()) { + resetGame(); + return; + } + + int[] coords = convertInput(input); + + if (coords == null) { + footer.add("Invalid Input!"); + firstTurn = null; + } else { + if (firstTurn == null) { + firstTurn = coords; + if (getChessBoard().getBoard()[firstTurn[1]][firstTurn[0]] != null) { + if (getChessBoard().getBoard()[firstTurn[1]][firstTurn[0]].getTeam() == currentTeam) { + String firstCoords = (char) (firstTurn[0] + 97) + "" + (firstTurn[1] + 1); + footer.add("Currently selected cell: " + firstCoords); + } else { + footer.add("It's " + currentTeam.name() + "'s turn "); + firstTurn = null; + } + } else { + footer.add("Can't select empty cell"); + firstTurn = null; + } + } else { + footer = makeMove(firstTurn, coords); + if(lastFeedback != ChessBoard.MoveFeedback.INVALID && lastFeedback != ChessBoard.MoveFeedback.OUTSIDEOFBOARD) + switchCurrentTeam(); + firstTurn = null; + } + } + + if (chessBoard.scanForOccurringFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE) == 0) { + footer.add("Black has Won!"); + footer.add("Press any key to restart game"); + setFinished(true); + } + if (chessBoard.scanForOccurringFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK) == 0) { + footer.add("White has Won!"); + footer.add("Press any key to restart game"); + setFinished(true); + } + + outputBuffer.addAll(getHeader()); + outputBuffer.addAll(getUpdatedOutputBoard(destroyedWhiteFigures, destroyedBlackFigures)); + outputBuffer.addAll(footer); } @@ -158,11 +209,20 @@ public class Chess extends Game { return result; } + private void switchCurrentTeam() { + if (currentTeam == ChessFigure.Team.WHITE) + currentTeam = ChessFigure.Team.BLACK; + else + currentTeam = ChessFigure.Team.WHITE; + } + public void resetGame() { chessBoard = new ChessBoard(); + firstTurn = null; currentTeam = ChessFigure.Team.WHITE; destroyedWhiteFigures = new ArrayList<>(); destroyedBlackFigures = new ArrayList<>(); + setFinished(false); outputBuffer.clear(); outputBuffer.addAll(getHeader()); outputBuffer.addAll(chessBoard.getOutputBoard()); diff --git a/src/test/java/Game/ChessTest.java b/src/test/java/Game/ChessTest.java index 9f298ea..a4d403c 100644 --- a/src/test/java/Game/ChessTest.java +++ b/src/test/java/Game/ChessTest.java @@ -85,4 +85,48 @@ class ChessTest { assertEquals(countBlackPawns - 1, chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK)); assertEquals(countWhiteBishops - 1, chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE)); } + + @Test + void update() { + //starting team = White + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.WHITE); + + //Emtpy cell + chess.update("d7"); + chess.update("g5"); + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.WHITE); + + chess.update("7b"); + chess.update("6b"); + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.BLACK); + + chess.update("e2"); + chess.update("e3"); + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.WHITE); + + chess.update("6b"); + chess.update("5b"); + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.BLACK); + + //Defeat enemy + chess.update("f1"); + chess.update("b5"); + assertEquals(7, chess.getChessBoard().scanForOccurringFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE)); + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.WHITE); + + chess.update("d7"); + chess.update("d6"); + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.BLACK); + + //Invalid Move + chess.update("5b"); + chess.update("a5"); + assertEquals(chess.getCurrentTeam(), ChessFigure.Team.BLACK); + + //King defeated + chess.update("5b"); + chess.update("e8"); + + assertTrue(chess.isFinished()); + } } \ No newline at end of file From 4ef75e249bb433518a8394b1cc0d329b676d150f Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Thu, 3 Feb 2022 15:53:54 +0100 Subject: [PATCH 21/23] Fixed bug for Queen with negative values --- src/main/java/Game/ChessObj/ChessFigure.java | 2 +- src/test/java/Game/ChessObj/ChessFigureTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/Game/ChessObj/ChessFigure.java b/src/main/java/Game/ChessObj/ChessFigure.java index 029b233..4dd993b 100644 --- a/src/main/java/Game/ChessObj/ChessFigure.java +++ b/src/main/java/Game/ChessObj/ChessFigure.java @@ -70,7 +70,7 @@ public class ChessFigure { return true; break; case QUEEN: - if ((dx == dy) || (dx == 0 ^ dy == 0)) + if ((Math.abs(dx) == Math.abs(dy)) || (dx == 0 ^ dy == 0)) return true; break; case CASTLE: diff --git a/src/test/java/Game/ChessObj/ChessFigureTest.java b/src/test/java/Game/ChessObj/ChessFigureTest.java index 2232596..4144b85 100644 --- a/src/test/java/Game/ChessObj/ChessFigureTest.java +++ b/src/test/java/Game/ChessObj/ChessFigureTest.java @@ -68,7 +68,7 @@ class ChessFigureTest { } break; case QUEEN: - if ((x == y) || (x == 0 ^ y == 0)) { + if (Math.abs(x) == Math.abs(y) || (x == 0 ^ y == 0)) { assertTrue(figure.isRelativeMoveValid(x, y)); continue; } From a98367671dcc5a3a4acd5d4bb7a6979fe9673c0d Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Thu, 3 Feb 2022 18:10:59 +0100 Subject: [PATCH 22/23] Added chess game to App --- src/main/java/Application/App.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/Application/App.java b/src/main/java/Application/App.java index 751218f..4929b65 100644 --- a/src/main/java/Application/App.java +++ b/src/main/java/Application/App.java @@ -2,6 +2,7 @@ package Application; import Game.Game; import Game.Tictactoe; +import Game.Chess; import java.util.ArrayList; import java.util.Scanner; @@ -69,6 +70,7 @@ public class App { ArrayList gameList = new ArrayList<>(); gameList.add(new Menu("Tic Tac Toe")); + gameList.add(new Menu("Chess")); Menu gameMenu = new Menu("Games"); @@ -105,6 +107,9 @@ public class App { case "Tic Tac Toe": setCurrentGame(new Tictactoe()); return; + case "Chess": + setCurrentGame(new Chess()); + return; default: break; } From 16f3089d5d7a0eb8a02743d409415051d76ba65a Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Thu, 3 Feb 2022 18:21:25 +0100 Subject: [PATCH 23/23] Changed Snapshot version to 0.2.0 for release --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6f1b0b4..1f84ad1 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.example CliArcadeService - 0.1.5-SNAPSHOT + 0.2.0-SNAPSHOT 15