Skip to content

Instantly share code, notes, and snippets.

@kamiyaowl
Created February 21, 2014 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamiyaowl/9141885 to your computer and use it in GitHub Desktop.
Save kamiyaowl/9141885 to your computer and use it in GitHub Desktop.
Scalaで迷路生成(棒倒し)
import scala.util.Random
object Stick {
def printMaze(m:Map[(Int,Int), String])(implicit size:(Int,Int)) = {
val w = size._1
val h = size._2
for(j <- 0 until h)
{
for(i <- 0 until w){
m.get((i,j)) match {
case Some(s) => print(s)
case None => print(" ")
}
}
println("")
}
}
def aroundWallMap(implicit size:(Int,Int), wall:String) = {
val w = size._1
val h = size._2
(for(i <- 0 until w) yield ((i,0) -> wall)) ++ (for(i <- 0 until w) yield ((i,h - 1) -> wall)) ++ (for(i <- 0 until h) yield ((0,i) -> wall)) ++ (for(i <- 0 until h) yield ((w - 1, i) -> wall)).toMap
}
def constantWallMap(implicit size:(Int,Int), wall:String) = {
val w = size._1
val h = size._2
(for(i <- 0 to ((w - 3) / 2) ; j <- 0 to ((h - 3) / 2)) yield ((i * 2 + 2,j * 2 + 2) -> wall)).toMap
}
def wallStick(m:Map[(Int,Int), String])(implicit wall:String) = {
var dst:Map[(Int,Int),String] = Map()
m foreach(p => {
var t = (new Random).shuffle(List((p._1._1, p._1._2 - 1), (p._1._1, p._1._2 + 1), (p._1._1 - 1, p._1._2), (p._1._1 + 1, p._1._2)))
while(m.contains(t.head)){
t = t.tail
}
dst += t.head -> wall
})
dst
}
def main(args:Array[String]) = {
implicit val w = 40 + 3//2n
implicit val h = 20 + 3
implicit val size = (w,h)
implicit val wall = "#"
val a = constantWallMap
val maze = a ++ wallStick(a) ++ aroundWallMap ++ Map((1,1) -> "S",(w - 2,h - 2) -> "G")
printMaze(maze)
}
}
###########################################
#S# # # # # #
# # # # ### ### ### ### ### # ### ### ### #
# # # # # # # # # # #
# # ### ##### # ### ### # # ### # # #######
# # # # # # # #
# # # ### # ##### # ### # # ####### # # ###
# # # # # # # # # # # #
# # # # # # ### ### # ### ### # ##### # ###
# # # # # # # # # # #
# # # # ### # ######### # # # # # ### ### #
# # # # # # # # # #
### ### # ##### # ##### ##### ### ##### ###
# # # # # # # # # #
# ### ####### # # # # ### ### ### ##### # #
# # # # # # # # # # # # # #
# # # ### ### ##### ### # # ##### # # ### #
# # # # # # # # # #
# # ##### ##### ### ### # ##### # ### # ###
# # # # # # # # # # #
### # ### ##### ##### # # # # # ####### # #
# # # # # # # # # # # G#
###########################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment