Skip to content

Commit

Permalink
Add output maze size and time
Browse files Browse the repository at this point in the history
  • Loading branch information
matsuyoshi30 committed Dec 14, 2019
1 parent cad79a5 commit 88282b5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
15 changes: 11 additions & 4 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ const (
DOWN
)

func (g *Game) Loop() error {
type Result int

const (
GOALED Result = iota
STOPPED
)

func (g *Game) Loop() (Result, error) {
e := make(chan Event)
go input(g.screen, e)

Expand All @@ -65,10 +72,10 @@ func (g *Game) Loop() error {
case ev := <-e:
switch ev {
case EXIT:
return nil
return STOPPED, nil
case RIGHT:
if g.maze.CheckGoal() { // gaol
return nil
return GOALED, nil
}
if g.maze.CheckMaze(RIGHT) {
g.maze.MoveCurrent(RIGHT)
Expand All @@ -89,7 +96,7 @@ func (g *Game) Loop() error {
}
}

return nil
return STOPPED, nil
}

func input(s tcell.Screen, e chan<- Event) {
Expand Down
22 changes: 18 additions & 4 deletions gomaze.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func initScreen() (tcell.Screen, error) {
return s, nil
}

func startGame(width, height int, seed bool, format bool) error {
func startGame(width, height int, seed bool, format bool) (Result, int, int, error) {
s, err := initScreen()
if err != nil {
return err
return STOPPED, 0, 0, err
}
defer s.Fini()

Expand All @@ -38,7 +38,8 @@ func startGame(width, height int, seed bool, format bool) error {
maze: m,
}

return game.Loop()
res, err := game.Loop()
return res, w / 2, h, err
}

func main() {
Expand Down Expand Up @@ -95,7 +96,20 @@ func main() {
wi := c.Bool("format")

if sc {
return startGame(tw, th, se, wi)
start := time.Now()
res, w, h, err := startGame(tw, th, se, wi)
if err != nil {
return err
}
end := time.Now()

if res == GOALED {
fmt.Println("Congrats!")
fmt.Printf("[Maze size] Width: %d / Height: %d\n", w, h)
fmt.Printf("[Your time] %s\n", end.Sub(start))
}

return nil
} else {
m := NewMaze(tw, th, se, wi)
m.printMaze()
Expand Down

0 comments on commit 88282b5

Please sign in to comment.