Moortiii icon

Neighbors - Unit Test

Moortiii | PRO | 09/14/19 11:14:07 AM UTC | 0 ⭐ | 7019 👁️ | Never ⏰ | []
Scala |

2.01 KB

|

None

|

0 👍

/

0 👎

// 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

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎