-
Notifications
You must be signed in to change notification settings - Fork 4
/
pachinko.go
188 lines (172 loc) · 3.68 KB
/
pachinko.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
package main
import (
"flag"
"fmt"
"image"
"math/rand"
"os"
"time"
"unicode/utf8"
"github.com/chzchzchz/sitbot/ascii"
)
func putCenterString(a *ascii.ASCII, s string) {
a.PutString(s, a.Columns()/2-len(s)/2, 0)
}
var pegWideFace = []rune{'^', '▔', '▀'}
var ballFace = []rune{'.', ':', '·'}
var edge = [][]rune{{'◢', '◣'},
{'▀', '▀'},
{'▔', '▔'},
{'▟', '▙'},
{'▞', '▚'},
{'▝', '▘'},
{'▕', '▌'},
{'▕', '▎'},
}
func randRune(s []rune) rune {
return s[rand.Intn(len(s))]
}
func placePegs(a *ascii.ASCII, y int) map[int]struct{} {
w := a.Columns()
pegs, pm, pegline := make([]int, rand.Intn(w)), make(map[int]struct{}), make([]bool, w)
for i := range pegs {
pegs[i] = rand.Intn(w)
for j := range pegs[:i] {
if pegs[i] == pegs[j] {
pegs[i] = rand.Intn(w)
}
}
pm[pegs[i]] = struct{}{}
pegline[pegs[i]] = true
}
if pegline[0] {
a.PutString(string(randRune(pegWideFace)), 0, y)
}
if pegline[len(pegline)-1] {
a.PutString(string(randRune(pegWideFace)), len(pegline)-1, y)
}
for i := 1; i < len(pegline)-1; i++ {
if !pegline[i] {
continue
}
if pegline[i-1] == pegline[i+1] {
a.PutString(string(randRune(pegWideFace)), i, y)
} else {
ee, s := edge[rand.Intn(len(edge))], ""
if !pegline[i-1] {
s = string(ee[0])
} else {
s = string(ee[1])
}
a.PutString(s, i, y)
}
}
return pm
}
func main() {
xPegs := flag.String("x", "", "use only x's for pegs")
flag.Parse()
if *xPegs != "" {
r, _ := utf8.DecodeRuneInString(*xPegs)
pegWideFace, edge = []rune{r}, [][]rune{{r, r}}
}
rand.Seed(time.Now().UnixNano())
w := 5 * 11
balls := make([]int, w/5)
a, _ := ascii.NewASCII("")
for i := 0; i < len(balls); i++ {
balls[i] = 2 + i*(w/len(balls))
a.PutString("(o)", balls[i]-1, 0)
a.PutString(":", balls[i], 1)
}
a.PutString(" ", w-1, 0)
h, y := w/2, 2
for y < h {
pm := placePegs(a, y)
collides, rounds := true, 0
for collides && rounds < 1000 {
rounds++
collides = false
for bi, b := range balls {
// Collision?
if _, ok := pm[b]; ok {
newv := b
if rand.Intn(2) == 0 {
newv += 1
} else {
newv -= 1
}
if newv < 0 {
newv = 0
} else if newv >= w {
newv = w - 1
}
bad := false
for _, bb := range balls {
if bad = (bb == newv); bad {
break
}
}
if !bad {
balls[bi] = newv
}
collides = true
}
}
}
for _, b := range balls {
a.PutString(":", b, y)
a.PutString(string(randRune(ballFace)), b, y+1)
}
y += 2
}
points := 0
for i := 0; i < len(balls); i++ {
a.PutString(string(randRune(ballFace)), balls[i], y)
}
cups := make([]int, len(balls))
for i := 0; i < len(balls); i++ {
off := 2 + i*(w/len(balls))
for _, b := range balls {
if b >= off-1 && b <= off+1 {
cups[i]++
points++
}
}
}
for i := 0; i < len(balls); i++ {
off := 2 + i*(w/len(balls))
switch cups[i] {
case 1:
a.PutString(`\./`, off-1, y)
case 2:
a.PutString(`\:/`, off-1, y)
case 3:
a.PutString(`\∵/`, off-1, y)
default:
a.PutString(`\_/`, off-1, y)
}
}
hdr, _ := ascii.NewASCII("")
hdr.PutString(" ", w, 0)
putCenterString(hdr, "✿ WELCOME TO PACHINKO2 ✿")
os.Stdout.Write(hdr.Bytes())
fmt.Println()
aa := ascii.RoundBox(image.Pt(a.Columns()+2, a.Rows()+2))
aa.Paste(a, image.Pt(1, 1))
os.Stdout.Write(aa.Bytes())
fmt.Println()
footer, _ := ascii.NewASCII("")
footer.PutString(" ", w, 0)
fs := ""
if points >= 2 {
fs = fmt.Sprintf("YOU SCORED: %d POINTS!!!", points)
} else if points == 1 {
fs = "NICE! ONE POINT!!!"
} else {
fs = fmt.Sprintf("YOU LOSE")
}
putCenterString(footer, fs)
os.Stdout.Write(footer.Bytes())
fmt.Println()
}