Skip to content

Commit

Permalink
ShipFactory TryAttack
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam0s007 committed Jun 17, 2023
1 parent a909f9c commit 6059f8d
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 10 deletions.
16 changes: 9 additions & 7 deletions shipsgame/src/main/battleship/Board.scala
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
}
}





}
1 change: 1 addition & 0 deletions shipsgame/src/main/battleship/ComputerUser.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package main.battleship
import Constants._

import scala.util.Random
Expand Down
1 change: 1 addition & 0 deletions shipsgame/src/main/battleship/Constants.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package main.battleship
// Constants.scala

object Constants {
Expand Down
11 changes: 9 additions & 2 deletions shipsgame/src/main/battleship/Main.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

import main.battleship

object Main {
def main(args: Array[String]): Unit = {
Expand All @@ -10,4 +10,11 @@ object Main {
//vector
// ship
// user
//
//


//ShipFactory : createShip
//tryAttack - też
//
//
//a julia:testy
3 changes: 2 additions & 1 deletion shipsgame/src/main/battleship/Ship.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down
60 changes: 60 additions & 0 deletions shipsgame/src/main/battleship/ShipFactory.scala
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
}
}
}
}
}
1 change: 1 addition & 0 deletions shipsgame/src/main/battleship/SimpleUser.scala
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
Expand Down
1 change: 1 addition & 0 deletions shipsgame/src/main/battleship/User.scala
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
Expand Down

0 comments on commit 6059f8d

Please sign in to comment.