forked from danicat/pacgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
236 lines (201 loc) · 3.71 KB
/
main.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"github.com/danicat/simpleansi"
)
type sprite struct {
row int
col int
}
var player sprite
var ghosts []*sprite
var maze []string
var score int
var numDots int
var lives = 1
func loadMaze(file string) error {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
maze = append(maze, line)
}
for row, line := range maze {
for col, char := range line {
switch char {
case 'P':
player = sprite{row, col}
case 'G':
ghosts = append(ghosts, &sprite{row, col})
case '.':
numDots++
}
}
}
return nil
}
func printScreen() {
simpleansi.ClearScreen()
for _, line := range maze {
for _, chr := range line {
switch chr {
case '#':
fallthrough
case '.':
fmt.Printf("%c", chr)
default:
fmt.Print(" ")
}
}
fmt.Println()
}
simpleansi.MoveCursor(player.row, player.col)
fmt.Print("P")
for _, g := range ghosts {
simpleansi.MoveCursor(g.row, g.col)
fmt.Print("G")
}
simpleansi.MoveCursor(len(maze)+1, 0)
fmt.Println("Score:", score, "\tLives:", lives)
}
func readInput() (string, error) {
buffer := make([]byte, 100)
cnt, err := os.Stdin.Read(buffer)
if err != nil {
return "", err
}
if cnt == 1 && buffer[0] == 0x1b {
return "ESC", nil
} else if cnt >= 3 {
if buffer[0] == 0x1b && buffer[1] == '[' {
switch buffer[2] {
case 'A':
return "UP", nil
case 'B':
return "DOWN", nil
case 'C':
return "RIGHT", nil
case 'D':
return "LEFT", nil
}
}
}
return "", nil
}
func makeMove(oldRow, oldCol int, dir string) (newRow, newCol int) {
newRow, newCol = oldRow, oldCol
switch dir {
case "UP":
newRow = newRow - 1
if newRow < 0 {
newRow = len(maze) - 1
}
case "DOWN":
newRow = newRow + 1
if newRow == len(maze) {
newRow = 0
}
case "RIGHT":
newCol = newCol + 1
if newCol == len(maze[0]) {
newCol = 0
}
case "LEFT":
newCol = newCol - 1
if newCol < 0 {
newCol = len(maze[0]) - 1
}
}
if maze[newRow][newCol] == '#' {
newRow = oldRow
newCol = oldCol
}
return
}
func movePlayer(dir string) {
player.row, player.col = makeMove(player.row, player.col, dir)
switch maze[player.row][player.col] {
case '.':
numDots--
score++
// Remove dot from the maze
maze[player.row] = maze[player.row][0:player.col] + " " + maze[player.row][player.col+1:]
}
}
func drawDirection() string {
dir := rand.Intn(4)
move := map[int]string{
0: "UP",
1: "DOWN",
2: "RIGHT",
3: "LEFT",
}
return move[dir]
}
func moveGhosts() {
for _, g := range ghosts {
dir := drawDirection()
g.row, g.col = makeMove(g.row, g.col, dir)
}
}
func initialise() {
cbTerm := exec.Command("stty", "cbreak", "-echo")
cbTerm.Stdin = os.Stdin
err := cbTerm.Run()
if err != nil {
log.Fatalln("unable to activate cbreak mode:", err)
}
}
func cleanup() {
cookedTerm := exec.Command("stty", "-cbreak", "echo")
cookedTerm.Stdin = os.Stdin
err := cookedTerm.Run()
if err != nil {
log.Fatalln("unable to activate cooked mode:", err)
}
}
func main() {
// initialise game
initialise()
defer cleanup()
// load resources
err := loadMaze("maze01.txt")
if err != nil {
log.Println("failed to load maze:", err)
return
}
// game loop
for {
// update screen
printScreen()
// process input
input, err := readInput()
if err != nil {
log.Println("error reading input:", err)
break
}
// process movement
movePlayer(input)
moveGhosts()
// process collisions
for _, g := range ghosts {
if player == *g {
lives--
}
}
// check game over
if input == "ESC" || numDots == 0 || lives <= 0 {
break
}
// repeat
}
}