-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
312 lines (258 loc) · 8.21 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"fmt"
"math/rand"
"encoding/csv"
"strconv"
"os"
"time"
)
const NO_GUESS int = -1
type Assets struct {
correctGuessOutStr string
gameWonOutStr string
mainSectionOutStr string
zeroAttempsOutStr string
}
// Struct defining the levels of the
// game as a doubly-linked list.
type GameLevel struct {
id int
nAttempts int
minRange int
maxRange int
numberToGuess int
next *GameLevel
prev *GameLevel
}
type Player struct {
attempts int
lastGuess int
}
type GameState struct {
nLevels int
level *GameLevel
player Player
wasWon bool
assets Assets
}
func clear() {
fmt.Print("\033[H\033[2J")
}
func enterpoint() bool {
var input string
fmt.Scanln(&input)
if input == "q" {
return false
}
return true
}
func check(err error, expect string) {
if err != nil {
fmt.Println(expect)
panic(err)
}
}
func main() {
rand.Seed(time.Now().UnixNano())
guessingGame := GameState{}
guessingGame.init()
for {
guessingGame.run()
if guessingGame.wasWon {
// If the player wins show this output
output := guessingGame.assets.gameWonOutStr
fmt.Print(output)
break
} else if guessingGame.player.attempts == 0 {
// If the player doesn't have more attempts
// show the output bellow
output := guessingGame.assets.zeroAttempsOutStr
num := guessingGame.level.numberToGuess
fmt.Printf(output, num)
if enterpoint() {
// If the player clicks 'enter' or another keyword than 'q'
// continue the game execution and reboot the game.
guessingGame.reboot()
} else {
// If the player choses to quit break the execution here
break
}
} else {
// If this section is reach means that the player opted to
// to quit the game instead of play the next level
break
}
}
}
func (asset *Assets) load() {
var dat []byte
var err error
dat, err = os.ReadFile("./assets/correct_guess.txt")
check(err, "Asset file 'correct_guess.txt' not found!")
asset.correctGuessOutStr = string(dat)
dat, err = os.ReadFile("./assets/no_more_attempts.txt")
check(err, "Asset file 'no_more_attemps.txt' not found!")
asset.zeroAttempsOutStr = string(dat)
dat, err = os.ReadFile("./assets/main_section.txt")
check(err, "Asset file 'main_section.txt' not found!")
asset.mainSectionOutStr = string(dat)
dat, err = os.ReadFile("./assets/game_won.txt")
check(err, "Asset file 'game_won.txt' not found!")
asset.gameWonOutStr = string(dat)
}
func (level *GameLevel) changeGuessingNumber() {
max, min := level.maxRange - 1, level.minRange + 1
level.numberToGuess = rand.Intn(max) + min
}
func (player *Player) consumeAttempt() bool {
if player.attempts > 0 {
player.attempts -= 1
return true
}
return false
}
func (game *GameState) init() {
game.player = Player{}
game.assets = Assets{}
defer game.assets.load()
defer game.initPlayer()
file, err := os.Open("./assets/levels.csv")
check(err, "Asset file 'levels.csv' not found!")
csvReader := csv.NewReader(file)
data, err := csvReader.ReadAll()
check(err, "Could not read the levels csv file!")
file.Close()
// Reads the info of the levels of the game
// from the file and insert new levels.
for i, level := range data {
if i == 0 { continue } // Ignore the name of the columns
min, err := strconv.Atoi(level[0])
check(err, "Error converting value to integer!")
max, err := strconv.Atoi(level[1])
check(err, "Error converting value to integer!")
attempts, err := strconv.Atoi(level[2])
check(err, "Error converting value to integer!")
game.addLevel(min, max, attempts)
}
}
func (game *GameState) initPlayer() {
game.player.attempts = game.level.nAttempts
game.player.lastGuess = NO_GUESS
}
func (game *GameState) reboot() {
var level *GameLevel
// Traversing the list from the next to the previous
// node to reinitialize the number to be guessed.
for level = game.level ; level.prev != nil ; level = level.prev {
level.changeGuessingNumber()
}
// Referencing the current level of the game to the first level
// and changing the number to predicted in this level.
game.level = level
game.level.changeGuessingNumber()
game.initPlayer()
}
func (game *GameState) run() {
clear()
// Executes while the player has attempts left
for game.player.consumeAttempt() {
output := game.assets.mainSectionOutStr
level := game.level.id
min := game.level.minRange
max := game.level.maxRange
attempts := game.player.attempts + 1
nLevels := game.nLevels
hint := game.giveHint()
fmt.Printf(output, level, nLevels, min, max, attempts, hint)
fmt.Scan(&game.player.lastGuess)
clear()
// Comparing the guess, if they match
// the player will transit to the next level
// else it will use another attempt
if game.checkGuess() {
// If there are any level to transit continue the game
// to the next level, else it means that the player had
// transited to all levels which also means that the
// player won the game.
if game.transitLevel() {
output := game.assets.correctGuessOutStr
prevLevel := game.level.prev.id
level := game.level.id
fmt.Printf(output, prevLevel, level)
if !enterpoint() {
// If the player chose to quit,
// break here.
break
} else {
// Else clear the screen and
// continues to the next level
clear()
}
} else {
// If this section is reach,
// it means that there are no more levels,
// that is, the game was won.
game.wasWon = true
break
}
}
}
}
func (game *GameState) transitLevel() bool {
// Changes the current level to the next level
// in the list of levels.
if game.level != nil && game.level.next != nil {
game.level = game.level.next
game.initPlayer()
return true
}
return false
}
func (game *GameState) addLevel(minRange, maxRange, attempts int) {
defer func () {
// Increases the number of level in the state
// at the end of the scope of this function.
game.nLevels += 1
}()
if game.level == nil {
game.level = &GameLevel{
id: 1,
nAttempts: attempts,
minRange: minRange,
maxRange: maxRange,
prev: nil,
next: nil,
}
game.level.changeGuessingNumber()
} else {
var level *GameLevel
for level = game.level ; level.next != nil ; level = level.next {}
level.next = &GameLevel{
id: level.id + 1,
nAttempts: attempts,
minRange: minRange,
maxRange: maxRange,
prev: level,
next: nil,
}
level.next.changeGuessingNumber()
}
}
func (game GameState) giveHint() string {
// analyses the last guess of the player and returns a hint,
// informing if the number to be guessed it lower or greater
// than the one the player gave.
if lastGuess := game.player.lastGuess; lastGuess == NO_GUESS {
return "no hints yet"
} else if game.level.numberToGuess > lastGuess {
return fmt.Sprintf("greater than %d", lastGuess)
} else {
return fmt.Sprintf("lower than %d", lastGuess)
}
}
func (game GameState) checkGuess() bool {
// compares the last guess of the player to the
// number to be guessed.
return game.level.numberToGuess == game.player.lastGuess
}