diff --git a/shipsgame/src/main/battleship/Main.scala b/shipsgame/src/main/battleship/Main.scala index 267fefe..3906e03 100644 --- a/shipsgame/src/main/battleship/Main.scala +++ b/shipsgame/src/main/battleship/Main.scala @@ -1,8 +1,32 @@ import main.battleship +import main.battleship.ShipFactory object Main { def main(args: Array[String]): Unit = { println("Hello world!") + val factory = new ShipFactory() + + // Wyświetlanie dostępnych statków + factory.printShips() + + // Próba utworzenia statku nieistniejącego typu + println(factory.createShip("Type0")) + + // Utworzenie statku typu 1 + println(factory.createShip("Type1")) + + // Próba utworzenia statku typu 1 jeszcze raz + println(factory.createShip("Type1")) + + // Wyświetlanie dostępnych statków po utworzeniu statku typu 1 + factory.printShips() + + // Utworzenie statku typu 2 + println(factory.createShip("Type2")) + + // Wyświetlanie dostępnych statków po utworzeniu statku typu 2 + factory.printShips() + } } diff --git a/shipsgame/src/main/battleship/Ship.scala b/shipsgame/src/main/battleship/Ship.scala index 6cb0b7d..969ee3c 100644 --- a/shipsgame/src/main/battleship/Ship.scala +++ b/shipsgame/src/main/battleship/Ship.scala @@ -4,4 +4,8 @@ package main.battleship class Ship(val shipType: ShipType.Value, val length: Int, val width: Int) { val size: Int = length * width var occupied_fields: Int = size + + override def toString: String = { + "Ship: " + shipType + " size: " + size + } } diff --git a/shipsgame/src/main/battleship/ShipFactory.scala b/shipsgame/src/main/battleship/ShipFactory.scala index e822066..52a08d0 100644 --- a/shipsgame/src/main/battleship/ShipFactory.scala +++ b/shipsgame/src/main/battleship/ShipFactory.scala @@ -2,59 +2,49 @@ package main.battleship import scala.collection.mutable class ShipFactory { - val usedShips: mutable.HashSet[ShipType.Value] = mutable.HashSet.empty + val usedShips: mutable.HashSet[ShipType.ShipType] = mutable.HashSet.empty - def checkIfUsed(shipType: ShipType.Value): Boolean = { - usedShips.contains(shipType) - } + def checkIfUsed(shipType: ShipType.ShipType): Boolean = usedShips.contains(shipType) - def addToUsed(shipType: ShipType.Value): Unit = { - usedShips += shipType - } - def removeFromUsed(shipType: ShipType.Value):Unit = { - usedShips.remove(shipType); - } + def addToUsed(shipType: ShipType.ShipType): Unit = usedShips += shipType - //funkcja sprawdzajaca, czy podany string jest poprawnym typem statku (pokrywa sie z enumem) - def isValidShipType(shipType: String): Boolean = { - ShipType.values.exists(_.toString == shipType) - } + def removeFromUsed(shipType: ShipType.ShipType): Unit = usedShips.remove(shipType) def createShip(shipType: String): Option[Ship] = { - if(!isValidShipType(shipType)){ - println("Ten typ statku nie istnieje.") - None - } - val shipTypeEnum = ShipType.withName(shipType) - if(checkIfUsed(shipTypeEnum)) { - println("Ten typ statku został już użyty.") - None - } else { - addToUsed(shipTypeEnum) - val ship = shipTypeEnum match { - case ShipType.Type1 => new Ship(shipTypeEnum, 5, 1) - case ShipType.Type2 => new Ship(shipTypeEnum, 4, 2) - case ShipType.Type3 => new Ship(shipTypeEnum, 3, 1) - case ShipType.Type4 => new Ship(shipTypeEnum, 2, 1) - // Dodaj tutaj więcej typów statków, jeśli są potrzebne + try { + val shipTypeEnum = ShipType.withName(shipType) + if (checkIfUsed(shipTypeEnum)) { + println("Ten typ statku został już użyty.") + None + } else { + addToUsed(shipTypeEnum) + ShipType.attributesOf(shipTypeEnum) match { + case Some(attr) => + val ship = new Ship(shipTypeEnum, attr.length, attr.width) + Some(ship) + case None => + println("Nie znaleziono atrybutów dla podanego typu statku.") + None + } } - Some(ship) + } catch { + case _: NoSuchElementException => + println("Nie ma takiego typu statku.") + None } } - def printShips(): Unit = { println("Dostępne typy statków:") ShipType.values.foreach { shipType => - if(!usedShips.contains(shipType)) { - shipType match { - case ShipType.Type1 => println(s"Typ: $shipType, Długość: 1, Szerokość: 5") - case ShipType.Type2 => println(s"Typ: $shipType, Długość: 2, Szerokość: 4") - case ShipType.Type3 => println(s"Typ: $shipType, Długość: 1, Szerokość: 3") - case ShipType.Type4 => println(s"Typ: $shipType, Długość: 1, Szerokość: 2") - // Dodaj tutaj więcej typów statków, jeśli są potrzebne + if (!usedShips.contains(shipType)) { + ShipType.attributesOf(shipType) match { + case Some(attr) => + println(s"Typ: $shipType, Długość: ${attr.length}, Szerokość: ${attr.width}") + case None => + println(s"Typ: $shipType, brak dostępnych atrybutów.") } } } } -} +} \ No newline at end of file diff --git a/shipsgame/src/main/battleship/ShipType.scala b/shipsgame/src/main/battleship/ShipType.scala index d96416f..c45daa3 100644 --- a/shipsgame/src/main/battleship/ShipType.scala +++ b/shipsgame/src/main/battleship/ShipType.scala @@ -3,4 +3,15 @@ package main.battleship object ShipType extends Enumeration { type ShipType = Value val Type1, Type2, Type3, Type4 = Value + + case class ShipAttributes(length: Int, width: Int) + + private val shipAttributes: Map[ShipType, ShipAttributes] = Map( + Type1 -> ShipAttributes(1, 5), + Type2 -> ShipAttributes(2, 4), + Type3 -> ShipAttributes(1, 3), + Type4 -> ShipAttributes(1, 2) + ) + + def attributesOf(shipType: ShipType): Option[ShipAttributes] = shipAttributes.get(shipType) }