#include "fbgfx.bi"
'~ #include "fbcon.bi"
const SUCCESS = 1
const NEUTRAL = 2
const FAILED = 3
dim shared as integer avishai_happy = 0
dim shared as integer avishai_sad = 0
dim shared as boolean over = TRUE
sub show_results(result as integer)
select case result
case SUCCESS
print "Avishai is happy."
avishai_happy += 1
case NEUTRAL
print "Avishai is apathetic."
case FAILED
print "Avishai cries."
avishai_sad += 1
end select
end sub
sub game_turn()
dim as integer choice
cls
print "It's a new day in 1988."
print "Options:"
print "1. Feed Avishai"
print "2. Give him medications"
print "3. Take him to the public garden"
print "4. Play music for him"
print "5. Put Avishai to bed"
print "6. Exit the game"
input choice
cls
select case choice
case 1
show_results(int(rnd * 3) + 1)
case 2
show_results(int(rnd * 3) + 1)
case 3
show_results(int(rnd * 3) + 1)
case 4
show_results(int(rnd * 3) + 1)
case 5
show_results(int(rnd * 3) + 1)
case 6
print "Exiting the game."
sleep
over = FALSE
case else
print "Invalid choice. Try again."
end select
sleep
end sub
sub main()
'~ screenres 80, 25, 32
randomize timer
while over
game_turn()
wend
locate 20, 1
print "Avishai was happy "& str(avishai_happy) & " times."
print "Avishai was sad "& str(avishai_sad) & " times."
sleep
end sub
main()
`SLEEP` is a problem here as it waits for a key press but doesn't clear the keyboard buffer, so whatever you press will show up at the `INPUT` later. FreeBASIC has `GetKey` for waiting and removing the key from the buffer.
`over` has the exact opposite meaning as one would expect here. It would also be cleaner not to set a global flag but to return the status from `game_turn`.
The five `CASE 1`, `CASE 2`, ..., `CASE 5` with always the same code can be merged into one `CASE 1 TO 5`. The other `SELECT CASE` construct and the happy and sad variables can be replaced by arrays with the reaction text and an array to count how often which result was randomly chosen.
Then the main loop is simple enough to get rid of the subroutines:
enum TResult
SUCCESS, NEUTRAL, FAILED, MAX = FAILED
end enum
dim reactions(TResult.MAX) as string = { "is happy", "is apathetic", "cries" }
dim stats(TResult.MAX) as integer, choice as integer, result as TResult
Randomize
do
Cls
print "It's a new day in 1988."
print "Options:"
print "1. Feed Avishai"
print "2. Give him medications"
print "3. Take him to the public garden"
print "4. Play music for him"
print "5. Put Avishai to bed"
print "6. Exit the game"
input choice
Cls
select case choice
case 1 to 5
result = Cast(TResult, Int(Rnd * (TResult.MAX + 1)))
print "Avishai "; reactions(result); "."
stats(result) += 1
case 6
print "Exiting the game."
GetKey
exit do
case else
print "Invalid choice. Try again."
end select
GetKey
loop
locate 20, 1
print "Avishai was happy"; stats(SUCCESS); " times."
print "Avishai was sad"; stats(FAILED); " times."
GetKey