Created
February 25, 2014 00:05
-
-
Save kamiyaowl/9199976 to your computer and use it in GitHub Desktop.
scalaで迷路(穴掘り法)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.util.Random | |
object Digging { | |
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 canDig(src:Map[(Int,Int),String])(target:(Int,Int), from:(Int,Int))(implicit size:(Int,Int)) = { | |
if(!src.contains(target)) false | |
else { | |
val dx = target._1 - from._1 | |
val dy = target._2 - from._2 | |
var suggest = aroundPoint(target)(size) filter(s => { | |
src.get(s) match { | |
case Some(n) => true | |
case None => false | |
} | |
}) | |
suggest.length == 3 | |
} | |
} | |
def intersect[T1,T2](m1:Map[T1,T2],m2:Map[T1,T2]) : Map[T1,T2] = { | |
(m1.toList intersect m2.toList).toMap | |
} | |
def dig(pos:(Int,Int), bsrc:Map[(Int,Int),String])(implicit size:(Int,Int)) : Map[(Int,Int),String] = { | |
var src = bsrc | |
src -= pos | |
var arounds = (new Random).shuffle(aroundPoint(pos)(size)) | |
while(!arounds.isEmpty){ | |
if(canDig(src)(arounds.head,pos)(size)) src = intersect(src,dig(arounds.head,src)(size)) | |
arounds = arounds.tail | |
} | |
src | |
} | |
def fill(implicit size:(Int,Int), wall:String) = (for(i <- 0 until size._1 ; j <- 0 until size._2) yield ((i,j) -> wall)).toMap | |
def main(args:Array[String]) = { | |
implicit val w = 40 | |
implicit val h = 20 | |
implicit val size = (w,h) | |
implicit val wall = "#" | |
val r = new Random | |
val dst = dig((1 + r.nextInt(w - 2),1 + r.nextInt(h - 2)),fill) ++ Map((1,1) -> "S", (w - 2,h - 2) -> "F") | |
printMaze(dst) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################## | |
#S# # # # # # ## # # | |
# # # ## # # # # ## ## ## # | |
# ## ## ### # # # ## ### ## # # | |
# # # ## # # # # ### # # # # | |
# # ## #### ## # ### # # #### ## | |
# # # # # # # # # # # # # ## | |
# # # # ### # # # # # ## # # | |
## # ### # # # # # ## ### # # # | |
# ## ### # ## # # # ## ## # # | |
# # ### # # ## # ### # # # | |
# # ## # # # # # ## # # ## ## | |
# ## # # # # # # ## # # # # # # | |
# # ### # # # ## ### # # | |
### # # # # ######## # # # # # | |
# # # # ### # # # ## # # # # ## | |
# ### # # # # # # # ## # # | |
# # # ## # # # ## # ## ### ## ## | |
## # # # # # # F# | |
######################################## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment