Advertisement
Guest User

lab

a guest
Apr 16th, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.83 KB | None | 0 0
  1. package o1.labyrintti
  2. import scala.collection.mutable.Buffer
  3. import scala.collection.mutable.Map
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10. class Labyrintti(val height: Int, val width: Int){
  11.     import Labyrintti._
  12.     import Labyrintti.Direction._
  13.  
  14.     private val squares = Map[Int, Buffer[Square]]() //jokainen Int arvo vastaa yhtä riviä ja valuena sillä on saman rivin kaikki ruutu-
  15.     for(y <- 0 to height - 1){
  16.       squares += y -> Buffer()
  17.       for(x <- 0 to width - 1){
  18.         squares(y) += new Square(x, y)
  19.       }
  20.     }
  21.    
  22.  
  23.     //Poistaa seinän ruudun x ja sen naapurin väliltä suunnassa dir
  24.     def removeWall(x: Square, dir: Direction):Unit = {
  25.       dir match {
  26.       case Up => {
  27.         require(!(x.y == height - 1), "Can't break top walls")
  28.         x.removeWall(dir.toString)
  29.         removeWall(getNeighbour(x, Up), Down)
  30.       }
  31.       case Right => {
  32.         require(!(x.x == width - 1), "can't break rightmost walls")
  33.         x.removeWall(dir.toString)
  34.         removeWall(getNeighbour(x, Right), Left)
  35.       }
  36.       case Down => {
  37.         require(!(x.y == 0), "can't break bottom walls")
  38.         x.removeWall(dir.toString)
  39.         removeWall(getNeighbour(x, Down), Up)
  40.       }
  41.       case Left => {
  42.         require(!(x.x == 0), "can't break leftmost walls")
  43.         x.removeWall(dir.toString)
  44.         removeWall(getNeighbour(x, Left), Right)
  45.       }
  46.       }
  47.     }
  48.    
  49.    
  50.     //poistaa seinän suunnasta dir, hyppää dir suunnan naapurin yli ja poistaa sitä seuraavasta ruudusta vastaavan seinän.
  51.     def createTunnel(x: Square, dir: Direction) = ???
  52.    
  53.    
  54.     //poistaa "maali" seinän ja asettaa kyseisen ruudun maaliksi
  55.     def removeFinishWall(x: Square, dir: Direction) = ???
  56.    
  57.    
  58.    
  59.     //selvittää ruudun naapurin suunnassa dir
  60.     def getNeighbour(x: Square, dir: Direction): Square = {
  61.       dir match{
  62.         case Up => {
  63.           require(!(x.y == height -1), "no neighbour for top row")
  64.           squares.apply(x.y + 1)(x.x)
  65.         }
  66.         case Right => {
  67.           require(!(x.x == width -1), "no neighbour for rightmost row")
  68.           squares.apply(x.y)(x.x + 1)
  69.         }
  70.         case Down => {
  71.           require(!(x.y == 0), "no neighbour for bottom row")
  72.           squares.apply(x.y - 1)(x.x)
  73.         }
  74.         case Left => {
  75.           require(!(x.x == 0), "no neighbour for leftmost row")
  76.           squares.apply(x.y)(x.x - 1)
  77.         }
  78.         }
  79.       }
  80.     //selvittää onko vielä ruutuja joista ei ole poistettu seiniä, eli onko jonkun ruudun seinä "true" kaikissa suunnissa.
  81.     def unvisitedSquares(): Boolean = {
  82.       var switch = false
  83.       for(y <- 0 to height - 1){
  84.         var x = squares(y)
  85.         for (ruutu <- x){
  86.           if (ruutu.wallUp.values.toList.count(_ == true) == 4) switch = true //yhtäkään ruudun sivua ei ole kaadettu -> vaihdetaan kytkin
  87.         }
  88.       }
  89.       switch
  90.     }
  91.    
  92.    
  93.    
  94. }
  95.  
  96. object Labyrintti {
  97.   /** Enumeration "type" for directions */
  98.   object Direction extends Enumeration {
  99.     type Direction = Value
  100.     val Up, Down, Left, Right = Value
  101.   }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement