From 677af7fedc41b86542d169784c907268d91203cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Bi=C5=9Bta?= Date: Thu, 22 Jun 2023 15:02:21 +0200 Subject: [PATCH] addShip modified and minor changes in tests --- shipsgame/src/main/scala/Board.scala | 14 ++++++++++---- shipsgame/src/test/scala/Test.scala | 24 +++++++++++++++++++----- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/shipsgame/src/main/scala/Board.scala b/shipsgame/src/main/scala/Board.scala index edb6def..042cd18 100644 --- a/shipsgame/src/main/scala/Board.scala +++ b/shipsgame/src/main/scala/Board.scala @@ -15,15 +15,21 @@ class Board{ occupied.isEmpty; } - def addShip(xs: Int, ys: Int, xe: Int, ye: Int, shipType: ShipType): Unit ={ - var length = abs(ys - ye); - var width = abs(xs - xe); - ships.addOne(new Ship(shipType, length, width)); + def addShip(xs: Int, ys: Int, xe: Int, ye: Int, ship: Option[Ship]): Boolean ={ +// var length = abs(ys - ye); to jest w shipFactory +// var width = abs(xs - xe); to jest w shipFactory + //jesli ship jest none to nie dodajemy statku, tylko wypisujemy komunikat ze statek nie zostal dodany + if (ship == None){ + println("Statek nie zostal dodany") + return false + } + ships.addOne(ship.get); for (i <- math.min(xs, xe) to math.max(xs, xe)){ for (j <- math.min(ys, ye) to math.max(ys, ye)){ occupied.add((i, j)); } } + true } def printBoard(): Unit ={ diff --git a/shipsgame/src/test/scala/Test.scala b/shipsgame/src/test/scala/Test.scala index 5b89f27..469ff8d 100644 --- a/shipsgame/src/test/scala/Test.scala +++ b/shipsgame/src/test/scala/Test.scala @@ -12,25 +12,39 @@ class Test extends AnyFunSuite{ val ship2 = shipFactory.createShip("Type2"); val ship3 = shipFactory.createShip("Type3"); val ship4 = shipFactory.createShip("Type4"); - + val ship5 = shipFactory.createShip("TypeNotExisting"); assert(ship1.get.size === 5) assert(ship2.get.size === 8) assert(ship3.get.size === 3) assert(ship4.get.size === 2) + assert(ship5 === None) } - test("addingOnBoard"){ - var board = new Board + test("addingOnBoard"){//dodajmy komentarze do wszystkich wywolan + + val board = new Board val shipFactory = new ShipFactory - board.addShip(2, 2, 6, 2, Type1) + val ship = shipFactory.createShip("Type1") + //sprawdzenie czy None zostanie dodane na plansze + assert(board.addShip(2, 2, 6, 2, None) == false) + //sprawdzenie czy statek zostanie dodany na plansze + assert(board.addShip(2, 2, 6, 2, ship) == true) + //sprawdzenie czy plansza nie jest pusta assert(board.isEmpty() === false) + //sprawdzenie czy atak sie powiodl assert(board.tryAttack(4, 2) === true) + //sprawdzenie czy atak na to samo pole sie nie powiedzie + assert(board.tryAttack(4, 2) === false) + //sprawdzenie czy atak na pole bez statku sie nie powiedzie assert(board.tryAttack(7, 8) === false) + //sprawdzenie czy occupied zawiera wspolrzedne statku assert(board.occupied.contains(3, 2)) + //sprawdzenie czy checked zawiera wspolrzedne poprzednio zaaatakowane assert(board.alreadyChecked(4, 2) === true) - + //sprawdzenie czy checked nie zawiera wspolrzednych zaatakowanych z niepowodzeniem + assert(board.alreadyChecked(7, 8) === false) } }