forked from Jullija/Battleship
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
84 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
package main.battleship | ||
import Constants._ | ||
|
||
import scala.util.Random | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
package main.battleship | ||
// Constants.scala | ||
|
||
object Constants { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
package main.battleship | ||
import Constants._ | ||
object SimpleUser extends User { | ||
var board: Board = new Board | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
package main.battleship | ||
trait User { | ||
var board: Board | ||
var enemy_board: Board | ||
|