Computer Science Canada What am i doing wrong? |
Author: | Krocker [ Wed Jan 19, 2011 11:39 am ] |
Post subject: | What am i doing wrong? |
hiya, im creating a game and i keep getting an error. here are the codes:
setscreen ("graphics:max;max,nobuttonbar") % Gives names to variables and their character type var font1, font2, font3, font4, font5, font6, font7, font8, font9 : int var score : int var value : int var x, y : int process errorcorrection if strintok (strint string var) = true then else end if end errorcorrection loop % Sets the background colour black GUI.SetBackgroundColor (black) % plays an introduction music Music.PlayFileLoop ("Intro.MP3") % Creates i fade in tittle screen font1 := Font.New ("Bookman Old Style:40") Font.Draw ("TO KNOW OR NOT TO KNOW?", 120, 345, font1, 7) delay (500) Font.Draw ("TO KNOW OR NOT TO KNOW?", 120, 345, font1, 18) delay (500) Font.Draw ("TO KNOW OR NOT TO KNOW?", 120, 345, font1, 20) delay (500) Font.Draw ("TO KNOW OR NOT TO KNOW?", 120, 345, font1, 22) delay (500) Font.Draw ("TO KNOW OR NOT TO KNOW?", 120, 345, font1, 24) delay (500) Font.Draw ("TO KNOW OR NOT TO KNOW?", 120, 345, font1, white) % Sub-titile font9 := Font.New ("Harrington:20") Font.Draw ("The Game About Everything and Anything!", 250, 210, font9, 37) % Askes the users to given an input in order to continue locate (36, 95) put "HOLD '1' TO CONTINUE: " .. get value % Gets the value input in order to exit loop if value = 1 then Music.PlayFileStop % <= Stops the music when value=1 exit end if end loop cls So what am i doing wrong?[list=][/list] |
Author: | DemonWasp [ Wed Jan 19, 2011 11:47 am ] |
Post subject: | RE:What am i doing wrong? |
First, is there any particular reason why you didn't fill out the form, like you're supposed to if you actually want help? Second, strintok ( strint string var ) is probably your first problem. Third, never use process. You are setting yourself up for disaster. |
Author: | Krocker [ Wed Jan 19, 2011 12:24 pm ] |
Post subject: | RE:What am i doing wrong? |
i did not have time to fill out the sheet. anyways, now i know the problem. but how do isolve it? what are the possible solutions for the strintok problem. |
Author: | Tony [ Wed Jan 19, 2011 12:36 pm ] |
Post subject: | RE:What am i doing wrong? |
Reading the documentation for proper usage is always a good bet on solving such problems. |
Author: | Krocker [ Wed Jan 19, 2011 1:17 pm ] |
Post subject: | RE:What am i doing wrong? |
k, ill keep that in mind for next times, but i need toknow how to solve the strintok problem and fast. |
Author: | DemonWasp [ Wed Jan 19, 2011 1:20 pm ] |
Post subject: | RE:What am i doing wrong? |
Tony has already told you: go read the documentation. Here's a short version: strintok returns true if the given string can be converted to an integer and false if it cannot. strint actually tries the conversion, but will cause your program to fail if the input is invalid. To get valid input, you want to fetch input, then check whether it is valid. If the input is invalid, go get new input; if the input is valid, get its value and continue. |
Author: | Krocker [ Wed Jan 19, 2011 1:29 pm ] |
Post subject: | RE:What am i doing wrong? |
ok, first,i dont see tony's post! wats up with that. nvm. i read the documentation. but i dont understand what to putin the code. im lost!!!!!!!! its only been 2 weeks since i started turing |
Author: | Tony [ Wed Jan 19, 2011 2:23 pm ] |
Post subject: | Re: RE:What am i doing wrong? |
Krocker @ Wed Jan 19, 2011 1:29 pm wrote: i dont understand what to putin the code
DemonWasp @ Wed Jan 19, 2011 1:20 pm wrote: To get valid input, you want to fetch input, then check whether it is valid. If the input is invalid, go get new input; if the input is valid, get its value and continue. |
Author: | DemonWasp [ Wed Jan 19, 2011 2:47 pm ] | ||
Post subject: | RE:What am i doing wrong? | ||
Let me break this down even more for you. I'm going to write something that is almost exactly the code you want, you just need to translate from "English" into "Turing". Here:
Copy and paste that code block into a new file in Turing. Start by replacing the two "let...variable" lines with actual Turing variable declarations. That part should be dead simple. Next, replace the FETCH INPUT line with actually fetching (string) input from the user. Next, replace the "is-valid-integer" logic with one of the functions we've discussed earlier in this thread. Next, replace the "convert-user-input-to-integer" function with the other function. You should now have working code that will demand that you enter a valid integer before proceeding. |
Author: | Krocker [ Thu Jan 20, 2011 8:50 am ] |
Post subject: | Re: What am i doing wrong? |
like this? var input : int loop input := get input () % FETCH INPUT if (strintok (input)) then % CHECK WHETHER INPUT IS VALID exit % INPUT IS VALID, STOP CHECKING end if % If you get here, you haven't exited above. % INPUT IS INVALID, TRY AGAIN end loop var value : int value := strint (input) % GET VALUE FROM INPUT im still getting an error. but i think im just missin a few steps. lol srry about this. |
Author: | DemonWasp [ Thu Jan 20, 2011 10:23 am ] | ||
Post subject: | RE:What am i doing wrong? | ||
You almost have it. The remaining errors are because you haven't translated the first variable, or fetch-input bits correctly. First, notice that the first variable is NOT an integer, but a string. Second, remember Turing's get command, and use that. Once you've got that done, you may want to have it prompt the user for input (have a put line that says something like "Enter an integer: ". Even better, you could wrap the whole thing as a function so that getting a few integers would look like:
|
Author: | Krocker [ Thu Jan 20, 2011 11:26 am ] |
Post subject: | RE:What am i doing wrong? |
k, thx that worked. um wat do you mean by prompt the user for input. i have alreday done that if u saw my line of codes above. i think that will work. i think... also, how do i put the functions in the game?. is it possible to make its so that i only have to put it once and it will do the conversion whenever an input is given?. Heres some of the other codes in my game. loop % Instructions on how to play the game font2 := Font.New ("Times New Roman:15") Font.Draw ("THE TARGET OF THIS GAME IS TO GET AT LEAST 210 POINTS AFTER THE 20 QUESTIONS.", 4, 600, font2, black) Font.Draw ("EACH CORRECT ANSWER IS WORTH 15 POINTS AND FOR EVERY WRONG ASNWER, YOU GET -10 POINTS.", 4, 560, font2, black) Font.Draw ("SIMPLY WAIT FOR THE ANSWERS TO APEAR, THEN CLICK", 4, 520, font2, black) Font.Draw ("THE CORRECT NUMBER THAT CORESPONDS TO THE CORRECT ANSWER.", 4, 480, font2, black) Font.Draw ("GOOD LUCK AND HAVE FUN!!!!", 4, 440, font2, blue) % Waits for the input form the user to continue locate (40, 80) put "HOLD '2' TO CONTINUE: " .. get value if value = 2 then exit end if end loop cls % Now comes the questions and the answers font3 := Font.New ("Arial Black:25") % <=Font for the question font4 := Font.New ("Arial Black:15") % <=Font for the answers font5 := Font.New ("Arial Black :20") % <=Font for the Answer Statment score := 0 delay (500) loop Pic.ScreenLoad ("WW2.JPG", 600, 2, picCopy) % Question 1 locate (1, 2) put "Score ", score Font.Draw ("1. World War 2 (WWII) ended in what year?", 100, 450, font3, black) Font.Draw ("i) 1945", 280, 400, font4, black) Font.Draw ("ii) 1845", 280, 360, font4, black) Font.Draw ("iii) 1950", 580, 400, font4, black) Font.Draw ("iv) None of the Above", 580, 360, font4, black) locate (43, 3) put "Your Answer: " .. get value % determines which button was pressed and if the answer is right or wrong if value = 1 %<= Right answer then Music.PlayFile ("CorrectAnswer.MP3") Font.Draw ("Correct : The answer is i) 1945", 190, 190, font5, green) delay (3000) cls score := score + 15 exit else Font.Draw ("INCORRECT", 190, 190, font5, red) Music.PlayFile ("wrong.MP3") score := score - 5 delay (1000) cls end if end loop %Question 2 loop Pic.ScreenLoad ("MapofCanada.JPG", 300, 10, picCopy) locate (1, 2) put "Score ", score Font.Draw ("2. Nunavut was established in _______?", 100, 450, font3, black) Font.Draw ("i) 2000", 280, 400, font4, black) Font.Draw ("ii) 1993", 280, 360, font4, black) Font.Draw ("iii) 1890", 580, 400, font4, black) Font.Draw ("iv) 1990", 580, 360, font4, black) locate (43, 3) put "Your Answer: " .. get value if value = 2 then Music.PlayFile ("CorrectAnswer.MP3") Font.Draw ("Correct : The answer is i) 1993", 50, 270, font5, green) delay (3000) cls score := score + 15 exit else Font.Draw ("INCORRECT", 190, 270, font5, red) Music.PlayFile ("wrong.MP3") score := score - 5 delay (1000) cls end if end loop also, i have 20 questions too, so ya. lol |
Author: | DemonWasp [ Thu Jan 20, 2011 11:44 am ] | ||||
Post subject: | RE:What am i doing wrong? | ||||
"Prompt" refers to "asking the user for something", not just "wait for user input". It's the difference between:
and
Don't worry about putting anything in a function, or into your main program, until you have a separate chunk of code that works correctly. |
Author: | Krocker [ Thu Jan 20, 2011 11:50 am ] |
Post subject: | RE:What am i doing wrong? |
i understand what prompt is, i have many put statments tht ask for user input, so ya. now i just need to know how to but those codes into my game. so ya. |
Author: | DemonWasp [ Thu Jan 20, 2011 12:31 pm ] |
Post subject: | RE:What am i doing wrong? |
First, encapsulate the (corrected) code for getting integer input into a function. The function should take one string argument (the prompt to show the user) and return an integer (the first correct integer value entered by the user). Once you do that, you should be able to refer to that code with the single method calls I gave as an example two posts ago. |