-
Notifications
You must be signed in to change notification settings - Fork 4
/
fifteen_puzzle.go
164 lines (151 loc) · 2.58 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"fmt"
"io"
"math/rand"
"strings"
"time"
)
func main() {
var p Puzzle
p.Play()
}
type Puzzle struct {
grid [4][4]int
x, y int
}
func (p *Puzzle) Play() {
var input string
rand.Seed(time.Now().UnixNano())
p.reset()
loop:
for {
p.drawGrid()
fmt.Printf("\nenter a move: ")
_, err := fmt.Scanln(&input)
fmt.Println("")
if err == io.EOF {
break
}
input = strings.TrimSpace(input)
input = strings.ToLower(input)
switch input {
case "n":
p.randomize()
case "w", "a", "s", "d":
if !p.move(p.conv(input[0])) {
fmt.Printf("cannot move there!\n\n")
continue
}
case "h":
fmt.Printf("wasd: move up/left/down/right\n")
fmt.Printf("n: start a new game\n")
fmt.Printf("q: quit\n")
fmt.Printf("\n\n")
case "":
case "q":
fmt.Printf("quitting\n\n")
break loop
default:
fmt.Printf("unknown move, enter again!\n\n")
continue
}
if p.checkSolution() {
fmt.Printf("you win!\n\n")
break
}
}
}
func (*Puzzle) conv(c byte) byte {
switch c {
case 'w':
return 'u'
case 'a':
return 'l'
case 's':
return 'd'
case 'd':
return 'r'
}
return 0
}
func (p *Puzzle) randomize() {
const r = "lrdu"
const rr = "rlud"
for i := 0; i < 1000; i++ {
n := rand.Intn(len(r))
if !p.move(r[n]) {
p.move(rr[n])
}
}
}
func (p *Puzzle) move(dir uint8) bool {
g := p.grid[:][:]
x, y := p.x, p.y
switch dir {
case 'l':
if y-1 < 0 {
return false
}
g[x][y], g[x][y-1] = g[x][y-1], g[x][y]
p.y--
case 'r':
if y+1 > 3 {
return false
}
g[x][y], g[x][y+1] = g[x][y+1], g[x][y]
p.y++
case 'd':
if x+1 > 3 {
return false
}
g[x][y], g[x+1][y] = g[x+1][y], g[x][y]
p.x++
case 'u':
if x-1 < 0 {
return false
}
g[x][y], g[x-1][y] = g[x-1][y], g[x][y]
p.x--
}
return true
}
func (p *Puzzle) reset() {
n := 1
for i := range p.grid {
for j := range p.grid[i] {
p.grid[i][j], n = n, n+1
}
}
p.x, p.y = 3, 3
p.grid[p.x][p.y] = -1
p.randomize()
}
func (p *Puzzle) drawGrid() {
for i := range p.grid {
for j := range p.grid {
if p.grid[i][j] < 0 {
fmt.Printf("%2c ", '.')
} else {
fmt.Printf("%2v ", p.grid[i][j])
}
}
fmt.Println("")
}
}
func (p *Puzzle) checkSolution() bool {
n := 1
loop:
for i := range p.grid {
for j := range p.grid[i] {
if p.grid[i][j] != n {
if i == 3 && j == 3 && p.grid[i][j] == -1 {
break loop
}
return false
}
n++
}
}
return true
}