News:

Welcome to RetroCoders Community

Main Menu

Simple Guessing Game.

Started by electricwalrus, Sep 10, 2022, 08:31 AM

Previous topic - Next topic

electricwalrus

This is a very simple freebasic program. Guess my number between 1-100. You have 7 trys to guess the number. Simple enough?

Screen 17

Dim guess as Byte
Dim number as byte
Dim GuessCount as integer

Randomize Timer

cls
Print "Welcome to the Guessing Game!"
Print
Print "I am thinking of a number between 1 and 100."
number = int(rnd*100)+1
'Print number
Print "Can you guess my number?"

Do
    GuessCount = GuessCount + 1
    Print
    Input "Your guess: ", guess
    if guess = number then
        Print "Well done! You guessed my number."
        Sleep
        End
    end if
    If guess < number Then
        Print "Higher..."
    else
        Print "Lower..."
    End if
    Print "You have " + str(7 - GuessCount) + " guesses remaining..."
Loop until GuessCount = 7

Print "You have guessed 7 times... Not good enough. You have no more guesses."
Print "Game over."
Sleep
End


__blackjack__

There is a simple counting loop coded manually that could be a FOR loop.  The IIF function can be used for the lower/higher output.

Randomize Timer

Const MaxGuessCount = 7

Dim number As Const Byte = Int(Rnd * 100) + 1
Dim guess As Byte

Cls
Print "Welcome to the Guessing Game!"
Print
Print "I am thinking of a number between 1 and 100."
Print "Can you guess my number?"

For guessCount As Byte = MaxGuessCount To 1 Step -1
    Print
    Input "Your guess: ", guess
    If guess = number Then Print "Well done! You guessed my number.": End
    Print IIf(guess < number, "Higher...", "Lower...")
    Print "You have"; guessCount - 1; " guesses remaining..."
Next

Print "You have guessed"; MaxGuessCount; " times... Not good enough. You have no more guesses."
Print "Game over."
,,Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." —Brian W. Kernighan