Skip to content

Commit

Permalink
feat(board): implement isFull
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablito2020 committed Apr 16, 2022
1 parent d3912f1 commit 07476d1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/kotlin/board/Board.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package board

import board.exceptions.InvalidPositionException
import board.exceptions.InvalidRowException
import movements.Position

class Board(private val row: Int, private val column: Int) {
Expand Down Expand Up @@ -36,4 +37,10 @@ class Board(private val row: Int, private val column: Int) {
board[position.row][position.column] = cell
}

fun isFull(row: Int): Boolean {
if (row < 0 || row >= this.row)
throw InvalidRowException()
return board[row].all { x -> x != Cell.EMPTY }
}

}
5 changes: 5 additions & 0 deletions src/main/kotlin/board/exceptions/InvalidRowException.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package board.exceptions

class InvalidRowException : Throwable() {

}
31 changes: 31 additions & 0 deletions src/test/kotlin/board/BoardTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package board

import board.exceptions.InvalidPositionException
import board.exceptions.InvalidRowException
import movements.Position
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand All @@ -17,6 +18,8 @@ class BoardTest {
board = Board(20, 10)
}

// isInside

@Test
fun `Out of bounds negative row position isn't inside board`(){
assertFalse(board.isInside(Position(-1, 2)))
Expand Down Expand Up @@ -52,6 +55,8 @@ class BoardTest {
assertTrue(board.isInside(Position(2, 2)))
}

// isEmpty

@Test
fun `Accessing invalid position on empty throws invalid position exception`() {
assertThrows<InvalidPositionException> {board.isEmpty(Position(-1, 2)) }
Expand All @@ -68,4 +73,30 @@ class BoardTest {
assertFalse(board.isEmpty(Position(0, 0)))
}

// isFull

@Test
fun `Accessing invalid negative throws InvalidRowException`() {
assertThrows<InvalidRowException>{board.isFull(-1)}
}

@Test
fun `Accessing invalid outside row throws InvalidRowException`() {
assertThrows<InvalidRowException>{board.isFull(20)}
}

@Test
fun `Accessing not full board returns false`() {
assertFalse(board.isFull(0))
}

@Test
fun `Accessing full board returns true`() {
for (column in 0 until 10)
board.writePosition(Cell.IBlock, Position(0, column))
assertTrue(board.isFull(0))
}

// TODO: isEmpty(row), writePosition(Cell, position), clear(row)

}

0 comments on commit 07476d1

Please sign in to comment.