Qbasicnews.com

QBasic => QB Discussion & Programming Help => Topic started by: tabnewflax on March 14, 2004, 05:26:42 PM



Title: QUIZ GAME WITH SCORE
Post by: tabnewflax on March 14, 2004, 05:26:42 PM
Im doing a simple quiz game containging the capitals of the world. And Im trying to add score to it so you will get one score for every right.

1.The problem is when you answer right the first time and goes to the second and answer right again the counter resets I have tried a lot of things but cannot come up with any good soulution. Here is the code so you can see and help me better.

2. Another question to how do I do if the user inputs Kabul or KABUL. I have tried to input LCASE$ and those things but I dont get it to work and I dont think it does eiter.
----------------------------------------------------
afghanistan:
INPUT "Afghanistan: ", city$
IF city$ = "kabul" then
            goto albania
else
             goto afgwrong
end if

afgwrong:
print "Wrong!"
             goto albania

INPUT "Albania: ", city$
IF city$ = "tirana" then
            goto sweden
else
             goto albwrong
end if

albwrong:
print "Wrong!"
             goto sweden

'AND SO ON.....................................................

And I'm sure there is an easier way to do this, if you know which you probably do plz write it down  :wink:


Title: QUIZ GAME WITH SCORE
Post by: Zack on March 14, 2004, 05:49:14 PM
Ewwww...Look at all that GOTO.
Code:
INPUT "Afghanistan: "; City$
IF LCASE$(City$)="kabul" THEN
   PRINT "Right!"
   Afghanistan_Correct=1
ELSE
   PRINT "Wrong!"
END IF
INPUT "Albania: "; City$
IF LCASE$(City%)="tirana" THEN
   PRINT "Right!"
   Albania_Correct=1
ELSE
   PRINT "Wrong!"
END IF
'And so on...

At the end, you can just check if the Afghanistan_Correct is equal to 1 or not to see if you got Afghanistan correct. If it is equal to one, then you got it correct. Otherwise, you got it wrong. You can do that with all the other Countryname_Correct variables that you put.


Title: QUIZ GAME WITH SCORE
Post by: Mango on March 14, 2004, 07:04:48 PM
Hi tabnewflax,

Like all programming projects, there are many ways to accomplish the same thing.  If I were wanting to solve your problem, I would structure the program as follows:

1st, create a file containing the countries and capitals as follows:

"IntCaps.txt"
Country, Capital
Italy, Rome
England, London


Then, I'd make the main program read the file into a pair of arrays, 1-containing the countries, and the other containing the capitals.  Then, I'd select a random array element, display the country and ask the user for the capital.  

The advantage of this method is that you could then use the same program to quiz any two related words...for example, you could make another file containing the "element name" and "symbol" if, for example you were studying chemistry...  

another advantage is that all your data is located in 1-place...separate from the program...in a format that is easy to modify.  Also, you could give the user choice...take the geography quiz, or the chemistry quiz...and altering it to give the song/artist quiz would be trivial.


Title: QUIZ GAME WITH SCORE
Post by: tabnewflax on March 15, 2004, 06:04:24 AM
Zack... I don't get how the score things work, and I can't write the underline than it stands end-of-statement. I guess it might be ok to skip the underline, but I dont get it how I shall check it in the end I need a more detailed explanation of how getting the scores together in the end. But thanx for the other stuffs it cleared up a bit.

And thanx Mango for yours too but that is a bit advance for me now I think, I will deal with your idea later ;)


Title: QUIZ GAME WITH SCORE
Post by: Zack on March 15, 2004, 11:47:03 AM
Quote from: "tabnewflax"
Zack... I don't get how the score things work, and I can't write the underline than it stands end-of-statement. I guess it might be ok to skip the underline, but I dont get it how I shall check it in the end I need a more detailed explanation of how getting the scores together in the end. But thanx for the other stuffs it cleared up a bit.

And thanx Mango for yours too but that is a bit advance for me now I think, I will deal with your idea later ;)

Don't worry at all about the underscores (the _ symbols) in the name of the variables. See, an underscore is just another legal character for a variable. Using one doesn't change how the variable acts, or what you can do with it. Just ignore it if you like - you can even change it's name to AlbaniaCorrect or AfghanistanCorrect.

Okay, basically, it asks you for the name of a capital of a country. You enter the name, and it's stored in the City$ variable. Then is compares the lowercase of City$ (LCASE$(City$)) to the correct city (in all lowercase, of course). If you got it right, it says "Right!" and it sets a variable called Countryname_correct to 1. Then, at the end of the program if you want to tally up how many city names you got correct, just check if the appropriate Countryname_correct variable is equal to 1.
See?


Title: QUIZ GAME WITH SCORE
Post by: na_th_an on March 15, 2004, 12:08:15 PM
GRR! Underscores are not allowed in QB! :P


Title: QUIZ GAME WITH SCORE
Post by: Agamemnus on March 15, 2004, 12:49:31 PM
Unless you hack it.


Title: QUIZ GAME WITH SCORE
Post by: Zack on March 15, 2004, 01:23:25 PM
Quote from: "na_th_an"
GRR! Underscores are not allowed in QB! :P

Huh? Yes they are... :normal:
Code:
A_B_C_D=5
PRINT A_B_C_D

Works fine.


Title: QUIZ GAME WITH SCORE
Post by: tabnewflax on March 15, 2004, 03:07:16 PM
Quote from: "Zack"
Then, at the end of the program if you want to tally up how many city names you got correct, just check if the appropriate Countryname_correct variable is equal to 1.
See?


I'm sorry that I am nagging so much, but I still don't get how to display the score at the end :P


Title: QUIZ GAME WITH SCORE
Post by: R@dioman on March 15, 2004, 03:12:48 PM
Whenever you get the answer correct, add an amount to a Score variable, and when it's time to display it, use the PRINT command like this:

PRINT Score%

Remember to use the appropriate variable type. If the score won't ever be higher than 32767, then use an integer (Score%). If it goes higher, use a long (Score&). More info on data types in QB's help.


Title: QUIZ GAME WITH SCORE
Post by: Zack on March 15, 2004, 03:14:06 PM
Ohhhh, I see what you want. That's pretty simple:
Code:
INPUT "Afghanistan: "; City$
IF LCASE$(City$)="kabul" THEN
   PRINT "Right!"
   Score=Score+1
ELSE
   PRINT "Wrong!"
   REM Score=Score-1 'Un-REM this to make it so that you lose a point with every wrong answer
END IF
INPUT "Albania: "; City$
IF LCASE$(City%)="tirana" THEN
   PRINT "Right!"
   Score=Score+1
ELSE
   PRINT "Wrong!"
   REM Score=Score-1 'Read above comment for info on this line
END IF
'And so on...
PRINT Score

And then the variable Score holds the total score (the total number of answers, in this case).


Title: QUIZ GAME WITH SCORE
Post by: na_th_an on March 15, 2004, 03:57:29 PM
Quote from: "Zack"
Quote from: "na_th_an"
GRR! Underscores are not allowed in QB! :P

Huh? Yes they are... :normal:
Code:
A_B_C_D=5
PRINT A_B_C_D

Works fine.


No, it doesn't. At least in QB 4.5:

(http://usuarios.lycos.es/firstqbasicforum/stuff/notunderscores.gif)

Quote from: "QB help"
A BASIC variable name may contain up to 40 characters. The characters  
allowed in a variable name are letters, numbers, the period (.), and  
the type-declaration characters (%, &, !, #, and $). The first character  
in a variable name must be a letter. If a variable begins with FN, it is  
assumed to be a call to a DEF FN function.  


Title: QUIZ GAME WITH SCORE
Post by: Z!re on March 15, 2004, 04:08:24 PM
This code:
Code:
pri_
nt "Hello w_
orld!"


If you open it in QB you will se: Print "Hello World!"

_ is used as line continuation. I use . instead if I want a eparator in the variable name:

This_Var becomes This.Var...


Title: QUIZ GAME WITH SCORE
Post by: Zack on March 15, 2004, 05:04:35 PM
I keep making this variable-name stupid mistakes! Dammit. Thanks, Nath.


Title: QUIZ GAME WITH SCORE
Post by: Agamemnus on March 15, 2004, 09:30:10 PM
The _ line separation thing only works when loading in a program, or something.


Title: QUIZ GAME WITH SCORE
Post by: tabnewflax 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?


Title: QUIZ GAME WITH SCORE
Post by: R@dioman 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.


Title: QUIZ GAME WITH SCORE
Post by: Zack 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
   CorrecAnswer AS INTEGER
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!"
   CountryVar.CorrectAnswer=1
   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).CountryName="Canada: "
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


Title: QUIZ GAME WITH SCORE
Post by: R@dioman 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:


Title: QUIZ GAME WITH SCORE
Post by: Zack 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.


Title: QUIZ GAME WITH SCORE
Post by: R@dioman 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.


Title: QUIZ GAME WITH SCORE
Post by: Zack on March 16, 2004, 05:20:51 PM
Whoops, my mistake there, thanks for seeing that.


Title: simple
Post by: peace 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.


Title: QUIZ GAME WITH SCORE
Post by: R@dioman 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.


Title: QUIZ GAME WITH SCORE
Post by: Zack 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.


Title: QUIZ GAME WITH SCORE
Post by: tabnewflax on March 17, 2004, 05:14:36 PM
I'm confused  :wink:

But hopefully I will get it


Title: QUIZ GAME WITH SCORE
Post by: Zack on March 17, 2004, 05:24:55 PM
Then *say* what confuses you, and I'll try to explain. :wink:


Title: QUIZ GAME WITH SCORE
Post by: tabnewflax 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 :P

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?


Title: QUIZ GAME WITH SCORE
Post by: adosorken 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. :)


Title: QUIZ GAME WITH SCORE
Post by: whitetiger0990 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
'INPUT ANSWER
'CHECK ANSWER
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.


Title: QUIZ GAME WITH SCORE
Post by: tabnewflax on March 19, 2004, 11:25:19 AM
I started a week ago with Qbasic so I don't know a shit, but I'am very greatful that you are so kind with help on thÝs fourm :wink:

I have not tested all the ideas but I will soon


Title: QUIZ GAME WITH SCORE
Post by: whitetiger0990 on March 19, 2004, 11:36:33 AM
heh... don't worry... you hopefully learn eventually. Everyone here... *ahem* ok... lots of people here will be glad to help.




edit: heres my quiz game...... look at it...
Code:
CLS
RANDOMIZE TIMER
'######below defines the max ammount of questions
maxq = 6
TYPE type1
flag AS SINGLE
question AS STRING * 30
answer AS STRING * 30
END TYPE
'######dimensiont the array as the max ammount of questions
DIM array(1 TO maxq) AS type1
'######below sets up the array
FOR i = 1 TO maxq
READ array(i).question
READ array(i).answer
array(i).flag = 0
NEXT i
FOR i = 1 TO maxq
'######this DO LOOP is here so that when it checks for a random number
'######it doesn't act like it was asked. it goes anround until it finds an
'######unasked question
DO
'######below finds a random number and checks to see if the question
'######was asked and if not sets flag to one (aka asked)
rand = INT(RND * maxq) + 1
IF array(rand).flag = 0 THEN
array(rand).flag = 1
'######below prints the question
PRINT array(rand).question
'######below is where you input your answer
INPUT a$
'######below checks the answers and if you are wrong it says the correct answer
IF LCASE$(a$) = LCASE$(RTRIM$(LTRIM$(array(rand).answer))) THEN score = score + 1: PRINT "Correct" ELSE PRINT "Wrong, the correct answer is "; LCASE$(RTRIM$(LTRIM$(array(rand).answer)))
EXIT DO
END IF
LOOP
NEXT i
PRINT
'######below prints something depending on your score
SELECT CASE score
CASE maxq: PRINT "You got them all right! Here's a cookie!"
CASE IS > maxq / 2: PRINT "Good job! You got"; score; "out of"; maxq
CASE 0: PRINT "You didn't get any correct..."
CASE ELSE: PRINT "You can do better but good job! You got"; score; "out of"; maxq
END SELECT
'######below are the data that holds the questions and answers
DATA "Will you answer no?", "maybe"
DATA "Are you dead?", "no"
DATA "Are you hungry?", "no"
DATA "What is the square root of 36", "6"
DATA "Who are you?", "Dead"
DATA "Can I poke you?", "42"


Title: QUIZ GAME WITH SCORE
Post by: Agamemnus on March 19, 2004, 04:00:44 PM
That's capital. Unless you actually want to quiz people on the capitols of capitals...


Title: QUIZ GAME WITH SCORE
Post by: adosorken on March 22, 2004, 10:03:23 AM
Quote from: "Agamemnus"
That's capital. Unless you actually want to quiz people on the capitols of capitals...

 :roll:  :roll:  :roll:  :roll:  :roll:
Typical Nitpicking 101, meet your new professor: Agamemnus! :)