-
Notifications
You must be signed in to change notification settings - Fork 1
/
whackboard.go
151 lines (131 loc) Β· 2.84 KB
/
whackboard.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
package main
import (
"math/rand"
"strconv"
"strings"
)
const (
Tree rune = 'π³'
TreeHot rune = 'π΄'
TreeCold rune = 'π²'
Fire rune = 'π₯'
Ice rune = 'π§'
Whack rune = 'π―'
Water rune = 'π§'
width int = 15
height int = 15
)
type Board struct {
board [][]rune
whackX int
whackY int
}
func NewBoard() *Board {
board := make([][]rune, height)
for i := range board {
board[i] = make([]rune, width)
for j := range board[i] {
r := rand.Float64()
if r < 0.02 {
board[i][j] = Fire
} else if r < 0.04 {
board[i][j] = Ice
} else {
board[i][j] = Tree
}
}
}
b := &Board{
board: board,
}
b.Generate()
return b
}
func (b *Board) RenderBoard(t string, fireScore, iceScore int, comment string) string {
s := ""
border := "β"
scoreStr := "π₯ " + strconv.Itoa(fireScore) + " π§ " + strconv.Itoa(iceScore)
l := len([]rune(scoreStr))
s += t + strings.Repeat(border, width-l/2-l%2-2) + scoreStr + strings.Repeat(border, width-l/2-2) + t + "\n"
for _, row := range b.board {
s += "β" + string(row) + "β\n"
}
l = len([]rune(comment))
s += t + strings.Repeat(border, width-l/2-l%2-1) + comment + strings.Repeat(border, width-l/2-1) + t + "\n"
return s
}
func (b *Board) Generate() {
b.whackX = rand.Intn(width)
b.whackY = rand.Intn(height)
if b.board[b.whackY][b.whackX] == Tree || b.board[b.whackY][b.whackX] == TreeHot || b.board[b.whackY][b.whackX] == TreeCold {
b.board[b.whackY][b.whackX] = Whack
return
}
b.Generate()
}
func (b *Board) Click(x, y int, team bool) string {
if x >= width || y >= height || x < 0 || y < 0 {
return "Out of bounds!"
}
comment := ""
if b.board[y][x] == Whack {
if team {
b.board[y][x] = Fire
fireScore++
} else {
b.board[y][x] = Ice
iceScore++
}
comment = "Nice!"
b.Generate()
} else if b.board[y][x] == Fire {
if !team {
b.board[y][x] = Water
iceScore--
comment = "Ouch! You made water!"
}
} else if b.board[y][x] == Ice {
if team {
b.board[y][x] = Water
fireScore--
comment = "Ouch! You made water!"
}
} else if b.board[y][x] == Tree {
if team {
b.board[y][x] = TreeHot
comment = "Palm tree!"
} else {
b.board[y][x] = TreeCold
comment = "Pine tree!"
}
} else if b.board[y][x] == Water {
if team {
fireScore--
comment = "Ouch! Water puts out fire!"
} else {
iceScore--
comment = "Ouch! Water melts ice!"
}
}
out:
for r, row := range b.board { // detect game end
for c, cell := range row {
if cell == TreeHot || cell == TreeCold || cell == Tree {
break out
}
if r == len(b.board)-1 && c == len(row)-1 {
s := ""
if fireScore > iceScore {
s += "π₯ wins!"
} else if iceScore > fireScore {
s += "π§ wins!"
} else {
s += "It's a tie!"
}
gameDoneMsg = s
return ""
}
}
}
return comment
}