• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

htwg-codebreaker-org / codebreaker / 14938345043

09 May 2025 09:52PM UTC coverage: 92.361%. First build
14938345043

Pull #2

github

web-flow
Merge a9aa27378 into a73d205da
Pull Request #2: testing new repo token in coverals

133 of 144 relevant lines covered (92.36%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/main/scala/de/htwg/codebreaker/model/WorldMap.scala
1
// src/main/scala/de/htwg/codebreaker/model/WorldMap.scala
2
package de.htwg.codebreaker.model
3

4
import MapObject._
5

6
/**
7
 * Repräsentation der Weltkarte als Raster von Tiles.
8
 *
9
 * @param width   Breite in Tiles
10
 * @param height  Höhe in Tiles
11
 * @param tiles   Flache Liste aller Tiles mit Position und Kontinent
12
 */
13
case class WorldMap(width: Int, height: Int, tiles: Vector[Tile]):
14

15
  /**
16
   * Erzeugt eine 2D‑Matrix von MapObject, die für jede Position
17
   * angibt, ob dort Spieler, Server, beides oder nichts steht.
18
   *
19
   * Wirft IllegalArgumentException, falls auf ungültige Koordinaten
20
   * zugegriffen wird (tileAt(x,y).get).
21
   */
22
  def getMapData(players: List[Player], servers: List[Server]): Vector[Vector[MapObject]] =
1✔
23
    Vector.tabulate(height, width) { (y, x) =>
1✔
24
      tileAt(x, y) match
1✔
25
        case Some(tile) =>
26
          val maybePlayer = players.zipWithIndex.find((p, _) => p.tile == tile)
1✔
27
          val maybeServer = servers.zipWithIndex.find((s, _) => s.tile == tile)
1✔
28

29
          (maybePlayer, maybeServer) match
30
            case (Some((_, pIdx)), Some((server, sIdx))) =>
1✔
31
              // Beides auf derselben Kachel
32
              PlayerAndServerTile(pIdx, sIdx, server.serverType, tile.continent)
33
            case (Some((_, pIdx)), None) =>
1✔
34
              // Nur Spieler
35
              PlayerOnTile(pIdx)
36
            case (None, Some((server, sIdx))) =>
1✔
37
              // Nur Server
38
              ServerOnTile(sIdx, server.serverType, tile.continent)
39
            case (None, None) =>
1✔
40
              // Nichts
41
              EmptyTile(tile.continent)
42
        case None =>
1✔
43
          // Ungültige Koordinate
44
          throw new IllegalArgumentException(s"Invalid tile coordinates at ($x, $y)")
1✔
45
    }
46

47
  /** Liefert Some(Tile) für gültige Koordinaten, sonst None. */
48
  def tileAt(x: Int, y: Int): Option[Tile] =
1✔
49
    tiles.find(t => t.x == x && t.y == y)
1✔
50

51
  /** Alle Tiles eines bestimmten Kontinents. */
52
  def tilesOf(continent: Continent): Vector[Tile] =
1✔
53
    tiles.filter(_.continent == continent)
1✔
54

55
  /** Kontinent‑Lookup per Koordinate. */
56
  def continentAt(x: Int, y: Int): Option[Continent] =
1✔
57
    tileAt(x, y).map(_.continent)
1✔
58

59
object WorldMap:
60

61
  /** Erzeugt die Standard‑Map mit Breite=20, Höhe=10 und Kontinent‑Einteilung. */
62
  def defaultMap: WorldMap =
1✔
63
    val width  = 20
64
    val height = 10
65
    val tiles = for {
1✔
66
      y <- 0 until height
1✔
67
      x <- 0 until width
1✔
68
    } yield Tile(x, y, classifyContinent(x, y))
1✔
69
    WorldMap(width, height, tiles.toVector)
1✔
70

71
  /**
72
   * Ordnet jeder (x,y)‑Koordinate einen Kontinent zu.
73
   * Regeln basieren auf typischen Positionsbereichen.
74
   */
75
  private def classifyContinent(x: Int, y: Int): Continent =
1✔
76
    (x, y) match
77
      case (x, y) if x <= 4 && y <= 3                             => Continent.NorthAmerica
1✔
78
      case (x, y) if x >= 2 && x <= 4 && y >= 4 && y <= 7         => Continent.SouthAmerica
1✔
79
      case (x, y) if x >= 6 && x <= 8 && y >= 1 && y <= 3         => Continent.Europe
1✔
80
      case (x, y) if x >= 6 && x <= 8 && y >= 4 && y <= 6         => Continent.Africa
1✔
81
      case (x, y) if x >= 10 && x <= 17 && y <= 5                => Continent.Asia
1✔
82
      case (x, y) if x >= 17 && y >= 6 && y <= 8                  => Continent.Oceania
1✔
83
      case (_, 9)                                                 => Continent.Antarctica
1✔
84
      case _                                                      => Continent.Ocean
1✔
85

86
  /** Druckt die Kurz‑Codes (z.B. "NA", "EU", "~~") für alle Kacheln. */
87
  def printContinentMap(map: WorldMap): Unit =
1✔
88
    for y <- 0 until map.height do
1✔
89
      val row = for x <- 0 until map.width yield
1✔
90
        map.continentAt(x, y).map(_.short).getOrElse("--")
1✔
91
      println(row.mkString(" "))
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc