Skip to content

Commit

Permalink
addShip modified and minor changes in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam0s007 committed Jun 22, 2023
1 parent c33530b commit 677af7f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
14 changes: 10 additions & 4 deletions shipsgame/src/main/scala/Board.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 ={
Expand Down
24 changes: 19 additions & 5 deletions shipsgame/src/test/scala/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}

0 comments on commit 677af7f

Please sign in to comment.