Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General agent grid #102

Merged
merged 2 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/java/app/model/agents/Cells/BooleanCell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package app.model.agents.Cells;

public class BooleanCell implements Cell
{
private boolean explored;
private boolean obstacle;

public void explored()
{
explored = true;
}

public void obstacle()
{
obstacle = true;
}

public boolean getExploredState()
{
return explored;
}

public boolean getObstacleState()
{
return obstacle;
}
}
5 changes: 5 additions & 0 deletions src/main/java/app/model/agents/Cells/Cell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package app.model.agents.Cells;

public interface Cell
{
}
19 changes: 19 additions & 0 deletions src/main/java/app/model/agents/Cells/CellFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package app.model.agents.Cells;

public abstract class CellFactory
{
public static Cell make(CellType type)
{
return create(type);
}

private static Cell create(CellType type)
{
switch(type)
{
case PHERAMONE -> {return new PheramoneCell();}
case BOOLEAN -> {return new BooleanCell();}
}
return null;
}
}
8 changes: 8 additions & 0 deletions src/main/java/app/model/agents/Cells/CellType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package app.model.agents.Cells;

public enum CellType
{
PHERAMONE,
BOOLEAN,
}

16 changes: 16 additions & 0 deletions src/main/java/app/model/agents/Cells/PheramoneCell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package app.model.agents.Cells;

public class PheramoneCell implements Cell
{
private double value;

public double getValue()
{
return value;
}

public void updateValue(double newValue)
{
value = newValue;
}
}
44 changes: 44 additions & 0 deletions src/main/java/app/model/agents/Grid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package app.model.agents;

import app.model.agents.Cells.*;

public class Grid
{
protected int rowSize;
protected int colSize;
protected Cell[][] grid;
protected double cellSize = 1.0;
protected CellType type;


public Grid(double length, double width, CellType type)
{
rowSize = (int)(length / cellSize);
colSize = (int)(width / cellSize);
this.type = type;

grid = new Cell[rowSize][colSize];
initializeAllCells();
}

public void initializeAllCells()
{
for(int row = 0; row < rowSize; row++)
{
for(int col = 0; col < colSize; col++)
{
grid[row][col] = CellFactory.make(type);
}
}
}

public Cell getCellAt(int row, int col)
{
switch (type)
{
case BOOLEAN -> {return (BooleanCell)grid[row][col];}
case PHERAMONE -> {return (PheramoneCell)grid[row][col];}
}
return null;
}
}
31 changes: 31 additions & 0 deletions src/test/java/jgfx/javagradlefx/GridTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package jgfx.javagradlefx;

import app.model.agents.*;
import app.model.agents.Cells.BooleanCell;
import app.model.agents.Cells.CellType;
import app.model.agents.Cells.PheramoneCell;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class GridTest
{
@Test
void testPheramoneCell()
{
Grid grid = new Grid(100, 100, CellType.PHERAMONE);
PheramoneCell cell = (PheramoneCell)grid.getCellAt(10, 10);
double value = cell.getValue();

assertEquals(value, 0.0);
}

@Test
void testBooleanCell()
{
Grid grid = new Grid(100, 100, CellType.BOOLEAN);
BooleanCell cell = (BooleanCell) grid.getCellAt(10, 10);
boolean value = cell.getExploredState();

assertEquals(value, false);
}
}