-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
164 lines (132 loc) · 2.79 KB
/
utils.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 (
"bytes"
"fmt"
"os"
"strconv"
"time"
"github.com/danicat/simpleansi"
)
func getLivesAsEmoji() string {
buf := bytes.Buffer{}
for i := maze.Player.Lives; i > 0; i-- {
buf.WriteString(cfg.Player + " ")
}
return buf.String()
}
func isGameOver() (string, bool) {
if maze.Player.Lives <= 0 {
moveCursor(maze.Player.Row, maze.Player.Col)
fmt.Print(cfg.Death)
return "Game Over", true
} else if maze.NumDots == 0 {
return "Congratulations! You win!", true
}
return "", false
}
func makeMove(oldRow, oldCol int, dir string) (newRow, newCol int) {
newRow, newCol = oldRow, oldCol
switch dir {
case "UP":
newRow--
if newRow < 0 {
newRow = len(maze.Layout) - 1
}
case "DOWN":
newRow++
if newRow == len(maze.Layout)-1 {
newRow = 0
}
case "RIGHT":
newCol++
if newCol == len(maze.Layout[0]) {
newCol = 0
}
case "LEFT":
newCol--
if newCol < 0 {
newCol = len(maze.Layout[0]) - 1
}
}
if maze.Layout[newRow][newCol] == '#' {
newRow = oldRow
newCol = oldCol
}
return newRow, newCol
}
func moveCursor(row, col int) {
if cfg.UseEmoji {
simpleansi.MoveCursor(row, col*2)
} else {
simpleansi.MoveCursor(row, col)
}
}
func printScreen() {
simpleansi.ClearScreen()
for _, line := range maze.Layout {
for _, char := range line {
switch char {
case '#':
fmt.Print(simpleansi.WithBlueBackground(cfg.Wall))
case '.':
fmt.Printf(cfg.Dot)
case 'X':
fmt.Printf(cfg.Pill)
default:
fmt.Printf(cfg.Space)
}
}
fmt.Println()
}
moveCursor(maze.Player.Row, maze.Player.Col)
fmt.Printf(cfg.Player)
for _, ghost := range maze.Ghosts {
moveCursor(ghost.Row, ghost.Col)
maze.GhostStatusMx.RLock()
if ghost.IsThreat {
fmt.Print(cfg.Ghost)
} else {
fmt.Print(cfg.GhostBlue)
}
maze.GhostStatusMx.RUnlock()
}
updatePlayerMessage("")
}
func readInput() (string, error) {
buffer := make([]byte, 100)
count, err := os.Stdin.Read(buffer)
if err != nil {
return "", err
}
if buffer[0] == 0x1b {
if count == 1 {
return "ESC", nil
} else if count >= 3 && 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 updatePlayerMessage(message string) {
livesRemaining := strconv.Itoa(maze.Player.Lives)
if cfg.UseEmoji {
livesRemaining = getLivesAsEmoji()
}
var pillTimeRemaining string
if pillTime := time.Until(maze.PillTimerEnd); pillTime > 0 {
pillTimeRemaining = fmt.Sprintf("\nPill Time Remaining: %v", pillTime.Round(time.Second))
}
moveCursor(len(maze.Layout)+1, 0)
fmt.Println("Score:", maze.Player.Score, "\nLives:", livesRemaining, pillTimeRemaining)
moveCursor(len(maze.Layout)+4, 0)
fmt.Println(message)
}