-
Notifications
You must be signed in to change notification settings - Fork 41
/
game.go
183 lines (151 loc) · 2.86 KB
/
game.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
package main
import (
"fmt"
"golang.org/x/exp/slices"
)
func main() {
var i1 Item
fmt.Println(i1)
fmt.Printf("i1: %#v\n", i1)
i2 := Item{1, 2}
fmt.Printf("i2: %#v\n", i2)
i3 := Item{
Y: 10,
// X: 20,
}
fmt.Printf("i3: %#v\n", i3)
fmt.Println(NewItem(10, 20))
fmt.Println(NewItem(10, -20))
i3.Move(100, 200)
fmt.Printf("i3 (move): %#v\n", i3)
p1 := Player{
Name: "Parzival",
Item: Item{500, 300},
}
fmt.Printf("p1: %#v\n", p1)
fmt.Printf("p1.X: %#v\n", p1.X)
fmt.Printf("p1.Item.X: %#v\n", p1.Item.X)
p1.Move(400, 600)
fmt.Printf("p1 (move): %#v\n", p1)
ms := []mover{
&i1,
&p1,
&i2,
}
moveAll(ms, 0, 0)
for _, m := range ms {
fmt.Println(m)
}
k := Jade
fmt.Println("k:", k)
fmt.Println("key:", Key(17))
// time.Time import json.Marshaler interface
// json.NewEncoder(os.Stdout).Encode(time.Now())
p1.FoundKey(Jade)
fmt.Println(p1.Keys)
p1.FoundKey(Jade)
fmt.Println(p1.Keys)
}
// Implement fmt.Stringer interface
func (k Key) String() string {
switch k {
case Jade:
return "jade"
case Copper:
return "copper"
case Crystal:
return "crystal"
}
return fmt.Sprintf("<Key %d>", k)
}
/* Exercise
- Add a "Keys" field to Player which is a slice of Key
- Add a "FoundKey(k Key) error" method to player which will add k to Key if it's not there
- Err if k is not one of the known keys
*/
// Go's version of "enum"
const (
Jade Key = iota + 1
Copper
Crystal
invalidKey // internal (not exported)
)
type Key byte
/* go >= 1.18
func NewNumber[T int | float64](kind string) T {
if kind == "int" {
return 0
}
return 0.0
}
*/
// Rule of thumb: Accept interfaces, return types
func moveAll(ms []mover, x, y int) {
for _, m := range ms {
m.Move(x, y)
}
}
type mover interface {
Move(x, y int)
// Move(int, int)
}
func (p *Player) FoundKey(k Key) error {
if k < Jade || k >= invalidKey {
return fmt.Errorf("invalid key: %#v", k)
}
// if !containsKey(p.Keys, k) {
if !slices.Contains(p.Keys, k) {
p.Keys = append(p.Keys, k)
}
return nil
}
func containsKey(keys []Key, k Key) bool {
for _, k2 := range keys {
if k2 == k {
return true
}
}
return false
}
type Player struct {
Name string
// X int
Item // Embed Item
// T
Keys []Key
}
/*
type T struct {
X int
}
*/
// i is called "the receiver"
// if you want to mutate, use pointer receiver
func (i *Item) Move(x, y int) {
i.X = x
i.Y = y
}
// func NewItem(x, y int) Item {
// func NewItem(x, y int) *Item {
// func NewItem(x, y int) (Item, error) {
func NewItem(x, y int) (*Item, error) {
if x < 0 || x > maxX || y < 0 || y > maxY {
return nil, fmt.Errorf("%d/%d out of bounds %d/%d", x, y, maxX, maxY)
}
i := Item{
X: x,
Y: y,
}
// The Go compiler does "escape analysis" and will allocation i on the heap
return &i, nil
}
// zero vs missing value
const (
maxX = 1000
maxY = 600
)
// Item is an item in the game
type Item struct {
X int
Y int
}