Skip to content

Commit

Permalink
feat(model): create concept of graph for map
Browse files Browse the repository at this point in the history
  • Loading branch information
manuandru committed Sep 7, 2023
1 parent a496d8b commit f3c2018
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/main/scala/scatan/model/map/UndirectedGraph.scala
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
41 changes: 41 additions & 0 deletions src/test/scala/scatan/model/map/UnorderedPairTest.scala
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
}
}
}

0 comments on commit f3c2018

Please sign in to comment.