Skip to content

Commit

Permalink
refine
Browse files Browse the repository at this point in the history
  • Loading branch information
matsuyoshi30 committed Dec 15, 2019
1 parent 5dbb425 commit 28a628f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
26 changes: 14 additions & 12 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ func (g *Game) Loop() (Result, error) {
go input(g.screen, e)

if g.bfs {
g.queue = make([]*Point, 0)
g.queue = append(g.queue, g.maze.Points[1][1])
g.maze.Points[1][1].status = VISITED
} else {
g.stack = make([]*Point, 0)
g.stack = append(g.stack, g.maze.Points[1][1])
g.maze.Points[1][1].status = VISITED
}
Expand Down Expand Up @@ -157,18 +155,20 @@ func (g *Game) next() Result {
}
}

var dx = [4]int{1, -1, 0, 0}
var dy = [4]int{0, 0, 1, -1}
var dx = [4]int{1, 0, 0, -1}
var dy = [4]int{0, 1, -1, 0}

func (g *Game) bfsearch() Result {
n, queue := g.queue[0], g.queue[1:]
for i := 0; i < 4; i++ {
if g.maze.Points[n.y+dy[i]][n.x+dx[i]].status == GOAL {
p := g.maze.Points[n.y+dy[i]][n.x+dx[i]]

if p.status == GOAL {
return GOALED
}
if g.maze.Points[n.y+dy[i]][n.x+dx[i]].status == PATH {
g.maze.Points[n.y+dy[i]][n.x+dx[i]].status = VISITED
g.queue = append(g.queue, g.maze.Points[n.y+dy[i]][n.x+dx[i]])
if p.status == PATH {
p.status = VISITED
g.queue = append(g.queue, p)
return NOTGOALED
}
}
Expand All @@ -179,12 +179,14 @@ func (g *Game) bfsearch() Result {
func (g *Game) dfsearch() Result {
n, stack := g.stack[0], g.stack[1:]
for i := 0; i < 4; i++ {
if g.maze.Points[n.y+dy[i]][n.x+dx[i]].status == GOAL {
p := g.maze.Points[n.y+dy[i]][n.x+dx[i]]

if p.status == GOAL {
return GOALED
}
if g.maze.Points[n.y+dy[i]][n.x+dx[i]].status == PATH {
g.maze.Points[n.y+dy[i]][n.x+dx[i]].status = VISITED
g.stack = append([]*Point{g.maze.Points[n.y+dy[i]][n.x+dx[i]]}, g.stack...)
if p.status == PATH {
p.status = VISITED
g.stack = append([]*Point{p}, g.stack...)
return NOTGOALED
}
}
Expand Down
8 changes: 6 additions & 2 deletions gomaze.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ func startSearch(width, height int, seed bool, format bool, bfs, dfs bool) (Resu
game := Game{
screen: s,
maze: m,
bfs: bfs,
dfs: dfs,
ticker: ticker,
}

if bfs {
game.queue = make([]*Point, 0)
} else {
game.stack = make([]*Point, 0)
}

res, err := game.Loop()
return res, err
}
Expand Down

0 comments on commit 28a628f

Please sign in to comment.