-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
203 lines (183 loc) · 3.58 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
package main
import (
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"os/signal"
"runtime"
"time"
)
const (
WIDTH = 32
HEIGHT = 24
// UDLR
RIGHT = 1
LEFT = 2
DOWN = 4
UP = 8
)
var (
player Player
mpp [WIDTH][HEIGHT]bool
)
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
player = Player{
Position: Position{
X: 1,
Y: 1,
},
}
initMap()
go keyPress()
go movePlayer()
go render()
<-c
fmt.Println("Done!")
}
type Position struct {
X int
Y int
}
type Player struct {
Position
}
var clear = map[string]string{
"linux": "clear",
"darwin": "clear",
"windows": "cls",
}
var controller = make(chan Position, 1)
func clearScreen() {
value, ok := clear[runtime.GOOS]
if ok {
cmd := exec.Command(value)
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
log.Fatalln(fmt.Errorf("error on run clear: %w", err))
}
} else {
log.Fatalln("your platform is unsupported! I can't clear terminal screen :(")
}
}
func keyPress() {
// disable input buffering
err := exec.Command("stty", "-f", "/dev/tty", "cbreak", "min", "1").Run()
if err != nil {
log.Fatalln(fmt.Errorf("error on run disable input buffering: %w", err))
}
// do not display entered characters on the screen
err = exec.Command("stty", "-f", "/dev/tty", "-echo").Run()
if err != nil {
log.Fatalln(fmt.Errorf("error on run command: %w", err))
}
// restore the echoing state when exiting
defer func() {
err := exec.Command("stty", "-f", "/dev/tty", "echo").Run()
if err != nil {
log.Fatalln(fmt.Errorf("error on run reset: %w", err))
}
}()
var b = make([]byte, 1)
for {
_, err = os.Stdin.Read(b)
if err != nil {
log.Fatalln(fmt.Errorf("error on read standard input: %w", err))
}
switch string(b) {
case "a", "A":
controller <- Position{X: -1, Y: 0}
case "s", "S":
controller <- Position{X: 0, Y: 1}
case "w", "W":
controller <- Position{X: 0, Y: -1}
case "d", "D":
controller <- Position{X: 1, Y: 0}
}
}
}
func movePlayer() {
for {
k := <-controller
if !mpp[player.X+k.X][player.Y+k.Y] {
player.X += k.X
player.Y += k.Y
}
}
}
func render() {
for {
clearScreen()
for y := 0; y < HEIGHT; y++ {
for x := 0; x < WIDTH; x++ {
if mpp[x][y] {
val := 0
if x > 0 && mpp[x-1][y] {
val += LEFT
}
if x < WIDTH-1 && mpp[x+1][y] {
val += RIGHT
}
if y > 0 && mpp[x][y-1] {
val += UP
}
if y < HEIGHT-1 && mpp[x][y+1] {
val += DOWN
}
switch val {
case UP, DOWN, UP + DOWN:
fmt.Print("║")
case LEFT, RIGHT, LEFT + RIGHT:
fmt.Print("═")
case RIGHT + DOWN:
fmt.Print("╔")
case RIGHT + UP:
fmt.Print("╚")
case LEFT + DOWN:
fmt.Print("╗")
case LEFT + UP:
fmt.Print("╝")
case UP + DOWN + RIGHT:
fmt.Print("╠")
case UP + DOWN + LEFT:
fmt.Print("╣")
case UP + LEFT + RIGHT:
fmt.Print("╩")
case DOWN + LEFT + RIGHT:
fmt.Print("╦")
case UP + DOWN + LEFT + RIGHT:
fmt.Print("╬")
default:
fmt.Print("═")
}
} else if player.X == x && player.Y == y {
fmt.Print("☺")
} else {
fmt.Print(string(32))
}
}
fmt.Print("\n")
}
time.Sleep(time.Millisecond * 100)
}
}
func initMap() {
rand.Seed(int64(time.Now().Nanosecond()))
for y := HEIGHT - 1; y >= 0; y-- {
for x := WIDTH - 1; x >= 0; x-- {
if y == 0 || y == HEIGHT-1 || x == 0 || x == WIDTH-1 {
mpp[x][y] = true
} else {
mpp[x][y] = rand.Int()%2 == 0
if !mpp[x][y] {
player.X = x
player.Y = y
}
}
}
}
}