Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package o1.labyrintti
- import scala.collection.mutable.Buffer
- import scala.collection.mutable.Map
- class Labyrintti(val height: Int, val width: Int){
- import Labyrintti._
- import Labyrintti.Direction._
- private val squares = Map[Int, Buffer[Square]]() //jokainen Int arvo vastaa yhtä riviä ja valuena sillä on saman rivin kaikki ruutu-
- for(y <- 0 to height - 1){
- squares += y -> Buffer()
- for(x <- 0 to width - 1){
- squares(y) += new Square(x, y)
- }
- }
- //Poistaa seinän ruudun x ja sen naapurin väliltä suunnassa dir
- def removeWall(x: Square, dir: Direction):Unit = {
- dir match {
- case Up => {
- require(!(x.y == height - 1), "Can't break top walls")
- x.removeWall(dir.toString)
- removeWall(getNeighbour(x, Up), Down)
- }
- case Right => {
- require(!(x.x == width - 1), "can't break rightmost walls")
- x.removeWall(dir.toString)
- removeWall(getNeighbour(x, Right), Left)
- }
- case Down => {
- require(!(x.y == 0), "can't break bottom walls")
- x.removeWall(dir.toString)
- removeWall(getNeighbour(x, Down), Up)
- }
- case Left => {
- require(!(x.x == 0), "can't break leftmost walls")
- x.removeWall(dir.toString)
- removeWall(getNeighbour(x, Left), Right)
- }
- }
- }
- //poistaa seinän suunnasta dir, hyppää dir suunnan naapurin yli ja poistaa sitä seuraavasta ruudusta vastaavan seinän.
- def createTunnel(x: Square, dir: Direction) = ???
- //poistaa "maali" seinän ja asettaa kyseisen ruudun maaliksi
- def removeFinishWall(x: Square, dir: Direction) = ???
- //selvittää ruudun naapurin suunnassa dir
- def getNeighbour(x: Square, dir: Direction): Square = {
- dir match{
- case Up => {
- require(!(x.y == height -1), "no neighbour for top row")
- squares.apply(x.y + 1)(x.x)
- }
- case Right => {
- require(!(x.x == width -1), "no neighbour for rightmost row")
- squares.apply(x.y)(x.x + 1)
- }
- case Down => {
- require(!(x.y == 0), "no neighbour for bottom row")
- squares.apply(x.y - 1)(x.x)
- }
- case Left => {
- require(!(x.x == 0), "no neighbour for leftmost row")
- squares.apply(x.y)(x.x - 1)
- }
- }
- }
- //selvittää onko vielä ruutuja joista ei ole poistettu seiniä, eli onko jonkun ruudun seinä "true" kaikissa suunnissa.
- def unvisitedSquares(): Boolean = {
- var switch = false
- for(y <- 0 to height - 1){
- var x = squares(y)
- for (ruutu <- x){
- if (ruutu.wallUp.values.toList.count(_ == true) == 4) switch = true //yhtäkään ruudun sivua ei ole kaadettu -> vaihdetaan kytkin
- }
- }
- switch
- }
- }
- object Labyrintti {
- /** Enumeration "type" for directions */
- object Direction extends Enumeration {
- type Direction = Value
- val Up, Down, Left, Right = Value
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement