-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |