Skip to content

Commit

Permalink
wip(map): init map model
Browse files Browse the repository at this point in the history
  • Loading branch information
manuandru committed Sep 6, 2023
1 parent 7b11f21 commit 51be676
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ lazy val scatan = (project in file("."))
"org.scalatest" %%% "scalatest" % "3.3.0-SNAP4" % Test,
"org.scalatest" %% "scalatest" % "3.3.0-SNAP4" % Test,
"org.scala-js" %%% "scalajs-dom" % "2.6.0",
"com.raquo" %%% "laminar" % "16.0.0"
"com.raquo" %%% "laminar" % "16.0.0",
"org.typelevel" %%% "cats-core" % "2.10.0",
"org.scalatestplus" %%% "scalacheck-1-17" % "3.2.16.0" % Test
),
scalaJSUseMainModuleInitializer := true
)
38 changes: 38 additions & 0 deletions src/main/scala/scatan/mvc/model/map/Hexagon.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package scatan.mvc.model.map

trait Hexagon:
def row: Int
def col: Int
def slice: Int
def neighbours: Set[Hexagon]
def isNeighbour(another: Hexagon): Boolean
def layer: Int

object Hexagon:
import cats.Monoid
given Monoid[Hexagon] with
def empty: Hexagon = Hexagon(0, 0, 0)
def combine(hex1: Hexagon, hex2: Hexagon): Hexagon =
Hexagon(
hex1.row + hex2.row,
hex1.col + hex2.col,
hex1.slice + hex2.slice
)

def apply(row: Int, col: Int, slice: Int): Hexagon = HexagonImpl(row, col, slice)

private val allowedDirections: Set[Hexagon] = Set(
Hexagon(+1, 0, -1),
Hexagon(+1, -1, 0),
Hexagon(0, -1, +1),
Hexagon(-1, 0, +1),
Hexagon(-1, +1, 0),
Hexagon(0, +1, -1)
)

private case class HexagonImpl(row: Int, col: Int, slice: Int) extends Hexagon:
def neighbours: Set[Hexagon] =
import cats.syntax.semigroup.*
allowedDirections.map((this: Hexagon) |+| _)
def isNeighbour(another: Hexagon): Boolean = neighbours.contains(another)
def layer: Int = (math.abs(row) + math.abs(col) + math.abs(slice)) / 2
7 changes: 7 additions & 0 deletions src/test/scala/scatan/BaseTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package scatan

import org.scalatest.*
import org.scalatest.flatspec.*
import org.scalatest.matchers.*

abstract class BaseTest extends AnyFlatSpec with should.Matchers
45 changes: 45 additions & 0 deletions src/test/scala/scatan/mvc/model/map/HexagonTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package scatan.mvc.model.map

import cats.kernel.Monoid
import cats.syntax.semigroup.*
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
import scatan.BaseTest
import scatan.mvc.model.map.Hexagon.given

class HexagonTest extends BaseTest with ScalaCheckPropertyChecks:

"An Hexagon" should "have 3 coordinates" in {
assertCompiles("Hexagon(0, 0, 0)")
assertDoesNotCompile("Hexagon()")
}

it should "be equals based on its coordinates" in {
forAll { (r: Int, c: Int, s: Int) =>
val hexagon1 = Hexagon(r, c, s)
val hexagon2 = Hexagon(r, c, s)
hexagon1 should equal(hexagon2)
}
}

it should "respect the identity law in order to be a Monoid" in {
forAll { (r: Int, c: Int, s: Int) =>
val hexagon = Hexagon(r, c, s)
hexagon |+| Monoid[Hexagon].empty should be(hexagon)
Monoid[Hexagon].empty |+| hexagon should be(hexagon)
}
}

it should "respect the associative law in order to be a Monoid" in {
forAll { (r1: Int, c1: Int, s1: Int, r2: Int, c2: Int, s2: Int) =>
val hexagon1 = Hexagon(r1, c1, s1)
val hexagon2 = Hexagon(r2, c2, s2)
hexagon1 |+| hexagon2 should be(hexagon2 |+| hexagon1)
}
}

it should "have 6 neighbours" in {
forAll { (r: Int, c: Int, s: Int) =>
val hexagon = Hexagon(r, c, s)
hexagon.neighbours.size should equal(6)
}
}

0 comments on commit 51be676

Please sign in to comment.