// This is part of an assignment to solve a puzzle similar to Futoshiki, it is called neighbors
// and an explanation of the game can be found at https://www.brainbashers.com/neighbourshelp.asp
/* Puzzle.object */
object Puzzle {
// The puzzle object retrieves the size directly from the puzzle it is currently
// set to parse. The size of the board can change between puzzles and this becomes
// an issue when trying to write unit tests for the solver.
private val lines = Utilities.getInput("src/puzzles/puzzle")
val puzzle:List[String] = lines.slice(2, lines.length)
val normalized_puzzle:List[String] = Utilities.getNormalizedPuzzle(puzzle)
val size:Int = Utilities.getSize(lines.slice(1, 2)(0))
def getPuzzle: List[String] = { puzzle }
}
/* Square.scala */
// The square uses the Puzzle.size property to create the correct default values
class Square(val x:Int, val y:Int,
val values:List[Int] = (1 to Puzzle.size).toList,
val neighbors:List[Direction] = List[Direction](),
val solved:Boolean = false)
{
// This method uses the Puzzle.size to filter out incorrect neighbor values
def getPossibleValuesForNeighbors:List[Int] = {
val neighbor_values_lists = for(value <- values) yield List(value + 1, value - 1)
neighbor_values_lists
.flatten
.distinct
.filter((value) => value > 0 && value <= Puzzle.size)
.sortWith(_ < _)
}
/* SquareTest */
// This test would fail if the currently parsed board is 2x2 as 4 is not a valid neighbor
// I think being able to pass a specific puzzle to my test class would solve the issue
// but I'm not sure how I would do that and if it's the correct approach.
test("it is possible to get all possible neighbor values from a square") {
val square_values = List(1, 3)
val square_with_values = new Square(1, 1, values = square_values)
val neighbor_values = square_with_values.getPossibleValuesForNeighbors
assert(neighbor_values === List(2, 4))
}
Comments
0 B
|👍
/👎
0 B
|👍
/👎