From 6059f8d307384a330db8bf85008c12af8289e859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Bi=C5=9Bta?= Date: Sat, 17 Jun 2023 14:05:12 +0200 Subject: [PATCH] ShipFactory TryAttack --- shipsgame/src/main/battleship/Board.scala | 16 ++--- .../src/main/battleship/ComputerUser.scala | 1 + shipsgame/src/main/battleship/Constants.scala | 1 + shipsgame/src/main/battleship/Main.scala | 11 +++- shipsgame/src/main/battleship/Ship.scala | 3 +- .../src/main/battleship/ShipFactory.scala | 60 +++++++++++++++++++ .../src/main/battleship/SimpleUser.scala | 1 + shipsgame/src/main/battleship/User.scala | 1 + 8 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 shipsgame/src/main/battleship/ShipFactory.scala diff --git a/shipsgame/src/main/battleship/Board.scala b/shipsgame/src/main/battleship/Board.scala index 9e193a9..616bb22 100644 --- a/shipsgame/src/main/battleship/Board.scala +++ b/shipsgame/src/main/battleship/Board.scala @@ -1,8 +1,7 @@ +package main.battleship import scala.collection.mutable -import scala.math import scala.math.abs -import main.battleship.ShipType import main.battleship.ShipType.ShipType class Board { @@ -51,12 +50,15 @@ class Board { checked.contains(x, y) } - def tryAttack(x: Int, y: Int): Boolean ={ - occupied.contains((x, y)); + def tryAttack(x: Int, y: Int): Boolean = { + if (occupied.contains((x, y))) { + occupied.remove((x, y)) + checked.add((x, y)) //wykorzystane pole wiec dodajemy do checked + true + } else { + false + } } - - - } diff --git a/shipsgame/src/main/battleship/ComputerUser.scala b/shipsgame/src/main/battleship/ComputerUser.scala index 8905019..136b105 100644 --- a/shipsgame/src/main/battleship/ComputerUser.scala +++ b/shipsgame/src/main/battleship/ComputerUser.scala @@ -1,3 +1,4 @@ +package main.battleship import Constants._ import scala.util.Random diff --git a/shipsgame/src/main/battleship/Constants.scala b/shipsgame/src/main/battleship/Constants.scala index edd66ae..5089b3f 100644 --- a/shipsgame/src/main/battleship/Constants.scala +++ b/shipsgame/src/main/battleship/Constants.scala @@ -1,3 +1,4 @@ +package main.battleship // Constants.scala object Constants { diff --git a/shipsgame/src/main/battleship/Main.scala b/shipsgame/src/main/battleship/Main.scala index e45f1a0..267fefe 100644 --- a/shipsgame/src/main/battleship/Main.scala +++ b/shipsgame/src/main/battleship/Main.scala @@ -1,4 +1,4 @@ - +import main.battleship object Main { def main(args: Array[String]): Unit = { @@ -10,4 +10,11 @@ object Main { //vector // ship // user -// \ No newline at end of file +// + + +//ShipFactory : createShip +//tryAttack - też +// +// +//a julia:testy diff --git a/shipsgame/src/main/battleship/Ship.scala b/shipsgame/src/main/battleship/Ship.scala index 977c3e7..6cb0b7d 100644 --- a/shipsgame/src/main/battleship/Ship.scala +++ b/shipsgame/src/main/battleship/Ship.scala @@ -1,4 +1,5 @@ -import main.battleship.ShipType +package main.battleship + class Ship(val shipType: ShipType.Value, val length: Int, val width: Int) { val size: Int = length * width diff --git a/shipsgame/src/main/battleship/ShipFactory.scala b/shipsgame/src/main/battleship/ShipFactory.scala new file mode 100644 index 0000000..e822066 --- /dev/null +++ b/shipsgame/src/main/battleship/ShipFactory.scala @@ -0,0 +1,60 @@ +package main.battleship + +import scala.collection.mutable +class ShipFactory { + val usedShips: mutable.HashSet[ShipType.Value] = mutable.HashSet.empty + + def checkIfUsed(shipType: ShipType.Value): Boolean = { + usedShips.contains(shipType) + } + + def addToUsed(shipType: ShipType.Value): Unit = { + usedShips += shipType + } + def removeFromUsed(shipType: ShipType.Value):Unit = { + usedShips.remove(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 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 + } + Some(ship) + } + } + + + 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 + } + } + } + } +} diff --git a/shipsgame/src/main/battleship/SimpleUser.scala b/shipsgame/src/main/battleship/SimpleUser.scala index 9241050..1f38057 100644 --- a/shipsgame/src/main/battleship/SimpleUser.scala +++ b/shipsgame/src/main/battleship/SimpleUser.scala @@ -1,3 +1,4 @@ +package main.battleship import Constants._ object SimpleUser extends User { var board: Board = new Board diff --git a/shipsgame/src/main/battleship/User.scala b/shipsgame/src/main/battleship/User.scala index 7c1dfd7..3e48fae 100644 --- a/shipsgame/src/main/battleship/User.scala +++ b/shipsgame/src/main/battleship/User.scala @@ -1,3 +1,4 @@ +package main.battleship trait User { var board: Board var enemy_board: Board