-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifteen-puzzle.go
59 lines (44 loc) · 1.24 KB
/
fifteen-puzzle.go
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
package main
import (
"github.com/HugoMFernandes/go-fifteen-puzzle/game"
"github.com/HugoMFernandes/go-fifteen-puzzle/game/io"
"flag"
"github.com/nsf/termbox-go"
)
const defaultPuzzleWidth = 4
const defaultPuzzleHeight = 4
func main() {
// Parse dimension arguments from command args
puzzleWidth, puzzleHeight := parseDimensionArguments()
// Initialize termbox
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetInputMode(termbox.InputEsc)
// Initialize input handler
input := io.CreateInputHandler()
// Initialize game renderer
renderer := io.CreateRenderer()
// Render welcome message
renderer.RenderWelcomeMessage()
input.ReadKey()
renderer.ClearScreen()
// Start game
game := game.CreateGame(puzzleWidth, puzzleHeight, renderer, input)
game.Run()
if game.Puzzle().IsSolved() {
// Render victory message
renderer.RenderVictoryMessage(game.Puzzle(), game.Stats())
input.ReadKey()
}
// Clear the screen before we quit
renderer.ClearScreen()
}
func parseDimensionArguments() (int, int) {
puzzleWidthPtr := flag.Int("w", defaultPuzzleWidth, "a puzzle width")
puzzleHeightPtr := flag.Int("h", defaultPuzzleHeight, "a puzzle height")
flag.Parse()
return *puzzleWidthPtr, *puzzleHeightPtr
}