Skip to content
Snippets Groups Projects
Commit 09169c50 authored by dragapsy's avatar dragapsy
Browse files

Tache 2 rectification du constructeur et ajout de la methode calculateNeighbours.

(test passe avec succès)
parent 90c85c8d
No related branches found
No related tags found
No related merge requests found
package model; package model;
<<<<<<< HEAD import java.util.ArrayList;
import java.util.Iterator; import java.util.List;
=======
>>>>>>> origin/main
public class ArrayGrid implements Grid{ public class ArrayGrid implements Grid{
private Cell [][] cells; private Cell [][] cells;
...@@ -12,41 +11,64 @@ public class ArrayGrid implements Grid{ ...@@ -12,41 +11,64 @@ public class ArrayGrid implements Grid{
private int numberOfColumns; private int numberOfColumns;
public ArrayGrid(int numberOfRows,int numberOfColumns){ public ArrayGrid(int numberOfRows, int numberOfColumns) throws Exception{
this.numberOfColumns=numberOfColumns; if(numberOfRows <= 0 || numberOfColumns <= 0){ throw new IllegalArgumentException("taille nulle ou négative");}
this.numberOfRows = numberOfRows; this.numberOfRows = numberOfRows;
if(numberOfRows==0 | numberOfRows<0 | numberOfColumns==0 | numberOfRows<0 | numberOfColumns<0) { this.numberOfColumns = numberOfColumns;
throw new IllegalArgumentException("numberOfRows or numberOfColumns can't be equal to zero or a negative value");
}
else {
this.cells = new Cell[numberOfRows][numberOfColumns]; this.cells = new Cell[numberOfRows][numberOfColumns];
Cell cell=new SquareCell();
for (int i = 0; i < numberOfRows; i++){ for (int i = 0; i < numberOfRows; i++){
for (int j = 0; j < numberOfColumns; j++){ for (int j = 0; j < numberOfColumns; j++){
cells[i][j]=cell; cells[i][j] = new SquareCell();
}
}
for (int i = 0; i < numberOfRows; i++) {
for (int j = 0; j < numberOfColumns; j++) {
calculateNeighbours(i,j);
}
} }
} }
public void calculateNeighbours( int rowIndex, int columnIndex){
List<Cell> neighbours = new ArrayList<>();
if (rowIndex==0){ neighbours.add(getCell(rowIndex+1,columnIndex));}
else if (rowIndex<this.getNumberOfRows()-1) {
neighbours.add(getCell(rowIndex-1,columnIndex));
neighbours.add(getCell(rowIndex+1,columnIndex));
}
if (rowIndex==this.getNumberOfRows()-1){ neighbours.add(getCell(rowIndex-1,columnIndex)); }
if (columnIndex==0){ neighbours.add(getCell(rowIndex,columnIndex+1));}
else if (columnIndex<this.getNumberOfColumns()-1 ) {
neighbours.add(getCell(rowIndex,columnIndex-1));
neighbours.add(getCell(rowIndex,columnIndex+1));
} }
if (columnIndex==this.getNumberOfColumns()-1){ neighbours.add(getCell(rowIndex,columnIndex-1));}
getCell(rowIndex,columnIndex).setNeighbours(neighbours);
} }
@Override @Override
public Cell getCell(int row, int column) { public Cell getCell(int row, int column) {
return null; return this.cells[row][column];
} }
@Override @Override
public int getNumberOfRows() { public int getNumberOfRows() {
return 0; return this.numberOfRows;
} }
@Override @Override
public int getNumberOfColumns() { public int getNumberOfColumns() {
return 0; return this.numberOfColumns;
} }
<<<<<<< HEAD
@Override @Override
public void color(ColorGenerator colorGenerator) { public void color(ColorGenerator colorGenerator) {
......
...@@ -13,12 +13,14 @@ public class SquareCell extends AbstractCell{ ...@@ -13,12 +13,14 @@ public class SquareCell extends AbstractCell{
public SquareCell(){ public SquareCell(){
setColor(AbstractCell.DEFAULT_CELL_COLOR); setColor(AbstractCell.DEFAULT_CELL_COLOR);
setNeighbours(null); this.neighbours= new ArrayList<Cell>();
} }
public SquareCell( Color color){ public SquareCell( Color color){
setColor(color); setColor(color);
setNeighbours(null); this.neighbours= new ArrayList<Cell>();
} }
public SquareCell(Color color,List<Cell>neighbours){ public SquareCell(Color color,List<Cell>neighbours){
...@@ -45,17 +47,8 @@ public class SquareCell extends AbstractCell{ ...@@ -45,17 +47,8 @@ public class SquareCell extends AbstractCell{
*/ */
@Override @Override
public void setNeighbours(List<Cell> cells) { public void setNeighbours(List<Cell> cells) {
if (cells==null){ this.neighbours=cells;
neighbours=new ArrayList<>();
}
else {
neighbours=new ArrayList<>();
for (Cell value:cells)
{
neighbours.add(value);
}
}
} }
......
...@@ -24,7 +24,7 @@ public class MatrixPane extends GridPane { ...@@ -24,7 +24,7 @@ public class MatrixPane extends GridPane {
public MatrixPane(@NamedArg("tileWidth") Double tileWidth, public MatrixPane(@NamedArg("tileWidth") Double tileWidth,
@NamedArg("tileHeight") Double tileHeight, @NamedArg("tileHeight") Double tileHeight,
@NamedArg("numberOfColumns") Integer numberOfColumns, @NamedArg("numberOfColumns") Integer numberOfColumns,
@NamedArg("numberOfRows") Integer numberOfRows) { @NamedArg("numberOfRows") Integer numberOfRows) throws Exception {
this.tileWidth = tileWidth; this.tileWidth = tileWidth;
this.tileHeight = tileHeight; this.tileHeight = tileHeight;
this.numberOfColumns = numberOfColumns; this.numberOfColumns = numberOfColumns;
......
...@@ -15,29 +15,21 @@ class ArrayGridTest { ...@@ -15,29 +15,21 @@ class ArrayGridTest {
private ArrayGrid arrayGridThreeFour; private ArrayGrid arrayGridThreeFour;
private final ArrayGrid arrayGridTwoTwo = new ArrayGrid(2,2); private final ArrayGrid arrayGridTwoTwo = new ArrayGrid(2,2);
ArrayGridTest() throws Exception {
}
@BeforeEach @BeforeEach
void initializeArrayGridThreeFour(){ void initializeArrayGridThreeFour() throws Exception {
arrayGridThreeFour = new ArrayGrid(3,4); arrayGridThreeFour = new ArrayGrid(3,4);
} }
@Test @Test
void testGetCellAndGridInitialization() { void testGetCellAndGridInitialization() {
assertThat(arrayGridThreeFour.getCell(0,0).getNeighbours()) assertThat(arrayGridThreeFour.getCell(0,0).getNeighbours()).hasSize(2).containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,0), arrayGridThreeFour.getCell(0,1));
.hasSize(2) 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));
.containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,0), arrayGridThreeFour.getCell(0,1)); assertThat(arrayGridThreeFour.getCell(2,3).getNeighbours()).hasSize(2).containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,3), arrayGridThreeFour.getCell(2,2));
assertThat(arrayGridThreeFour.getCell(1,1).getNeighbours()).hasSize(4) assertThat(arrayGridThreeFour.getCell(2,2).getNeighbours()).hasSize(3).containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,2), arrayGridThreeFour.getCell(2,1), arrayGridThreeFour.getCell(2,3));
.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 @Test
...@@ -47,12 +39,12 @@ class ArrayGridTest { ...@@ -47,12 +39,12 @@ class ArrayGridTest {
} }
@Test @Test
void testGetNumberOfRows() { void testGetNumberOfRows() throws Exception {
assertThat(new ArrayGrid(100,200).getNumberOfRows()).isEqualTo(100); assertThat(new ArrayGrid(100,200).getNumberOfRows()).isEqualTo(100);
} }
@Test @Test
void testGetNumberOfColumns() { void testGetNumberOfColumns() throws Exception {
assertThat(new ArrayGrid(100,200).getNumberOfColumns()).isEqualTo(200); assertThat(new ArrayGrid(100,200).getNumberOfColumns()).isEqualTo(200);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment