diff --git a/src/main/scala/floodsim/model/cell/Cell.scala b/src/main/scala/floodsim/model/cell/Cell.scala index 59e341d..cb2fbb5 100644 --- a/src/main/scala/floodsim/model/cell/Cell.scala +++ b/src/main/scala/floodsim/model/cell/Cell.scala @@ -1,9 +1,6 @@ package floodsim.model.cell -import floodsim.model.grid.Coordinate - sealed trait Cell: - def coordinate: Coordinate def dimensions: Double def waterVolume: Double def obstacles: Seq[Obstacle] @@ -34,7 +31,6 @@ sealed trait Cell: protected def withUpdatedValues(waterVolume: Double, absorption: AbsorptionRate.AbsorptionRate): Cell final private case class BaseCell( - coordinate: Coordinate, dimensions: Double, waterVolume: Double, obstacles: Seq[Obstacle], @@ -47,9 +43,8 @@ final private case class BaseCell( copy(waterVolume = math.max(0.0, waterVolume), absorption = absorption) object Cell: - def apply(cellType: CellType, dimensions: Double, altitude: Double, coordinate: Coordinate): Cell = + def apply(cellType: CellType, dimensions: Double, altitude: Double): Cell = BaseCell( - coordinate = coordinate, dimensions = dimensions, waterVolume = 0.0, obstacles = Seq.empty, diff --git a/src/test/scala/floodsim/model/cell/CellTest.scala b/src/test/scala/floodsim/model/cell/CellTest.scala index 10f2077..c38c765 100644 --- a/src/test/scala/floodsim/model/cell/CellTest.scala +++ b/src/test/scala/floodsim/model/cell/CellTest.scala @@ -1,18 +1,15 @@ package floodsim.model.cell -import floodsim.model.grid.Coordinate import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers class CellTest extends AnyFlatSpec with Matchers: "Cell companion object" should "create a Cell with default values" in { - val coordinate = Coordinate(1, 2) val cellType = CellType.Field val dimensions = 10.0 val altitude = 5.0 - val cell = Cell(cellType, dimensions, altitude, coordinate) - cell.coordinate shouldBe coordinate + val cell = Cell(cellType, dimensions, altitude) cell.dimensions shouldBe dimensions cell.waterVolume shouldBe 0.0 cell.obstacles shouldBe empty @@ -22,83 +19,76 @@ class CellTest extends AnyFlatSpec with Matchers: cell.physics shouldBe DefaultCellPhysics } -"Cell" should "calculate water height correctly" in { - val cell = Cell( - CellType.Field, - dimensions = 10.0, - altitude = 5.0, - coordinate = Coordinate(0, 0) - ) - val cellWithWater = cell.updateWater(100.0) - cellWithWater.waterHeight shouldBe 1.0 // 100 / (10*10) = 1.0 -} + "Cell" should "calculate water height correctly" in { + val cell = Cell( + CellType.Field, + dimensions = 10.0, + altitude = 5.0 + ) + val cellWithWater = cell.updateWater(100.0) + cellWithWater.waterHeight shouldBe 1.0 // 100 / (10*10) = 1.0 + } -it should "calculate water speed correctly" in { - val cell = Cell( - CellType.Field, - dimensions = 10.0, - altitude = 5.0, - coordinate = Coordinate(0, 0) - ) - cell.waterSpeed shouldBe 10.0 // MaxWaterSpeed -} + it should "calculate water speed correctly" in { + val cell = Cell( + CellType.Field, + dimensions = 10.0, + altitude = 5.0 + ) + cell.waterSpeed shouldBe 10.0 // MaxWaterSpeed + } -it should "calculate total height correctly" in { - val cell = Cell( - CellType.Field, - dimensions = 10.0, - altitude = 5.0, - coordinate = Coordinate(0, 0) - ) - val cellWithWater = cell.updateWater(100.0) // 1.0 water height - cellWithWater.totalHeight shouldBe 6.0 // altitude(5.0) + waterHeight(1.0) -} + it should "calculate total height correctly" in { + val cell = Cell( + CellType.Field, + dimensions = 10.0, + altitude = 5.0 + ) + val cellWithWater = cell.updateWater(100.0) // 1.0 water height + cellWithWater.totalHeight shouldBe 6.0 // altitude(5.0) + waterHeight(1.0) + } -it should "return the cell type symbol" in { - val concreteCell = Cell(CellType.HouseWithConcrete, 10.0, 5.0, Coordinate(0, 0)) - concreteCell.asSymbol shouldBe "C" -} + it should "return the cell type symbol" in { + val concreteCell = Cell(CellType.HouseWithConcrete, 10.0, 5.0) + concreteCell.asSymbol shouldBe "C" + } -it should "update water volume correctly with absorption" in { - val cell = Cell( - CellType.Field, // Field has absorption of 0.9 - dimensions = 10.0, - altitude = 5.0, - coordinate = Coordinate(0, 0) - ) - val initialWaterVolume = 50.0 - val cellWithWater = cell.updateWater(initialWaterVolume) - val additionalWater = 10.0 - val updatedCell = cellWithWater.updateWater(additionalWater) - updatedCell.waterVolume should be > cellWithWater.waterVolume - updatedCell.waterVolume should be < (cellWithWater.waterVolume + additionalWater) - updatedCell.absorption.value should be < cellWithWater.absorption.value -} + it should "update water volume correctly with absorption" in { + val cell = Cell( + CellType.Field, // Field has absorption of 0.9 + dimensions = 10.0, + altitude = 5.0 + ) + val initialWaterVolume = 50.0 + val cellWithWater = cell.updateWater(initialWaterVolume) + val additionalWater = 10.0 + val updatedCell = cellWithWater.updateWater(additionalWater) + updatedCell.waterVolume should be > cellWithWater.waterVolume + updatedCell.waterVolume should be < (cellWithWater.waterVolume + additionalWater) + updatedCell.absorption.value should be < cellWithWater.absorption.value + } -it should "handle negative resulting water volumes" in { - val cell = Cell( - CellType.Field, - dimensions = 10.0, - altitude = 5.0, - coordinate = Coordinate(0, 0) - ) - val cellWithWater = cell.updateWater(5.0) - val updatedCell = cellWithWater.updateWater(-10.0) - updatedCell.waterVolume shouldBe 0.0 -} + it should "handle negative resulting water volumes" in { + val cell = Cell( + CellType.Field, + dimensions = 10.0, + altitude = 5.0 + ) + val cellWithWater = cell.updateWater(5.0) + val updatedCell = cellWithWater.updateWater(-10.0) + updatedCell.waterVolume shouldBe 0.0 + } -it should "preserve all properties except water volume and absorption when updating water" in { - val coordinate = Coordinate(1, 2) - val cellType = CellType.Field - val dimensions = 10.0 - val altitude = 5.0 - val cell = Cell(cellType, dimensions, altitude, coordinate) - val updatedCell = cell.updateWater(20.0) - updatedCell.waterVolume should not be cell.waterVolume - updatedCell.coordinate shouldBe cell.coordinate - updatedCell.dimensions shouldBe cell.dimensions - updatedCell.obstacles shouldBe cell.obstacles - updatedCell.altitude shouldBe cell.altitude - updatedCell.cellType shouldBe cell.cellType - updatedCell.physics shouldBe cell.physics -} + it should "preserve all properties except water volume and absorption when updating water" in { + val cellType = CellType.Field + val dimensions = 10.0 + val altitude = 5.0 + val cell = Cell(cellType, dimensions, altitude) + val updatedCell = cell.updateWater(20.0) + updatedCell.waterVolume should not be cell.waterVolume + updatedCell.dimensions shouldBe cell.dimensions + updatedCell.obstacles shouldBe cell.obstacles + updatedCell.altitude shouldBe cell.altitude + updatedCell.cellType shouldBe cell.cellType + updatedCell.physics shouldBe cell.physics + }