Skip to content

Commit

Permalink
shipFactory and ShipType modified
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam0s007 committed Jun 17, 2023
1 parent 6059f8d commit 4ffeb0c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 40 deletions.
24 changes: 24 additions & 0 deletions shipsgame/src/main/battleship/Main.scala
Original file line number Diff line number Diff line change
@@ -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()

}
}

Expand Down
4 changes: 4 additions & 0 deletions shipsgame/src/main/battleship/Ship.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
70 changes: 30 additions & 40 deletions shipsgame/src/main/battleship/ShipFactory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}
}
}
}
}
11 changes: 11 additions & 0 deletions shipsgame/src/main/battleship/ShipType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 4ffeb0c

Please sign in to comment.