-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
254 lines (209 loc) · 5.04 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"slices"
"strconv"
"strings"
"time"
)
type Factory struct {
resources []int // R(I)
resourceCost []int // C(I)
finishedProducts []int // F(I)
productValue []int // P(I)
cash int // CH
manufacturingCost int // M
month int // T
}
var rng *rand.Rand
func rnd(min, max int) int {
return rng.Intn(max-min) + min
}
func input(prompt string, validator func(string) bool) string {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print(prompt)
text, _ := reader.ReadString('\n')
text = strings.Replace(text, "\n", "", -1)
if validator(text) {
return text
}
}
}
func selectInput(prompt string) int {
command := input(fmt.Sprintf("%s (Q to return) ? ", prompt), func(s string) bool {
return len(s) == 1 && slices.ContainsFunc(
[]rune{'1', '2', '3', 'Q'},
func(r rune) bool { return rune(strings.ToUpper(s)[0]) == r },
)
})
if rune(strings.ToUpper(command)[0]) == 'Q' {
return -1
}
i, _ := strconv.Atoi(command)
return i
}
func numericInput(prompt string) int {
num, _ := strconv.Atoi(input(prompt, func(s string) bool {
return len(s) > 0 && strings.IndexFunc(s, func(r rune) bool {
return r < '0' || r > '9'
}) == -1
}))
return num
}
func initFactory() *Factory {
f := Factory{
month: 1,
cash: 500,
manufacturingCost: 2,
resources: make([]int, 3),
finishedProducts: make([]int, 3),
resourceCost: make([]int, 3),
productValue: make([]int, 3),
}
for i := 0; i < 3; i++ {
f.resourceCost[i] = rnd(10, 20)
f.productValue[i] = rnd(50, 90)
}
return &f
}
// display: outputs current game state for the player
func (f *Factory) display() {
fmt.Println("Item: Materials: Product:")
for i := 0; i < 3; i++ {
fmt.Printf(
"%d%7d $%d%7d $%d\n",
i+1,
f.resources[i],
f.resourceCost[i],
f.finishedProducts[i],
f.productValue[i],
)
}
fmt.Printf("Month %d, you have $%d\n", f.month, f.cash)
fmt.Printf("Manufacturing costs are $%d/unit\n", f.manufacturingCost)
}
func (f *Factory) purchase() {
for {
id := selectInput("Which material to purchase")
if id < 0 {
return
}
amount := numericInput(fmt.Sprintf("That costs $%d/unit, you have $%d. How many to purchase? ", f.resourceCost[id-1], f.cash))
cost := amount * f.resourceCost[id-1]
if cost > f.cash {
fmt.Printf("Purchasing %d units would cost %d, you have insufficient funds!\n", amount, cost)
continue
}
f.cash -= cost
f.resources[id-1] += amount
}
}
func (f *Factory) manufacture() {
for {
id := selectInput("Which material to manufacture")
if id < 0 {
return
}
amount := numericInput(fmt.Sprintf("Manufacturing costs $%d/unit, you have $%d. How many to manufacture? ", f.manufacturingCost, f.cash))
cost := amount * f.manufacturingCost
if cost > f.cash {
fmt.Printf("Manufacturing %d units would cost %d, you have insufficient funds!\n", amount, cost)
continue
}
hasMaterials := true
for i := 0; i < 3; i++ {
if i == id-1 {
continue
}
if f.resources[i] < amount {
hasMaterials = false
break
}
}
if hasMaterials == false {
fmt.Println("You have insufficient materials to manufacture that much!")
continue
}
for i := 0; i < 3; i++ {
if i == id-1 {
continue
}
f.resources[i] -= amount
}
f.cash -= cost
f.finishedProducts[id-1] += amount
}
}
func (f *Factory) sell() {
for {
id := selectInput("Which product to sell")
if id < 0 {
return
}
amount := numericInput(fmt.Sprintf("You have %d units, of that product, they sell for $%d/unit. How many to sell? ", f.finishedProducts[id-1], f.productValue[id-1]))
if amount > f.finishedProducts[id-1] {
fmt.Println("You have insufficient products to sell that much!")
continue
}
f.cash += f.productValue[id-1] * amount
f.finishedProducts[id-1] -= amount
}
}
func (f *Factory) update() {
j := 0
for i := 0; i < 3; i++ {
for j < 10 || j > 20 {
j = f.resourceCost[i] + rnd(-2, 2)
}
f.resourceCost[i] = j
j = 0
for j < 50 || j > 90 {
j = f.productValue[i] + rnd(-5, 5)
}
f.productValue[i] = j
}
j = 0
for j < 1 || j > 9 {
j = f.manufacturingCost + rnd(-2, 2)
}
f.manufacturingCost = j
}
func (f *Factory) netWorth() int {
netWorth := f.cash
for i := 0; i < 3; i++ {
netWorth += f.productValue[i] * f.finishedProducts[i]
netWorth += f.resourceCost[i] * f.resources[i]
}
return netWorth
}
func main() {
source := rand.NewSource(time.Now().UnixNano())
rng = rand.New(source)
state := initFactory()
for state.month < 12 {
state.display()
command := input("Transaction (O,B,M,S) ? ", func(s string) bool {
return len(s) == 1 && slices.ContainsFunc(
[]rune{'O', 'B', 'M', 'S'},
func(r rune) bool { return rune(strings.ToUpper(s)[0]) == r },
)
})
switch rune(strings.ToUpper(command)[0]) {
case 'B':
state.purchase()
break
case 'M':
state.manufacture()
break
case 'S':
state.sell()
}
state.update()
state.month++
}
fmt.Printf("Your net worth is $%d", state.netWorth())
}