From 51baf2b06e96358cc735eff436ee324afd1851ff Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Sat, 29 Jan 2022 20:07:21 +0100 Subject: [PATCH] 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