diff --git a/app/src/main/java/model/ArrayGrid.java b/app/src/main/java/model/ArrayGrid.java index 7ff469fa1ac44cc6bf6efcf4227656034c41a663..4558d91543209c421b69c3a929157232688714c3 100644 --- a/app/src/main/java/model/ArrayGrid.java +++ b/app/src/main/java/model/ArrayGrid.java @@ -1,21 +1,23 @@ package model; +import javafx.scene.paint.Color; + public class ArrayGrid implements Grid{ - public Cell[][] cells; + private Cell[][] cells; public ArrayGrid(int numberOfRows, int numberOfColumns) { if(numberOfRows <= 0 || numberOfColumns <= 0) throw new IllegalArgumentException("Le nombre de lignes ou de colonnes ne peut pas être nul ou négatif."); - Cell[][] cells = new Cell[numberOfRows][numberOfColumns]; - for(Cell[] column : cells ) { - for(Cell cell : column) { - cell = new SquareCell(); + this.cells = new Cell[numberOfRows][numberOfColumns]; + for(int i = 0; i < numberOfRows; i++) { + for(int j = 0; j < numberOfColumns; j++) { + cells[i][j] = new SquareCell(); } } } @Override public Cell getCell(int row, int column) { - return cells[row][column]; + return this.cells[row][column]; } @Override diff --git a/app/src/test/java/model/ArrayGridTest.java b/app/src/test/java/model/ArrayGridTest.java index 8f0924add8ac22a6c9066ca321901497a9d74b29..1128fa54f0388a5e3fec1ff4741ef30a6dc01bd5 100644 --- a/app/src/test/java/model/ArrayGridTest.java +++ b/app/src/test/java/model/ArrayGridTest.java @@ -1,79 +1,79 @@ -//package model; -// -//import javafx.scene.paint.Color; -//import org.junit.jupiter.api.BeforeEach; -//import org.junit.jupiter.api.Test; -// -//import java.util.Iterator; -// -//import static org.assertj.core.api.Assertions.assertThat; -//import static org.assertj.core.api.Assertions.assertThatThrownBy; -// -//class ArrayGridTest { -// -// -// private ArrayGrid arrayGridThreeFour; -// private final ArrayGrid arrayGridTwoTwo = new ArrayGrid(2,2); -// -// @BeforeEach -// void initializeArrayGridThreeFour(){ -// arrayGridThreeFour = new ArrayGrid(3,4); -// } -// -// -// @Test -// void testGetCellAndGridInitialization() { -// assertThat(arrayGridThreeFour.getCell(0,0).getNeighbours()) -// .hasSize(2) -// .containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,0), arrayGridThreeFour.getCell(0,1)); -// assertThat(arrayGridThreeFour.getCell(1,1).getNeighbours()).hasSize(4) -// .containsExactlyInAnyOrder(arrayGridThreeFour.getCell(0,1), -// arrayGridThreeFour.getCell(2,1), -// arrayGridThreeFour.getCell(1,2), -// arrayGridThreeFour.getCell(1,0)); -// assertThat(arrayGridThreeFour.getCell(2,3).getNeighbours()).hasSize(2) -// .containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,3), -// arrayGridThreeFour.getCell(2,2)); -// assertThat(arrayGridThreeFour.getCell(2,2).getNeighbours()).hasSize(3) -// .containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,2), -// arrayGridThreeFour.getCell(2,1), -// arrayGridThreeFour.getCell(2,3)); -// } -// -// @Test -// void testConstructionWithIllegalParameters(){ -// assertThatThrownBy(() -> new ArrayGrid(-4,10)).isInstanceOf(IllegalArgumentException.class); -// assertThatThrownBy(() -> new ArrayGrid(4,0)).isInstanceOf(IllegalArgumentException.class); -// -// } -// @Test -// void testGetNumberOfRows() { -// assertThat(new ArrayGrid(100,200).getNumberOfRows()).isEqualTo(100); -// } -// -// @Test -// void testGetNumberOfColumns() { -// assertThat(new ArrayGrid(100,200).getNumberOfColumns()).isEqualTo(200); -// } -// -// private void setArrayGridThreeFourRed(){ -// for (int rowIndex = 0; rowIndex < 3; rowIndex++) { -// for (int columnIndex = 0; columnIndex < 4; columnIndex++) { -// arrayGridThreeFour.getCell(rowIndex,columnIndex).setColor(Color.RED); -// } -// } -// } -// @Test -// void testColor() { -// setArrayGridThreeFourRed(); -// arrayGridThreeFour.color(cell -> Color.BLACK); -// for (int rowIndex = 0; rowIndex < 3; rowIndex++) { -// for (int columnIndex = 0; columnIndex < 4; columnIndex++) { -// assertThat(arrayGridThreeFour.getCell(rowIndex,columnIndex).getColor()).isEqualTo(Color.BLACK); -// } -// } -// } -// +package model; + +import javafx.scene.paint.Color; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Iterator; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class ArrayGridTest { + + + private ArrayGrid arrayGridThreeFour; + private final ArrayGrid arrayGridTwoTwo = new ArrayGrid(2,2); + + @BeforeEach + void initializeArrayGridThreeFour(){ + arrayGridThreeFour = new ArrayGrid(3,4); + } + + + @Test + void testGetCellAndGridInitialization() { + assertThat(arrayGridThreeFour.getCell(0,0).getNeighbours()) + .hasSize(2) + .containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,0), arrayGridThreeFour.getCell(0,1)); + assertThat(arrayGridThreeFour.getCell(1,1).getNeighbours()).hasSize(4) + .containsExactlyInAnyOrder(arrayGridThreeFour.getCell(0,1), + arrayGridThreeFour.getCell(2,1), + arrayGridThreeFour.getCell(1,2), + arrayGridThreeFour.getCell(1,0)); + assertThat(arrayGridThreeFour.getCell(2,3).getNeighbours()).hasSize(2) + .containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,3), + arrayGridThreeFour.getCell(2,2)); + assertThat(arrayGridThreeFour.getCell(2,2).getNeighbours()).hasSize(3) + .containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,2), + arrayGridThreeFour.getCell(2,1), + arrayGridThreeFour.getCell(2,3)); + } + + @Test + void testConstructionWithIllegalParameters(){ + assertThatThrownBy(() -> new ArrayGrid(-4,10)).isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> new ArrayGrid(4,0)).isInstanceOf(IllegalArgumentException.class); + + } + @Test + void testGetNumberOfRows() { + assertThat(new ArrayGrid(100,200).getNumberOfRows()).isEqualTo(100); + } + + @Test + void testGetNumberOfColumns() { + assertThat(new ArrayGrid(100,200).getNumberOfColumns()).isEqualTo(200); + } + + private void setArrayGridThreeFourRed(){ + for (int rowIndex = 0; rowIndex < 3; rowIndex++) { + for (int columnIndex = 0; columnIndex < 4; columnIndex++) { + arrayGridThreeFour.getCell(rowIndex,columnIndex).setColor(Color.RED); + } + } + } + @Test + void testColor() { + setArrayGridThreeFourRed(); + arrayGridThreeFour.color(cell -> Color.BLACK); + for (int rowIndex = 0; rowIndex < 3; rowIndex++) { + for (int columnIndex = 0; columnIndex < 4; columnIndex++) { + assertThat(arrayGridThreeFour.getCell(rowIndex,columnIndex).getColor()).isEqualTo(Color.BLACK); + } + } + } + // @Test // void testIterator() { // Iterator<Cell> iterator = arrayGridTwoTwo.iterator(); @@ -87,4 +87,4 @@ // assertThat(iterator.hasNext()).isFalse(); // // } -//} \ No newline at end of file +} \ No newline at end of file