From 9560dfb17875b708b122595971e33a1fdd704a45 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Sat, 22 Jan 2022 19:00:31 +0100 Subject: [PATCH] Added setCellState to class Board with test --- src/main/java/Game/TicTacToe/Board.java | 12 ++++++++++++ src/test/java/Game/TicTacToe/BoardTest.java | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/main/java/Game/TicTacToe/Board.java b/src/main/java/Game/TicTacToe/Board.java index 1930ba9..0f82d3d 100644 --- a/src/main/java/Game/TicTacToe/Board.java +++ b/src/main/java/Game/TicTacToe/Board.java @@ -34,6 +34,18 @@ public class Board { } } + public void setCellState(int cell, boolean cross) { + if (cell <= 9 && cell >= 1) { + if (this.states[cell-1] != State.EMPTY) { + return; + } + if (cross) { + this.states[cell-1] = State.CROSS; + } else { + this.states[cell-1] = State.CIRCLE; + } + } + } /* diff --git a/src/test/java/Game/TicTacToe/BoardTest.java b/src/test/java/Game/TicTacToe/BoardTest.java index ae9f366..bda9727 100644 --- a/src/test/java/Game/TicTacToe/BoardTest.java +++ b/src/test/java/Game/TicTacToe/BoardTest.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.Arrays; + import static org.junit.jupiter.api.Assertions.*; class BoardTest { @@ -34,6 +36,17 @@ class BoardTest { } } + @Test + void setCellState() { + Board.State[] lastBoard = Arrays.copyOf(board.getStates(), board.getStates().length); + board.setCellState(1, true); + assertFalse(Arrays.equals(lastBoard, board.getStates())); + assertEquals(board.getStates()[0], Board.State.CROSS); + board.setCellState(1, false); + assertEquals(board.getStates()[0], Board.State.CROSS); + board.setCellState(2, false); + assertEquals(board.getStates()[1], Board.State.CIRCLE); + } @SuppressWarnings("AssertBetweenInconvertibleTypes") @Test