How to resolve the algorithm Guess the number/With feedback step by step in the BASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Guess the number/With feedback step by step in the BASIC programming language
Table of Contents
Problem Statement
Write a game (computer program) that follows the following rules:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Guess the number/With feedback step by step in the BASIC programming language
Source code in the basic programming language
100 L% = 1
110 U% = 10
120 N% = RND(1) * (U% - L% + 1) + L%
130 PRINT "A NUMBER FROM " L%;
140 PRINT " TO " U%;
150 PRINT ", CALLED A TARGET, HAS BEEN RANDOMLY CHOSEN."
160 FOR Q = 0 TO 1 STEP 0
170 INPUT "GUESS THE TARGET NUMBER: "; G%
180 IF G% < L% OR G% > U% THEN PRINT "THE INPUT WAS INAPPROPRIATE."
190 IF G% > N% THEN PRINT "THE GUESS WAS HIGHER THAN THE TARGET."
200 IF G% < N% THEN PRINT "THE GUESS WAS LESS THAN THE TARGET."
210 Q = G% = N%
220 NEXT
230 PRINT "THE GUESS WAS EQUAL TO THE TARGET."
Min = 5: Max = 15
chosen = int(rand*(Max-Min+1)) + Min
print "Guess a whole number between "+Min+" and "+Max
do
input "Enter your number " ,guess
if guess < Min OR guess > Max then
print "That was an invalid number"
end
else
if guess < chosen then print "Sorry, your number was too low"
if guess > chosen then print "Sorry, your number was too high"
if guess = chosen then print "Well guessed!"
end if
until guess = chosen
100 cls
110 nmax = 20
120 chosen = int(rnd(1)*nmax)+1
130 print "Guess a whole number between 1 a ";nmax;chr$(10)
140 do
150 input "Enter your number: ",guess
160 if guess < n or guess > nmax then
170 print "That was an invalid number"
180 exit do
190 else
200 if guess < chosen then print "Sorry, your number was too low"
210 if guess > chosen then print "Sorry, your number was too high"
220 if guess = chosen then print "Well guessed!"
230 endif
240 loop until guess = chosen
250 end
Public Sub Main()
Randomize
Dim guess As Integer, max As Integer = 20
Dim n As Integer = Int(Rnd * max) + 1
Print "Guess which number I've chosen in the range 1 to "; max; Chr(10)
Do
Print " Your guess : "
Input guess
If guess > n And guess <= 20 Then
Print "Your guess is higher than the chosen number, try again"
Else If guess = n Then
Print "Correct, well guessed!"
Break
Else If guess < n And guess >= 1 Then
Print "Your guess is lower than the chosen number, try again"
Else
Print "Your guess is inappropriate, try again"
End If
Loop
End
100 CLS
110 RANDOMIZE 1
120 L = 20
130 X = INT(RND(1)*L)+1
140 PRINT "Guess a whole number between 1 a ";L;CHR$(10)
150 'do
160 INPUT "Enter your number: ", N
170 IF N < N OR N > L THEN PRINT "That was an invalid number" : GOTO 230
180 'else
190 IF N < X THEN PRINT "Sorry, your number was too low"
200 IF N > X THEN PRINT "Sorry, your number was too high"
210 IF N = X THEN PRINT "Well guessed!"
220 IF N <> X THEN GOTO 150
230 END
100 PROGRAM "Guess.bas"
110 RANDOMIZE
120 LET UP=10:LET LO=1 ! Limits
130 PRINT "I'm thinking of a number between";LO;"and";UP
140 LET COUNT=0:LET NR=RND(UP-LO+1)+LO
150 DO
160 LET COUNT=COUNT+1
170 INPUT PROMPT "Guess a number: ":GU
180 SELECT CASE GU
190 CASE IS>NR
200 PRINT "My number is lower that."
210 CASE IS<NR
220 PRINT "My number is higher that."
230 CASE ELSE
240 PRINT "Well guessed! Numner of tips:";COUNT
250 END SELECT
260 LOOP UNTIL NR=GU
100 RANDOMIZE
110 LET T = 0
120 LET N = 20
130 LET R = INT(RND*N)+1
140 PRINT "GUESS A WHOLE NUMBER BETWEEN 1 AND";N
150 LET T = T+1
160 PRINT "ENTER YOUR NUMBER ";
170 INPUT G
180 IF G <> R THEN 210
190 PRINT "YOU GUESSED IT IN";T;"TRIES."
200 GOTO 310
210 IF G > R THEN 240
220 IF G < 1 THEN 240
230 PRINT "SORRY, YOUR NUMBER WAS TOO LOW"
240 IF G < R THEN 270
250 IF G > 100 THEN 270
260 PRINT "SORRY, YOUR NUMBER WAS TOO HIGH"
270 IF G >= 1 THEN 300
280 IF G <= 100 THEN 300
290 PRINT "THAT WAS AN INVALID NUMBER"
300 IF G <> R THEN 140
310 END
100 CLS
120 L = 20
130 X = INT(RND(1)*L)+1
140 PRINT "Guess a whole number between 1 a ";L;CHR$(10)
150 'do
160 INPUT "Enter your number: "; N
170 IF N < N OR N > L THEN PRINT "That was an invalid number" : GOTO 230
180 'else
190 IF N < X THEN PRINT "Sorry, your number was too low"
200 IF N > X THEN PRINT "Sorry, your number was too high"
210 IF N = X THEN PRINT "Well guessed!"
220 IF N <> X THEN GOTO 150
230 END
DIM secretNumber AS _BYTE ' the secret number
DIM guess AS _BYTE ' the player's guess
RANDOMIZE TIMER ' set a seed based on system time
secretNumber%% = INT(RND * 10) + 1 ' a random value between 1 and 10
PRINT "The computer has chosen a secret number between 1 and 10."
DO
PRINT "What is your guess";
INPUT guess%%
SELECT CASE guess%%
CASE IS < 1, IS > 10
PRINT "Please enter a number between 1 and 10!"
_CONTINUE
CASE IS < secretNumber%%
PRINT "Your guess is LOWER than the target."
CASE IS > secretNumber%%
PRINT "Your guess is HIGHER than the target."
END SELECT
LOOP UNTIL guess%% = secretNumber%%
PRINT "You won!"; secretNumber%%; "was the secret number!"
CLS
RANDOMIZE TIMER ' set a seed based on system time
nmax = 20
chosen = INT(RND * nmax) + 1
PRINT "Guess a whole number between 1 a"; nmax; CHR$(10)
DO
INPUT "Enter your number"; guess
IF guess < n OR guess > nmax THEN
PRINT "That was an invalid number"
EXIT DO
ELSE
IF guess < chosen THEN PRINT "Sorry, your number was too low"
IF guess > chosen THEN PRINT "Sorry, your number was too high"
IF guess = chosen THEN PRINT "Well guessed!"
END IF
LOOP UNTIL guess = chosen
END
nmin = 5
nmax = 15
chosen = int( rnd( 1) * (nmax-nmin+1)) +nmin
print "Guess a whole number between "; nmin; " and "; nmax
[loop]
input "Enter your number "; guess
if guess < nmin or guess > nmax then
print "That was an invalid number"
end
else
if guess < chosen then print "Sorry, your number was too low"
if guess > chosen then print "Sorry, your number was too high"
if guess = chosen then print "Well guessed!": end
end if
if guess <> chosen then [loop]
RANDOMIZE
LET nmax = 20
LET chosen = int(rnd*nmax)+1
PRINT "Guess a whole number between 1 a"; nmax; chr$(10)
DO
INPUT prompt "Enter your number ": guess
IF guess < n or guess > nmax then
PRINT "That was an invalid number"
EXIT DO
ELSE
IF guess < chosen then PRINT "Sorry, your number was too low"
IF guess > chosen then PRINT "Sorry, your number was too high"
IF guess = chosen then PRINT "Well guessed!"
END IF
LOOP until guess = chosen
END
nmin = 5
nmax = 15
chosen = int(ran(nmax-nmin+1)) + nmin
print "Guess a whole number between ", nmin, " and ", nmax
repeat
input "Enter your number " guess
if guess < nmin or guess > nmax then
print "That was an invalid number"
end
else
if guess < chosen print "Sorry, your number was too low"
if guess > chosen print "Sorry, your number was too high"
if guess = chosen print "Well guessed!"
fi
until guess = chosen
You may also check:How to resolve the algorithm Hash from two arrays step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the Zsh programming language
You may also check:How to resolve the algorithm Even or odd step by step in the Symsyn programming language
You may also check:How to resolve the algorithm FASTA format step by step in the Lua programming language
You may also check:How to resolve the algorithm Almost prime step by step in the ARM Assembly programming language