-
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.
feat(model): create concept of graph for map
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package scatan.model.map | ||
|
||
/** A graph is a set of nodes and edges. | ||
*/ | ||
trait UndirectedGraph[Node, Edge <: UnorderedPair[Node, Node]]: | ||
|
||
/** @return | ||
* the set of nodes | ||
*/ | ||
def nodes: Set[Node] | ||
|
||
/** @return | ||
* the set of edges | ||
*/ | ||
def edges: Set[Edge] | ||
|
||
/** An unordered pair of elements. | ||
* @param a | ||
* an element | ||
* @param b | ||
* another element | ||
*/ | ||
case class UnorderedPair[A, B](a: A, b: B): | ||
override def equals(that: Any): Boolean = that match | ||
case UnorderedPair(a1, b1) => (a == a1 && b == b1) || (a == b1 && b == a1) | ||
case _ => false | ||
|
||
override def hashCode: Int = a.hashCode + b.hashCode |
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,41 @@ | ||
package scatan.model.map | ||
|
||
import cats.kernel.Monoid | ||
import cats.syntax.semigroup.* | ||
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks | ||
import scatan.BaseTest | ||
import scatan.model.map.Hexagon.given | ||
import scatan.model.map.HexTiledMap.* | ||
|
||
class UnorderedPairTest extends BaseTest with ScalaCheckPropertyChecks: | ||
|
||
"A UnorderedPair" should "have 2 elements" in { | ||
assertCompiles("UnorderedPair(0, 0)") | ||
assertDoesNotCompile("UnorderedPair()") | ||
} | ||
|
||
it should "be equals based on its elements" in { | ||
forAll { (a: Int, b: Int) => | ||
val pair1 = UnorderedPair(a, b) | ||
val pair2 = UnorderedPair(a, b) | ||
pair1 shouldBe pair2 | ||
} | ||
} | ||
|
||
it should "be equals also in reverse order of its elements" in { | ||
forAll { (a: Int, b: Int) => | ||
val pair1 = UnorderedPair(a, b) | ||
val pair2 = UnorderedPair(b, a) | ||
pair1 shouldBe pair2 | ||
} | ||
} | ||
|
||
it should "be not equals if its elements are different" in { | ||
forAll { (a1: Int, b1: Int, a2: Int, b2: Int) => | ||
whenever(a1 != a2 || b1 != b2) { | ||
val pair1 = UnorderedPair(a1, b1) | ||
val pair2 = UnorderedPair(a2, b2) | ||
pair1 should not be pair2 | ||
} | ||
} | ||
} |