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;
<<<<<<< HEAD
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
=======
>>>>>>> origin/main
public class ArrayGrid implements Grid{
private Cell [][] cells;
......@@ -12,41 +11,64 @@ public class ArrayGrid implements Grid{
private int numberOfColumns;
public ArrayGrid(int numberOfRows,int numberOfColumns){
this.numberOfColumns=numberOfColumns;
this.numberOfRows=numberOfRows;
if(numberOfRows==0 | numberOfRows<0 | numberOfColumns==0 | numberOfRows<0 | numberOfColumns<0) {
throw new IllegalArgumentException("numberOfRows or numberOfColumns can't be equal to zero or a negative value");
}
else {
this.cells=new Cell[numberOfRows][numberOfColumns];
Cell cell=new SquareCell();
for (int i=0; i<numberOfRows;i++){
for(int j=0; j<numberOfColumns;j++){
cells[i][j]=cell;
public ArrayGrid(int numberOfRows, int numberOfColumns) throws Exception{
if(numberOfRows <= 0 || numberOfColumns <= 0){ throw new IllegalArgumentException("taille nulle ou négative");}
this.numberOfRows = numberOfRows;
this.numberOfColumns = numberOfColumns;
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();
}
}
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
public Cell getCell(int row, int column) {
return null;
return this.cells[row][column];
}
@Override
public int getNumberOfRows() {
return 0;
return this.numberOfRows;
}
@Override
public int getNumberOfColumns() {
return 0;
return this.numberOfColumns;
}
<<<<<<< HEAD
@Override
public void color(ColorGenerator colorGenerator) {
......
......@@ -13,12 +13,14 @@ public class SquareCell extends AbstractCell{
public SquareCell(){
setColor(AbstractCell.DEFAULT_CELL_COLOR);
setNeighbours(null);
this.neighbours= new ArrayList<Cell>();
}
public SquareCell( Color color){
setColor(color);
setNeighbours(null);
this.neighbours= new ArrayList<Cell>();
}
public SquareCell(Color color,List<Cell>neighbours){
......@@ -45,17 +47,8 @@ public class SquareCell extends AbstractCell{
*/
@Override
public void setNeighbours(List<Cell> cells) {
if (cells==null){
neighbours=new ArrayList<>();
}
else {
neighbours=new ArrayList<>();
for (Cell value:cells)
{
neighbours.add(value);
}
}
this.neighbours=cells;
}
......
......@@ -24,7 +24,7 @@ public class MatrixPane extends GridPane {
public MatrixPane(@NamedArg("tileWidth") Double tileWidth,
@NamedArg("tileHeight") Double tileHeight,
@NamedArg("numberOfColumns") Integer numberOfColumns,
@NamedArg("numberOfRows") Integer numberOfRows) {
@NamedArg("numberOfRows") Integer numberOfRows) throws Exception {
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.numberOfColumns = numberOfColumns;
......
......@@ -15,29 +15,21 @@ class ArrayGridTest {
private ArrayGrid arrayGridThreeFour;
private final ArrayGrid arrayGridTwoTwo = new ArrayGrid(2,2);
ArrayGridTest() throws Exception {
}
@BeforeEach
void initializeArrayGridThreeFour(){
void initializeArrayGridThreeFour() throws Exception {
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));
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
......@@ -47,12 +39,12 @@ class ArrayGridTest {
}
@Test
void testGetNumberOfRows() {
void testGetNumberOfRows() throws Exception {
assertThat(new ArrayGrid(100,200).getNumberOfRows()).isEqualTo(100);
}
@Test
void testGetNumberOfColumns() {
void testGetNumberOfColumns() throws Exception {
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