Qbasicnews.com
April 03, 2020, 07:13:12 PM
 Pages: 1 [2] 3
 Author Topic: QUIZ GAME WITH SCORE  (Read 7408 times)
tabnewflax
New Member

Posts: 22

 « Reply #15 on: March 16, 2004, 05:56:28 AM »

Thank you guys for the help I guess this topic can be closed now...

But instead of doing a new one I can ask a new question here.

What if I want it to be random? I know Mango had an example of that with the .txt file but to extreme for me now I think. So what do you say?
 Logged
R@dioman
Ancient QBer

Posts: 410

 « Reply #16 on: March 16, 2004, 11:52:14 AM »

To make a random number, use the RND function. It returns a single precision fraction between 0 and 1. So to get a random number between 1 and 10 would be this:

a% = INT(RND * 10) + 1

or between 20 and 100:

a% = INT(RND * 80) + 20

So the number you multiply by RND is the amount of numbers in the range, and the added number is where to start extracting from.
 Logged

Zack
*/-\*

Posts: 3974

 « Reply #17 on: March 16, 2004, 12:19:11 PM »

You mean, have a set of countries and their corrosponding capitals, and then randomly pick which city to ask?
It's a little more tricky, but here are the things you should know: RND, TYPE, and arrays.
I'll do just a really really quick overview of them, but if you really don't know them, just ask, and I'll go further.
A TYPE is a way of organizing your variables. Think of it as a way to have more than one place to store stuff in your variable.
Code:
'Here's an example of a TYPE. This is the one you'll use in the quiz game:
TYPE Country
CountryName AS STRING * 100
CityName        AS STRING * 100
END TYPE

You may have noticed something odd: the *100 at the end. See, when declaring strings in a TYPE, you must explicitly declare the maximum number of characters that the string will hold. In this case, I don't think any country or city will have more than 100 characters in it's name, so I decided on that.
A TYPE is not a variable. It is a format for one. So you need to declare a variable (using the DIM command) of type Country. Normally, you can use DIM like this:
DIM MyVar AS INTEGER
Notice the AS INTEGER. Well, INTEGER is a TYPE. Except it's built into the QB language.
So we can DIM it like this:
Code:
DIM CountryVar AS Country

You can access the members of a TYPE's variables (in our case, CountryName and CityName) by using the previously declared variable of type Country, followed by a period, and then the member name.
Code:
CountryVar.CountryName="Albania: ";  'Print the prompt
CountryVar.CityName="tirana"
INPUT City\$
IF LCASE\$(City\$)=CountryVar.CityName THEN
PRINT "Right!"
Score=Score+1
END IF

Did you get all that? At the end of the program, to check whether the user got the correct answer for Albania, you check if CountryVar.CorrectAnswer is equal to 1.

Now we get to arrays. An array is a just a variable that holds a bunch of other variables. The "inside variables" or elements of the array can be of any type...INTEGER, SINGLE, STRING....or TYPE "Country"!
This is how you declare an array:
Code:
DIM CountryArray(1) AS Country

This makes an array, CountryArray, of type Country, with two elements. Why two elements? Because the "(1)" we put indicates that the element numbers go up to 1. And it starts at zero. So we have the first element, element zero, and the second, element 1.
To access elements of the array, it's simple:
Code:
CountryArray(0).CountryName="Albania"
CountryArray(0).CityName="tirana"
CountryArray(1).CountryName="Afghanistan"

And so on.

RND is a function that returns a random number between 0 and 1. RANDOMIZE TIMER initializes it.
Code:
RANDOMIZE TIMER
A=RND
PRINT A

See? Run the program again, you'll get a different number printed out.
To make RND return a number that is an integer (no fractional part), and is between 0 and 1 (because there are two subscripts in our country array, and we want to randomly choose which subscript to use), simply do this:
Code:
RANDOMIZE TIMER
A=CINT(RND)  'the CINT function rounds the given number
PRINT A

Either you'll get 0 or you'll get 1.
Now, to get a number between 0 and 10, you'd do this:
Code:
RANDOMIZE TIMER
A=CINT(RND*10)
PRINT A

And for a number between 0 and 11, you would do * 11. Get the picture?

Now, let's fit this all together!
Code:
RANDOMIZE TIMER
TYPE Country
CountryName AS STRING * 100
CityName AS STRING * 100
END TYPE
DIM Countries(2) AS Country
Countries(0).CountryName="Albania: "
Countries(0).CityName="tirana"
Countries(1).CountryName="Afghanistan: "
Countries(1).CityName="kabul"
Countries(2).CityName="ottawa"
WhichCountry=CINT(RND*2)   'between 0 and 2
PRINT Countries(WhichCountry).CountryName;  'prompt
INPUT City\$
IF LCASE\$(City\$) = Countries(WhichCountry).CityName THEN
PRINT "Right!"
Score=Score+1
END IF
 Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
R@dioman
Ancient QBer

Posts: 410

 « Reply #18 on: March 16, 2004, 03:46:31 PM »

Zack, I think it's better that you don't write the entire thing for him. Just push him in the right direction. You'll be helping him out more that way because he'll be learning more. :wink:
 Logged

Zack
*/-\*

Posts: 3974

 « Reply #19 on: March 16, 2004, 04:53:14 PM »

Well, true, but I think I went over all the aspects and points of it rather well.
 Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
R@dioman
Ancient QBer

Posts: 410

 « Reply #20 on: March 16, 2004, 05:05:33 PM »

Zack wrote:
Quote

Now, to get a number between 0 and 10, you'd do this:
Code:
RANDOMIZE TIMER
A=CINT(RND*10)
PRINT A

This is not true. That would give you a number between 0 and 9. Here's what I wrote 2 posts up:
Quote

To make a random number, use the RND function. It returns a single precision fraction between 0 and 1. So to get a random number between 1 and 10 would be this:

a% = INT(RND * 10) + 1

or between 20 and 100:

a% = INT(RND * 80) + 20

So the number you multiply by RND is the amount of numbers in the range, and the added number is where to start extracting from.
 Logged

Zack
*/-\*

Posts: 3974

 « Reply #21 on: March 16, 2004, 05:20:51 PM »

Whoops, my mistake there, thanks for seeing that.
 Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
peace
Member

Posts: 31

 « Reply #22 on: March 16, 2004, 06:36:11 PM »

REM QR = questions right C\$ = capitial answer
CLS
QR=1:C\$=""
INPUT "Afghanistan:";C\$
IF C\$ = "kabul" then
QR = QR + 1
PRINT "You are correct!!!"
ELSE
PRINT "You are wrong the correct answer is kabal!"
ENDIF
...

Imknow that is simple but the main thing is just have a variable set that only is added to qwhen the answer is correct.
 Logged

eace is desirable but, War is inevitable.
R@dioman
Ancient QBer

Posts: 410

 « Reply #23 on: March 16, 2004, 06:46:56 PM »

Zack: sorry, you we're right about the random numbers. Actually, we we're both right. I noticed you were using CINT(RNd... and I was using INT(RND...
The difference is how they round up the numbers. CINT converts to integer by rounding the fractional part of the expression. So using CINT(RND * 10) would give you a number between 0 and 10. Using INT(RND... rounds it off to the next largest integer less than or equal to the expression. So INT(RND * 10) would give you numbers between 0 and 9.
 Logged

Zack
*/-\*

Posts: 3974

 « Reply #24 on: March 16, 2004, 07:01:20 PM »

Quote from: "R@dioman"
Zack: sorry, you we're right about the random numbers. Actually, we we're both right. I noticed you were using CINT(RNd... and I was using INT(RND...
The difference is how they round up the numbers. CINT converts to integer by rounding the fractional part of the expression. So using CINT(RND * 10) would give you a number between 0 and 10. Using INT(RND... rounds it off to the next largest integer less than or equal to the expression. So INT(RND * 10) would give you numbers between 0 and 9.
Heeeeey....I thought you were saying I was wrong, but you were wrong. :wink:  That's okay.
 Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
tabnewflax
New Member

Posts: 22

 « Reply #25 on: March 17, 2004, 05:14:36 PM »

I'm confused  :wink:

But hopefully I will get it
 Logged
Zack
*/-\*

Posts: 3974

 « Reply #26 on: March 17, 2004, 05:24:55 PM »

Then *say* what confuses you, and I'll try to explain. :wink:
 Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
tabnewflax
New Member

Posts: 22

 « Reply #27 on: March 19, 2004, 07:45:45 AM »

Wow now it's working, but....

I had to put in a DO/LOOP before whichcountry and after and if
otherwise it wont loop, hmm

Have I made anything wrong or is it another way to do it easier?

And, how do I do so it will stop after going trough every country and so it doesnt pick the same country twice?
 Logged
*/-\*

Posts: 3655

 « Reply #28 on: March 19, 2004, 09:31:30 AM »

Something like that would require that you use "flags", or variables that tell you whether or not a certain one's been used. In most languages, there's a Boolean type, but we don't have that in QB so you're probably gonna be stuck with Integers. Anyways, one way of doing this is to have an array, with each element in it corresponding to a certain capital. Like if you have 20 capitols, you can have an array sorta like this:

Code:
DIM capitolArray(1 TO 20) AS INTEGER

and set the value to 1 when the answer's been given. Also, on each loop, check to make sure the value isn't set to 1, and if it is, just get a new number. I don't know how experienced you are, but this isn't too terribly complicated. It does reqiure a solid knowledge of variables and programming logic though.
 Logged

I'd knock on wood, but my desk is particle board.
whitetiger0990
__/--\__

Posts: 2964

 « Reply #29 on: March 19, 2004, 10:56:26 AM »

*is pondering of posting his quiz game that he ust made that works*

oh well heres a snippit...
Code:
IF array(rand).flag = 0 THEN
array(rand).flag = 1
'PRINT QUESTION
END IF

yea... i removed some code where the commented part is and put what shouold me there...

edit: i used types and an array too... rand is a random number.
 Logged

[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
 Pages: 1 [2] 3