From 08468123300cd283e5777b5415f1e13c7c84e688 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Sat, 22 Jan 2022 18:48:56 +0100 Subject: [PATCH] added equals method for Board with test --- src/main/java/Game/TicTacToe/Board.java | 16 ++++++++++++++++ src/test/java/Game/TicTacToe/BoardTest.java | 12 +++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/Game/TicTacToe/Board.java b/src/main/java/Game/TicTacToe/Board.java index 92ac33e..1930ba9 100644 --- a/src/main/java/Game/TicTacToe/Board.java +++ b/src/main/java/Game/TicTacToe/Board.java @@ -34,6 +34,8 @@ public class Board { } } + + /* 1 ║2 ║3 o ║ x ║ o @@ -63,4 +65,18 @@ public class Board { public State[] getStates() { return this.states; } + + @Override + public boolean equals(Object o) { + if (!(o instanceof Board)) { + return false; + } + Board x = (Board)o; + for (int i = 0; i < x.getStates().length; i++) { + if (this.getStates()[i] != x.getStates()[i]) { + return false; + } + } + return true; + } } diff --git a/src/test/java/Game/TicTacToe/BoardTest.java b/src/test/java/Game/TicTacToe/BoardTest.java index e75cbdb..ae9f366 100644 --- a/src/test/java/Game/TicTacToe/BoardTest.java +++ b/src/test/java/Game/TicTacToe/BoardTest.java @@ -35,5 +35,15 @@ class BoardTest { } - + @SuppressWarnings("AssertBetweenInconvertibleTypes") + @Test + void testEquals() { + assertNotEquals(board, "Test"); + Board secondBoard = new Board(); + assertEquals(board, secondBoard); + secondBoard.getStates()[0] = Board.State.CROSS; + assertNotEquals(board, secondBoard); + board.getStates()[0] = Board.State.CROSS; + assertEquals(board, secondBoard); + } } \ No newline at end of file