From 5fa62c49cee579aa742c46a005f3e9189999f4ea Mon Sep 17 00:00:00 2001 From: Alessandro Mazzoli Date: Sat, 9 Sep 2023 18:04:47 +0200 Subject: [PATCH] wip: implementing base model classes in a functional way --- src/main/scala/scatan/model/Building.scala | 5 ++ .../scala/scatan/model/BuildingType.scala | 8 +++ .../scala/scatan/model/DevelopmentCard.scala | 5 ++ .../scala/scatan/model/DevelopmentType.scala | 10 ++++ src/main/scala/scatan/model/Player.scala | 27 +++++++++ .../scala/scatan/model/ResourceCard.scala | 5 ++ .../scala/scatan/model/ResourceType.scala | 10 ++++ src/test/scala/scatan/model/PlayerTest.scala | 57 +++++++++++++++++++ .../scatan/model/building/BuildingTest.scala | 17 ++++++ .../model/building/BuildingTypeTest.scala | 27 +++++++++ .../development/DevelopmentTypeTest.scala | 37 ++++++++++++ .../development/DevelpmentCardTest.scala | 17 ++++++ .../model/resource/ResourceCardTest.scala | 16 ++++++ .../model/resource/ResourceTypeTest.scala | 36 ++++++++++++ 14 files changed, 277 insertions(+) create mode 100644 src/main/scala/scatan/model/Building.scala create mode 100644 src/main/scala/scatan/model/BuildingType.scala create mode 100644 src/main/scala/scatan/model/DevelopmentCard.scala create mode 100644 src/main/scala/scatan/model/DevelopmentType.scala create mode 100644 src/main/scala/scatan/model/Player.scala create mode 100644 src/main/scala/scatan/model/ResourceCard.scala create mode 100644 src/main/scala/scatan/model/ResourceType.scala create mode 100644 src/test/scala/scatan/model/PlayerTest.scala create mode 100644 src/test/scala/scatan/model/building/BuildingTest.scala create mode 100644 src/test/scala/scatan/model/building/BuildingTypeTest.scala create mode 100644 src/test/scala/scatan/model/development/DevelopmentTypeTest.scala create mode 100644 src/test/scala/scatan/model/development/DevelpmentCardTest.scala create mode 100644 src/test/scala/scatan/model/resource/ResourceCardTest.scala create mode 100644 src/test/scala/scatan/model/resource/ResourceTypeTest.scala diff --git a/src/main/scala/scatan/model/Building.scala b/src/main/scala/scatan/model/Building.scala new file mode 100644 index 00000000..30db1f80 --- /dev/null +++ b/src/main/scala/scatan/model/Building.scala @@ -0,0 +1,5 @@ +package scatan.model + +import scatan.mvc.lib.Model + +final case class Building(buildingType: BuildingType) \ No newline at end of file diff --git a/src/main/scala/scatan/model/BuildingType.scala b/src/main/scala/scatan/model/BuildingType.scala new file mode 100644 index 00000000..29a4d295 --- /dev/null +++ b/src/main/scala/scatan/model/BuildingType.scala @@ -0,0 +1,8 @@ +package scatan.model + +import scatan.mvc.lib.Model + +enum BuildingType: + case Settlement + case City + case Road \ No newline at end of file diff --git a/src/main/scala/scatan/model/DevelopmentCard.scala b/src/main/scala/scatan/model/DevelopmentCard.scala new file mode 100644 index 00000000..a2297f8b --- /dev/null +++ b/src/main/scala/scatan/model/DevelopmentCard.scala @@ -0,0 +1,5 @@ +package scatan.model + +import scatan.mvc.lib.Model + +final case class DevelopmentCard(developmentType: DevelopmentType) \ No newline at end of file diff --git a/src/main/scala/scatan/model/DevelopmentType.scala b/src/main/scala/scatan/model/DevelopmentType.scala new file mode 100644 index 00000000..8f6491ca --- /dev/null +++ b/src/main/scala/scatan/model/DevelopmentType.scala @@ -0,0 +1,10 @@ +package scatan.model + +import scatan.mvc.lib.Model + +enum DevelopmentType: + case Knight + case RoadBuilding + case YearOfPlenty + case Monopoly + case VictoryPoint \ No newline at end of file diff --git a/src/main/scala/scatan/model/Player.scala b/src/main/scala/scatan/model/Player.scala new file mode 100644 index 00000000..d34d5fde --- /dev/null +++ b/src/main/scala/scatan/model/Player.scala @@ -0,0 +1,27 @@ +package scatan.model + +import scatan.mvc.lib.Model + +enum Color: + case Red + case Blue + case Green + case Yellow + +final case class Player( + name: String, + color: Color, + buildings: Seq[Building] = Seq.empty, + resources: Seq[ResourceCard] = Seq.empty, + developmentCards: Seq[DevelopmentCard] = Seq.empty +) + +extension (player: Player) + def addBuilding(building: Building): Player = + player.copy(buildings = player.buildings :+ building) + + def addResource(resource: ResourceCard): Player = + player.copy(resources = player.resources :+ resource) + + def addDevelopmentCard(developmentCard: DevelopmentCard): Player = + player.copy(developmentCards = player.developmentCards :+ developmentCard) diff --git a/src/main/scala/scatan/model/ResourceCard.scala b/src/main/scala/scatan/model/ResourceCard.scala new file mode 100644 index 00000000..a1c09f7f --- /dev/null +++ b/src/main/scala/scatan/model/ResourceCard.scala @@ -0,0 +1,5 @@ +package scatan.model + +import scatan.mvc.lib.Model + +final case class ResourceCard(resourceType: ResourceType) \ No newline at end of file diff --git a/src/main/scala/scatan/model/ResourceType.scala b/src/main/scala/scatan/model/ResourceType.scala new file mode 100644 index 00000000..5d0fd891 --- /dev/null +++ b/src/main/scala/scatan/model/ResourceType.scala @@ -0,0 +1,10 @@ +package scatan.model + +import scatan.mvc.lib.Model + +enum ResourceType: + case Wood + case Brick + case Sheep + case Wheat + case Rock diff --git a/src/test/scala/scatan/model/PlayerTest.scala b/src/test/scala/scatan/model/PlayerTest.scala new file mode 100644 index 00000000..7b22f94d --- /dev/null +++ b/src/test/scala/scatan/model/PlayerTest.scala @@ -0,0 +1,57 @@ +package scatan.model + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should + +class PlayerTest extends AnyFlatSpec with should.Matchers: + + "A Player" should "exists" in { + val player: Player = Player("name", Color.Red) + player should not be null + } + + it should "have a name" in { + val player: Player = Player("name", Color.Red) + player.name should be("name") + } + + it should "have a color" in { + val player: Player = Player("name", Color.Red) + player.color should be(Color.Red) + } + + it should "have buildings" in { + val player: Player = Player("name", Color.Red, Seq.empty) + player.buildings should be(Seq.empty[Building]) + } + + it should "have resources" in { + val player: Player = Player("name", Color.Red, Seq.empty, Seq.empty) + player.resources should be(Seq.empty[ResourceCard]) + } + + it should "have development cards" in { + val player: Player = Player("name", Color.Red, Seq.empty, Seq.empty, Seq.empty) + player.developmentCards should be(Seq.empty[DevelopmentCard]) + } + + it should "be able to add a building" in { + val player: Player = Player("name", Color.Red) + val building: Building = Building(BuildingType.City) + val playerWithBuilding: Player = player.addBuilding(building) + playerWithBuilding.buildings should be(Seq(building)) + } + + it should "be able to add a resource" in { + val player: Player = Player("name", Color.Red) + val resource: ResourceCard = ResourceCard(ResourceType.Brick) + val playerWithResource: Player = player.addResource(resource) + playerWithResource.resources should be(Seq(resource)) + } + + it should "be able to add a development card" in { + val player: Player = Player("name", Color.Red) + val developmentCard: DevelopmentCard = DevelopmentCard(DevelopmentType.Knight) + val playerWithDevelopmentCard: Player = player.addDevelopmentCard(developmentCard) + playerWithDevelopmentCard.developmentCards should be(Seq(developmentCard)) + } diff --git a/src/test/scala/scatan/model/building/BuildingTest.scala b/src/test/scala/scatan/model/building/BuildingTest.scala new file mode 100644 index 00000000..c83544c8 --- /dev/null +++ b/src/test/scala/scatan/model/building/BuildingTest.scala @@ -0,0 +1,17 @@ +package scatan.model.building + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should +import scatan.model.{Building, BuildingType} + +class BuildingTest extends AnyFlatSpec with should.Matchers: + + "A Building" should "exists" in { + val building: Building = Building(BuildingType.Settlement) + building should not be null + } + + it should "have a building type" in { + val building: Building = Building(BuildingType.Settlement) + building.buildingType should be(BuildingType.Settlement) + } diff --git a/src/test/scala/scatan/model/building/BuildingTypeTest.scala b/src/test/scala/scatan/model/building/BuildingTypeTest.scala new file mode 100644 index 00000000..8716fa2f --- /dev/null +++ b/src/test/scala/scatan/model/building/BuildingTypeTest.scala @@ -0,0 +1,27 @@ +package scatan.model.building + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should +import scatan.model.BuildingType + +class BuildingTypeTest extends AnyFlatSpec with should.Matchers: + + "A BuildingType" should "exists" in { + val buildingType: BuildingType = BuildingType.Settlement + buildingType should not be null + } + + it should "be able to be a settlement" in { + val buildingType: BuildingType = BuildingType.Settlement + buildingType should be(BuildingType.Settlement) + } + + it should "be able to be a city" in { + val buildingType: BuildingType = BuildingType.City + buildingType should be(BuildingType.City) + } + + it should "be able to be a road" in { + val buildingType: BuildingType = BuildingType.Road + buildingType should be(BuildingType.Road) + } diff --git a/src/test/scala/scatan/model/development/DevelopmentTypeTest.scala b/src/test/scala/scatan/model/development/DevelopmentTypeTest.scala new file mode 100644 index 00000000..d1d0aa9c --- /dev/null +++ b/src/test/scala/scatan/model/development/DevelopmentTypeTest.scala @@ -0,0 +1,37 @@ +package scatan.model.development + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should +import scatan.model.DevelopmentType + +class DevelopmentTypeTest extends AnyFlatSpec with should.Matchers: + + "A DevelopmentType" should "exists" in { + val developmentType: DevelopmentType = DevelopmentType.Knight + developmentType should not be null + } + + it should "be able to be a knight" in { + val developmentType: DevelopmentType = DevelopmentType.Knight + developmentType should be(DevelopmentType.Knight) + } + + it should "be able to be a victory point" in { + val developmentType: DevelopmentType = DevelopmentType.VictoryPoint + developmentType should be(DevelopmentType.VictoryPoint) + } + + it should "be able to be a road building" in { + val developmentType: DevelopmentType = DevelopmentType.RoadBuilding + developmentType should be(DevelopmentType.RoadBuilding) + } + + it should "be able to be a monopoly" in { + val developmentType: DevelopmentType = DevelopmentType.Monopoly + developmentType should be(DevelopmentType.Monopoly) + } + + it should "be able to be a year of plenty" in { + val developmentType: DevelopmentType = DevelopmentType.YearOfPlenty + developmentType should be(DevelopmentType.YearOfPlenty) + } diff --git a/src/test/scala/scatan/model/development/DevelpmentCardTest.scala b/src/test/scala/scatan/model/development/DevelpmentCardTest.scala new file mode 100644 index 00000000..ae091b9f --- /dev/null +++ b/src/test/scala/scatan/model/development/DevelpmentCardTest.scala @@ -0,0 +1,17 @@ +package scatan.model.development + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should +import scatan.model.{DevelopmentCard, DevelopmentType} + +class DevelpmentCardTest extends AnyFlatSpec with should.Matchers: + + "A DevelopmentCard" should "exists" in { + val developmentCard: DevelopmentCard = DevelopmentCard(DevelopmentType.Knight) + developmentCard should not be null + } + + it should "have a development type" in { + val developmentCard: DevelopmentCard = DevelopmentCard(DevelopmentType.Knight) + developmentCard.developmentType should be(DevelopmentType.Knight) + } diff --git a/src/test/scala/scatan/model/resource/ResourceCardTest.scala b/src/test/scala/scatan/model/resource/ResourceCardTest.scala new file mode 100644 index 00000000..b74fa386 --- /dev/null +++ b/src/test/scala/scatan/model/resource/ResourceCardTest.scala @@ -0,0 +1,16 @@ +package scatan.model + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should + +class ResourceCardTest extends AnyFlatSpec with should.Matchers: + + "A ResourceCard" should "exists" in { + val resourceCard: ResourceCard = ResourceCard(ResourceType.Brick) + resourceCard should not be null + } + + it should "have a resource type" in { + val resourceCard: ResourceCard = ResourceCard(ResourceType.Brick) + resourceCard.resourceType should be(ResourceType.Brick) + } diff --git a/src/test/scala/scatan/model/resource/ResourceTypeTest.scala b/src/test/scala/scatan/model/resource/ResourceTypeTest.scala new file mode 100644 index 00000000..ae40ca38 --- /dev/null +++ b/src/test/scala/scatan/model/resource/ResourceTypeTest.scala @@ -0,0 +1,36 @@ +package scatan.model + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should + +class ResourceTypeTest extends AnyFlatSpec with should.Matchers: + + "A ResourceType" should "exists" in { + val resourceType: ResourceType = ResourceType.Brick + resourceType should not be null + } + + it should "be able to be a wood" in { + val resourceType: ResourceType = ResourceType.Wood + resourceType should be(ResourceType.Wood) + } + + it should "be able to be a brick" in { + val resourceType: ResourceType = ResourceType.Brick + resourceType should be(ResourceType.Brick) + } + + it should "be able to be a sheep" in { + val resourceType: ResourceType = ResourceType.Sheep + resourceType should be(ResourceType.Sheep) + } + + it should "be able to be a wheat" in { + val resourceType: ResourceType = ResourceType.Wheat + resourceType should be(ResourceType.Wheat) + } + + it should "be able to be a rock" in { + val resourceType: ResourceType = ResourceType.Rock + resourceType should be(ResourceType.Rock) + }