Qbasicnews.com

QBasic => QB Discussion & Programming Help => Topic started by: tannervp on December 01, 2005, 09:26:54 PM



Title: Random number guessing game.....
Post by: tannervp on December 01, 2005, 09:26:54 PM
I am trying to make a guessing game that starts with a random number between 1 and 20 that gives the player 5 guesses, and after each guess tells them whether they are too high or too low.


I want HELP, not the code

~~ :rotfl:  :bounce: Charlie :bounce:  :rotfl: ~~


Title: Random number guessing game.....
Post by: tannervp on December 01, 2005, 09:35:54 PM
please?


Title: Random number guessing game.....
Post by: Kevin_theprogrammer on December 01, 2005, 09:53:29 PM
This belongs in the Newb forum...


Title: Random number guessing game.....
Post by: WaffleMan on December 01, 2005, 10:01:51 PM
Simple: First, randomly generate a number, then offer the user a guess. Take the user's input and compare it with the generated number: if the user's number is higher than the generated number, print "Too high!" If the user's number is lower, then print "Too low!"

Also, have a tries counter that adds 1 to itself every time the user misses the guess. If the user fails on all 5 tries, then print "Game over!"

But instead of that, have a generic tries counter, so you can let the user know how well he did. Example: "Congradulations! You got the number in *num* tries!"


Title: Random number guessing game.....
Post by: tannervp on December 01, 2005, 10:06:37 PM
Quote from: "WaffleMan"
Simple: First, randomly generate a number, then offer the user a guess. Take the user's input and compare it with the generated number: if the user's number is higher than the generated number, print "Too high!" If the user's number is lower, then print "Too low!"

Also, have a tries counter that adds 1 to itself every time the user misses the guess. If the user fails on all 5 tries, then print "Game over!"

But instead of that, have a generic tries counter, so you can let the user know how well he did. Example: "Congradulations! You got the number in num tries!"


I just don't know how to make a random number generator thingy


Title: Random number guessing game.....
Post by: Kevin_theprogrammer on December 01, 2005, 10:08:36 PM
-_-

the command RND

I suggest you read ALL of Vic's tutorials, which can be found on this site...


Title: Random number guessing game.....
Post by: WaffleMan on December 01, 2005, 10:09:42 PM
There's a simple command I learned:

getnum% = INT(RND * num) + 1  

where "num" represents the maximum value between 1 and said number.  Make sure you add "RANDOMIZE TIMER" up at the top so the number is different everytime the program is run.

Kev: Yeah, but RND doesn't generate whole numbers, only decimals smaller than 1.


Title: Random number guessing game.....
Post by: tannervp on December 01, 2005, 10:10:09 PM
well, that doesn't help. I need to know how to have it randomize the certain range.


Title: Random number guessing game.....
Post by: WaffleMan on December 01, 2005, 10:11:08 PM
You said you wanted to generate a random number between 1 and 20. The command "INT(RND * 20) + 1" will do that for yo.


Title: Random number guessing game.....
Post by: Moneo on December 01, 2005, 10:13:19 PM
Ok, since you don't want code, then I have to assume that you know how to code getting a random number between 1 and 20. Am I correct so far?
If NO, then you need coding help.
If YES, continue.

The rest is pretty trivial, with a few considerations like:

1) You should validate the user's guesses to make sure that they are between 1 and 20. If his guess is invalid, print an error message saying that it must be between 1-20, and don't count his error against the 5 total guesses.

2) Your response cannot be just "too low" or "too high", he might just guess the right number.

3) You could give him a little help, like:
- make an array of 20 entries, corresponding to the 20 numbers.
- when he makes a valid (1-20) guess that is not the answer, test to see if the guess location in the table is greater than zero. If it is, print a message saying that that guess was already used, and don't penalize him. If it is zero, just add one to the array location.

4) More sophisticated help could be given, like if the answer is 9 and he already tried 5 which you told him was too low, Now he says 3, which is obviously too low also. You could figure a way of warning him.

Have fun!
*****


Title: Random number guessing game.....
Post by: tannervp on December 01, 2005, 10:19:19 PM
Quote from: "tannervp"
well, that doesn't help...


so sorry, your post came before mine


Title: Random number guessing game.....
Post by: Moneo on December 01, 2005, 10:25:25 PM
Quote from: "tannervp"
Quote from: "tannervp"
well, that doesn't help...


so sorry, your post came before mine

I'm assuming you meant my (Moneo) post.

Did you read my other suggestions?
*****


Title: Random number guessing game.....
Post by: tannervp on December 01, 2005, 10:28:18 PM
yep. One thing...
Code:
RANDOMIZE TIMER
INPUT "Pick a number:"; num$
getnum% = INT(RND * 20) + 1
IF num$ =


THAT's where I need help.


Title: Random number guessing game.....
Post by: Deleter on December 01, 2005, 10:30:55 PM
well you can either change num$ to simply num if your user is only going to be inputting numbers. Otherwise you can do the VAL command. VAL(num$) converts your string to a number. then you can do it like this
Code:

IF VAL(num$) = getnum% THEN
    PRINT "YOU WIN"
ELSEIF num > getnum'.....etc

I'd finish the code, but it looks like you need the practice and it would do you no good if I wrote the program for you


Title: Random number guessing game.....
Post by: yetifoot on December 02, 2005, 12:25:34 AM
surely if you give someone 5 guesss from 1-20
they can always win by splitting in half till they get it.


10 lower
5   higher
7   higher
8   higher
9   bingo


Title: Random number guessing game.....
Post by: Agamemnus on December 02, 2005, 03:16:20 AM
yes, that's right.


Title: Random number guessing game.....
Post by: Moneo on December 02, 2005, 03:16:28 PM
The "splitting in half" technique is called a Binary Search. Some programmers may think of it, but most regular users wont.
*****


Title: Random number guessing game.....
Post by: axipher on December 03, 2005, 01:03:39 AM
binary, a wonderful thing, but anyway there are many tutorials on this, plus theres a program called Learn To Program Basic, from scholastic, i bought it in a bok order in grade 8 like 3 years ago and it had programs premade, one of them was a guessing game too, altough the basic files were different, the coding was BASICally the same, lol, no pun intended but it happened, anyway, the coding aint really that hard no offence, its all basic math really


Title: Random number guessing game.....
Post by: yetifoot on December 03, 2005, 02:28:39 PM
Quote
Some programmers may think of it, but most regular users wont.


i wouldnt count on it, we were taught that in the first year of secondary school(age 11-12)


Title: Random number guessing game.....
Post by: TheDarkJay on December 03, 2005, 04:05:18 PM
Isn't their a EXAMPLE that shows you how to do this on QbasicNews.Com

Also A Trick i use to make random numbers easier is

Code:
Declare FUNCTION int.rnd(base1 as long, base2 as long) as long

FUNCTION int.rnd(base1 as long, base2 as long) as long
    Return int((rnd*base2)+base1)
END FUNCTION


Int.Rnd ((The lowest possible number),(the highest Possible number)


Title: Random number guessing game.....
Post by: axipher on December 03, 2005, 07:49:42 PM
thats nice, i wish the RND function could be used like this

Code:
num = RND(x1 to x2), Step x


i remember one program that u could use to program basuc that worked like this, it was nice, u could go like

Code:
num = RND(0 to 10), Step 2


and it would be a 0,2,4,6,8, or 10, iot worked great

now while were talking about math functions, is there anyway to round a number to no decimal places in qb?


Title: Random number guessing game.....
Post by: thegrogen on December 03, 2005, 09:08:40 PM
Quote from: "axipher"

now while were talking about math functions, is there anyway to round a number to no decimal places in qb?


INT()


Title: Random number guessing game.....
Post by: yetifoot on December 03, 2005, 10:49:47 PM
Code:
Declare FUNCTION int.rnd(base1 as long, base2 as long) as long

FUNCTION int.rnd(base1 as long, base2 as long) as long
    Return int((rnd*base2)+base1)
END FUNCTION


looking at this code posted i tested and it appears incorrect ie

int.rnd(0, 5) should return number in range 0, 1 , 2 ,3, 4, 5

but it will never return 5

This is waht i use requires #include "crt.bi"

Code:
Function GetRand(nmin As uInteger, nmax As uInteger)As uInteger
  GetRand = Int((nmax - nmin + 1) * (rand() / RAND_MAX) + nmin)
End Function


however you can use this also, this is the native fb version, not sure which is faster

Code:
Function GetRand2(nmin As uInteger, nmax As uInteger) As uInteger
  GetRand2 = Int((nmax - nmin + 1) * Rnd + nmin)
End Function



EDIT : sorry just saw im in a QB forum , here is QB one

Code:
Function GetRand2(nmin As Long, nmax As Long) As Long
  GetRand2 = Int((nmax - nmin + 1) * Rnd + nmin)
End Function


Title: Random number guessing game.....
Post by: axipher on December 04, 2005, 05:20:08 PM
Quote from: "thegrogen"
Quote from: "axipher"

now while were talking about math functions, is there anyway to round a number to no decimal places in qb?


INT()


i was playing around with a code i dled and figured out that i can use

Code:
PRINT USING "###"; number


it works fine for numbers from -999 to 999 which is wat i want, its for stats and exp in an rpg


Title: Random number guessing game.....
Post by: jakeman922 on February 08, 2006, 03:06:33 PM
Quote from: "Kevin_theprogrammer"
I suggest you read ALL of Vic's tutorials, which can be found on this site...


Whatever happened to Vic, anyway? I read his tutorials about 2 years ago. He taught me almost everything I know about programming in any form of BASIC.


Title: Random number guessing game.....
Post by: Agamemnus on February 08, 2006, 07:17:40 PM
Isn't  vic "v1ctor"? That would be the guy who made and is still updating FB...


Title: Random number guessing game.....
Post by: Zack on February 08, 2006, 09:17:37 PM
I really don't think so, Aga. Although we should really ask him. Vic Luce, AKA radiohands made tutorials that taught me loads. Although he disappeared by the time I got involved in the community.