forked from MarkCLewis/ProblemSolvingUsingScala
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeVis.scala
More file actions
76 lines (66 loc) · 2.23 KB
/
MazeVis.scala
File metadata and controls
76 lines (66 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import scalafx.Includes._
import scalafx.application._
import scalafx.scene.Scene
import scalafx.scene.control._
import scalafx.scene.shape._
import scalafx.scene.canvas._
import scalafx.scene.input._
import scalafx.scene.paint._
import scalafx.event.ActionEvent
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
val size = 600
val maze = Array(Array( 0,-1, 0, 0, 0, 0, 0, 0, 0, 0),
Array( 0,-1, 0,-1,-1,-1,-1,-1,-1, 0),
Array( 0,-1, 0,-1, 0, 0,-1, 0, 0, 0),
Array( 0,-1, 0,-1,-1, 0,-1, 0,-1, 0),
Array( 0, 0, 0,-1, 0, 0, 0, 0,-1, 0),
Array( 0,-1, 0,-1, 0,-1,-1,-1,-1,-1),
Array( 0,-1, 0,-1, 0, 0, 0, 0, 0, 0),
Array( 0,-1, 0,-1, 0,-1, 0,-1, 0, 0),
Array( 0,-1, 0,-1, 0,-1, 0,-1,-1,-1),
Array( 0,-1, 0, 0, 0,-1, 0, 0, 0, 0))
def renderMaze(m:Array[Array[Int]], x:Int, y:Int, gc:GraphicsContext):Unit = {
for(r <- 0 until m.length; c <- 0 until m(r).length) {
m(r)(c) match {
case 0 => gc.fill = Color.White
case -1 => gc.fill = Color.Black
case -2 => gc.fill = Color.Blue
}
gc.fillRect(c*size/m(r).length, r*size/m.length, size/m(r).length, size/m.length)
}
gc.fill = Color.Red
gc.fillOval(y*size/m(x).length, x*size/m.length, size/m(x).length, size/m.length)
}
def shortestPath(m:Array[Array[Int]], x:Int, y:Int, ex:Int, ey:Int, gc:GraphicsContext):Int = {
if(x==ex && y==ey) 0
else if(x<0 || x>=maze.length || y<0 || y>=maze(x).length || maze(x)(y)<0) {
1000000000
} else {
maze(x)(y) = -2
Platform.runLater(renderMaze(m, x, y, gc))
Thread.sleep(200)
val answer = (shortestPath(m, x+1, y, ex, ey, gc) min
shortestPath(m, x-1, y, ex, ey, gc) min
shortestPath(m, x, y+1, ex, ey, gc) min
shortestPath(m, x, y-1, ex, ey, gc))+1
Platform.runLater(renderMaze(m, x, y, gc))
Thread.sleep(200)
maze(x)(y) = 0
answer
}
}
val app = new JFXApp {
stage = new JFXApp.PrimaryStage {
title = "Canvas"
scene = new Scene(size, size) {
val canvas = new Canvas(size, size)
val gc = canvas.graphicsContext2D
content = canvas
Future {
println(shortestPath(maze, 0, 0, 9, 9, gc))
}
}
}
}
app.main(args)