Skip to content

Commit

Permalink
refactor(road-build): add possibility to skip check in road build to …
Browse files Browse the repository at this point in the history
…avoid heavy construction during tests
  • Loading branch information
manuandru committed Oct 9, 2023
1 parent bc7b988 commit 59e7dca
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
13 changes: 13 additions & 0 deletions src/test/scala/scatan/model/game/BaseScatanStateTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ import scatan.BaseTest
import scatan.model.game.config.ScatanPlayer
import scatan.model.game.ops.EmptySpotsOps.emptySpots
import scatan.model.map.Spot
import scatan.model.map.RoadSpot
import scatan.model.game.ops.EmptySpotsOps.emptyRoadSpot
import scatan.model.game.ops.BuildingOps.assignBuilding
import scatan.model.components.BuildingType

abstract class BaseScatanStateTest extends BaseTest:

protected def players(n: Int): Seq[ScatanPlayer] =
(1 to n).map(i => ScatanPlayer(s"Player $i"))

protected def noConstrainToBuildRoad: ScatanState => ((RoadSpot, ScatanPlayer) => Boolean) = _ => (_, _) => true
protected def spotShouldBeEmptyToBuildRoad: ScatanState => ((RoadSpot, ScatanPlayer) => Boolean) = s =>
(r, _) => s.emptyRoadSpot.contains(r)

// Avoid heavy check on spot type
extension (state: ScatanState)
def assignRoadWithoutRule(spot: RoadSpot, player: ScatanPlayer): Option[ScatanState] =
state.assignBuilding(spot, BuildingType.Road, player, noConstrainToBuildRoad)

protected def emptySpot(state: ScatanState): Spot = state.emptySpots.head

val threePlayers = players(3)
Expand Down
52 changes: 26 additions & 26 deletions src/test/scala/scatan/model/game/ops/AwardOpsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class AwardOpsTest extends BaseScatanStateTest:
val player1 = threePlayers.head
val it = state.emptyRoadSpot.iterator
val stateWithAward = for
stateWithOneRoad <- state.assignBuilding(it.next, BuildingType.Road, player1)
stateWithTwoRoad <- stateWithOneRoad.assignBuilding(it.next, BuildingType.Road, player1)
stateWithThreeRoad <- stateWithTwoRoad.assignBuilding(it.next, BuildingType.Road, player1)
stateWithFourRoad <- stateWithThreeRoad.assignBuilding(it.next, BuildingType.Road, player1)
stateWithFiveRoad <- stateWithFourRoad.assignBuilding(it.next, BuildingType.Road, player1)
stateWithOneRoad <- state.assignRoadWithoutRule(it.next, player1)
stateWithTwoRoad <- stateWithOneRoad.assignRoadWithoutRule(it.next, player1)
stateWithThreeRoad <- stateWithTwoRoad.assignRoadWithoutRule(it.next, player1)
stateWithFourRoad <- stateWithThreeRoad.assignRoadWithoutRule(it.next, player1)
stateWithFiveRoad <- stateWithFourRoad.assignRoadWithoutRule(it.next, player1)
yield stateWithFiveRoad
stateWithAward match
case Some(state) => state.awards(Award(AwardType.LongestRoad)) should be(Some((player1, 5)))
Expand All @@ -52,18 +52,18 @@ class AwardOpsTest extends BaseScatanStateTest:
val player2 = threePlayers.tail.head
val it = state.emptyRoadSpot.iterator
val firstStateWithAward = for
stateWithOneRoad <- state.assignBuilding(it.next, BuildingType.Road, player1)
stateWithTwoRoad <- stateWithOneRoad.assignBuilding(it.next, BuildingType.Road, player1)
stateWithThreeRoad <- stateWithTwoRoad.assignBuilding(it.next, BuildingType.Road, player1)
stateWithFourRoad <- stateWithThreeRoad.assignBuilding(it.next, BuildingType.Road, player1)
stateWithFiveRoad <- stateWithFourRoad.assignBuilding(it.next, BuildingType.Road, player1)
stateWithOneRoad <- state.assignRoadWithoutRule(it.next, player1)
stateWithTwoRoad <- stateWithOneRoad.assignRoadWithoutRule(it.next, player1)
stateWithThreeRoad <- stateWithTwoRoad.assignRoadWithoutRule(it.next, player1)
stateWithFourRoad <- stateWithThreeRoad.assignRoadWithoutRule(it.next, player1)
stateWithFiveRoad <- stateWithFourRoad.assignRoadWithoutRule(it.next, player1)
yield stateWithFiveRoad
val secondStateWithAward = for
stateWithOneRoad <- firstStateWithAward.get.assignBuilding(it.next, BuildingType.Road, player2)
stateWithTwoRoad <- stateWithOneRoad.assignBuilding(it.next, BuildingType.Road, player2)
stateWithThreeRoad <- stateWithTwoRoad.assignBuilding(it.next, BuildingType.Road, player2)
stateWithFourRoad <- stateWithThreeRoad.assignBuilding(it.next, BuildingType.Road, player2)
stateWithFiveRoad <- stateWithFourRoad.assignBuilding(it.next, BuildingType.Road, player2)
stateWithOneRoad <- firstStateWithAward.get.assignRoadWithoutRule(it.next, player2)
stateWithTwoRoad <- stateWithOneRoad.assignRoadWithoutRule(it.next, player2)
stateWithThreeRoad <- stateWithTwoRoad.assignRoadWithoutRule(it.next, player2)
stateWithFourRoad <- stateWithThreeRoad.assignRoadWithoutRule(it.next, player2)
stateWithFiveRoad <- stateWithFourRoad.assignRoadWithoutRule(it.next, player2)
yield stateWithFiveRoad
firstStateWithAward match
case Some(state) => state.awards(Award(AwardType.LongestRoad)) should be(Some((player1, 5)))
Expand All @@ -80,19 +80,19 @@ class AwardOpsTest extends BaseScatanStateTest:
val player2 = threePlayers.tail.head
val it = state.emptyRoadSpot.iterator
val firstStateWithAward = for
stateWithOneRoad <- state.assignBuilding(it.next, BuildingType.Road, player1)
stateWithTwoRoad <- stateWithOneRoad.assignBuilding(it.next, BuildingType.Road, player1)
stateWithThreeRoad <- stateWithTwoRoad.assignBuilding(it.next, BuildingType.Road, player1)
stateWithFourRoad <- stateWithThreeRoad.assignBuilding(it.next, BuildingType.Road, player1)
stateWithFiveRoad <- stateWithFourRoad.assignBuilding(it.next, BuildingType.Road, player1)
stateWithOneRoad <- state.assignRoadWithoutRule(it.next, player1)
stateWithTwoRoad <- stateWithOneRoad.assignRoadWithoutRule(it.next, player1)
stateWithThreeRoad <- stateWithTwoRoad.assignRoadWithoutRule(it.next, player1)
stateWithFourRoad <- stateWithThreeRoad.assignRoadWithoutRule(it.next, player1)
stateWithFiveRoad <- stateWithFourRoad.assignRoadWithoutRule(it.next, player1)
yield stateWithFiveRoad
val secondStateWithAward = for
stateWithOneRoad <- firstStateWithAward.get.assignBuilding(it.next, BuildingType.Road, player2)
stateWithTwoRoad <- stateWithOneRoad.assignBuilding(it.next, BuildingType.Road, player2)
stateWithThreeRoad <- stateWithTwoRoad.assignBuilding(it.next, BuildingType.Road, player2)
stateWithFourRoad <- stateWithThreeRoad.assignBuilding(it.next, BuildingType.Road, player2)
stateWithFiveRoad <- stateWithFourRoad.assignBuilding(it.next, BuildingType.Road, player2)
stateWithSixRoad <- stateWithFiveRoad.assignBuilding(it.next, BuildingType.Road, player2)
stateWithOneRoad <- firstStateWithAward.get.assignRoadWithoutRule(it.next, player2)
stateWithTwoRoad <- stateWithOneRoad.assignRoadWithoutRule(it.next, player2)
stateWithThreeRoad <- stateWithTwoRoad.assignRoadWithoutRule(it.next, player2)
stateWithFourRoad <- stateWithThreeRoad.assignRoadWithoutRule(it.next, player2)
stateWithFiveRoad <- stateWithFourRoad.assignRoadWithoutRule(it.next, player2)
stateWithSixRoad <- stateWithFiveRoad.assignRoadWithoutRule(it.next, player2)
yield stateWithSixRoad
firstStateWithAward match
case Some(state) => state.awards(Award(AwardType.LongestRoad)) should be(Some((player1, 5)))
Expand Down
6 changes: 1 addition & 5 deletions src/test/scala/scatan/model/game/ops/BuildingOpsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ class BuildingOpsTest extends BaseScatanStateTest:
private def roadNearSpot(state: ScatanState, spot: StructureSpot): RoadSpot =
state.emptyRoadSpot.filter(_.contains(spot)).head

private def noConstrainToBuildRoad: ScatanState => ((RoadSpot, ScatanPlayer) => Boolean) = _ => (_, _) => true
private def spotShouldBeEmptyToBuildRoad: ScatanState => ((RoadSpot, ScatanPlayer) => Boolean) = s =>
(r, _) => s.emptyRoadSpot.contains(r)

"A State with buildings Ops" should "have empty buildings when state start" in {
val state = ScatanState(threePlayers)
state.assignedBuildings.keySet should have size 0
Expand All @@ -34,7 +30,7 @@ class BuildingOpsTest extends BaseScatanStateTest:
val state = ScatanState(threePlayers)
given ScatanState = state
state
.assignBuilding(spotToBuildRoad(state), BuildingType.Road, threePlayers.head, noConstrainToBuildRoad) match
.assignRoadWithoutRule(spotToBuildRoad(state), threePlayers.head) match
case Some(state) => state.assignedBuildings should have size 1
case None => fail("state should be defined")
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/scala/scatan/model/game/ops/ScoreOpsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ScoreOpsTest extends BaseScatanStateTest:
val state = ScatanState(threePlayers)
val player1 = threePlayers.head
val it = state.emptyRoadSpot.iterator
val stateWithRoad = state.assignBuilding(it.next(), BuildingType.Road, player1)
val stateWithRoad = state.assignRoadWithoutRule(it.next(), player1)
stateWithRoad match
case Some(state) =>
state.scores(player1) should be(0)
Expand All @@ -60,11 +60,11 @@ class ScoreOpsTest extends BaseScatanStateTest:
val stateWithSettlementAndAward =
for
stateWithSettlement <- state.assignBuilding(state.emptyStructureSpot.head, BuildingType.Settlement, player1)
oneRoadState <- stateWithSettlement.assignBuilding(roadSpotIterator.next, BuildingType.Road, player1)
twoRoadState <- oneRoadState.assignBuilding(roadSpotIterator.next, BuildingType.Road, player1)
threeRoadState <- twoRoadState.assignBuilding(roadSpotIterator.next, BuildingType.Road, player1)
fourRoadState <- threeRoadState.assignBuilding(roadSpotIterator.next, BuildingType.Road, player1)
fiveRoadState <- fourRoadState.assignBuilding(roadSpotIterator.next, BuildingType.Road, player1)
oneRoadState <- stateWithSettlement.assignRoadWithoutRule(roadSpotIterator.next, player1)
twoRoadState <- oneRoadState.assignRoadWithoutRule(roadSpotIterator.next, player1)
threeRoadState <- twoRoadState.assignRoadWithoutRule(roadSpotIterator.next, player1)
fourRoadState <- threeRoadState.assignRoadWithoutRule(roadSpotIterator.next, player1)
fiveRoadState <- fourRoadState.assignRoadWithoutRule(roadSpotIterator.next, player1)
yield fiveRoadState
stateWithSettlementAndAward match
case Some(state) =>
Expand Down

0 comments on commit 59e7dca

Please sign in to comment.