Computer Science Canada Turning help with randint statement |
Author: | CHE.R.RY_YUI [ Thu Jan 01, 2009 4:45 pm ] |
Post subject: | Turning help with randint statement |
i'm creating a game for school. but, i need some help with randint statement. randint is "randint ( var i : int, low, high : int )". but i don't want randomly from lowest to highest. (ex. 0 to 100) i give specific numbers (ex. 0, 30, 50, 70, 100) and program chooses randomly choose from it, not randomly from 0 to 100. how can i do that? please help! |
Author: | Insectoid [ Thu Jan 01, 2009 4:55 pm ] | ||
Post subject: | RE:Turning help with randint statement | ||
First, use Rand.Int (low, high : int). It's a function rather than a procedure, so can be used in more efficient ways. ex.
You could do this with a bit of math, but if statements are better. There are 5 possible options: 0, 30, 50, 70, or 100. Set a variable to a random number between 1 and 5 inclusively (Rand.Int (1, 5)). If number = 1, make number 0. if number = 2, make number 30. if number = 3, make number 50, etc. |
Author: | CHE.R.RY_YUI [ Thu Jan 01, 2009 5:39 pm ] |
Post subject: | RE:Turning help with randint statement |
thank you. It helps me a lot even though i haven't learned about Rand.Int!! |
Author: | syntax_error [ Thu Jan 01, 2009 6:12 pm ] |
Post subject: | RE:Turning help with randint statement |
Have an array of the set values that you want to pick 'randomly' from and then use rand.Int to pick a 'random' index of that array and that becomes your value, much cleaner, imho. |
Author: | Insectoid [ Thu Jan 01, 2009 6:14 pm ] |
Post subject: | RE:Turning help with randint statement |
Cleaner, but either works. If you had a hundred possibilities, you would use the array, while with 5 it is perfectly feasible to use a couple if's. |
Author: | CHE.R.RY_YUI [ Thu Jan 01, 2009 6:23 pm ] | ||||||||
Post subject: | RE:Turning help with randint statement | ||||||||
can you help me with one more thing? My program is to tell the player that the number that program ramdomly chooses and the player chooses are the same or different. i want the player to make a guess by using buttons. so i made this...
This is the one you taught me
how do i state that "wait for the mouse is clicked on the buttons above that says "100 million", "50 million" etc. to get the player's guess" the program doesn't wait for the mouse to click. it says "guess has no variable"
thanks! Mod Edit: Remember to use syntax tags! Thanks ![]()
|
Author: | DarkRider [ Fri Jan 02, 2009 10:45 pm ] | ||||
Post subject: | Re: Turning help with randint statement | ||||
Based on the given code, you are not proccessing your GUI butons. To do so use the GUI.ProcessEvent (). It's a function that will return true if the GUI quit flag is true (calling GUI.Quit () will make this flag true), thus you will have to evaluate the result. Everythime the function is called it will check the state of every declared GUI object before it and update the screen with the currrent graphic of that GUI. In order to make sure your GUI object stays active, you will have to keep calling GUI.ProcessEvent (), thus similar code to the following has to be used:
Also make sure you declare guess in your code as well:
|
Author: | CHE.R.RY_YUI [ Sat Jan 03, 2009 7:29 am ] | ||||||
Post subject: | RE:Turning help with randint statement | ||||||
thanks, again and i'm sorry for the trouble. it doesn't seem to be working. so far this is what i have done T_T pls help.
***This part!!! i can click on the buttons, but no response of the message that says "Same", "Larger" or "Smaller" that are on if statement below.
Mod Edit: Remember to use syntax tags! Thanks ![]()
|
Author: | The_Bean [ Sat Jan 03, 2009 9:12 am ] | ||
Post subject: | Re: Turning help with randint statement | ||
Your problem is with the:
Your never actually exiting the loop, because your never making GUI.ProcessEvent = true. To do this you need to add GUI.Quit inside of every procedure to make it = true in every case where they click a button. Also if you want to play multiple times you will also need a GUI.ResetQuit in there somewhere to make to make GUI.ProcessEvent = false again. And your smaller and larger are backwards. And you can only make 1 guess at the number, then it is changed to something different. |
Author: | Tony [ Sat Jan 03, 2009 3:30 pm ] | ||
Post subject: | RE:Turning help with randint statement | ||
You seem to have trouble understanding how loops work. Here's a tutorial -- http://compsci.ca/v3/viewtopic.php?t=3678
Is effectively an infinite loop -- you can't just drop it into the middle of the program, and hope that you threw enough source code into the file to make it work. |
Author: | CHE.R.RY_YUI [ Sat Jan 03, 2009 4:32 pm ] | ||
Post subject: | Re: Turning help with randint statement | ||
The_Bean @ Sat Jan 03, 2009 10:12 pm wrote: Your problem is with the:
Your never actually exiting the loop, because your never making GUI.ProcessEvent = true. To do this you need to add GUI.Quit inside of every procedure to make it = true in every case where they click a button. Also if you want to play multiple times you will also need a GUI.ResetQuit in there somewhere to make to make GUI.ProcessEvent = false again. And your smaller and larger are backwards. And you can only make 1 guess at the number, then it is changed to something different. my turing doesn't have GUI.ResetQuit. it says "ResetQuit is not in the export list of GUI." is there another way to do it when i want to play multiple times? now, my program asks for only one guess and doesn't ask for another guess. it just keeps on checking whether i get the first guess right or wrong. |
Author: | CHE.R.RY_YUI [ Sat Jan 03, 2009 4:37 pm ] | ||
Post subject: | Re: RE:Turning help with randint statement | ||
Tony @ Sun Jan 04, 2009 4:30 am wrote: You seem to have trouble understanding how loops work. Here's a tutorial -- http://compsci.ca/v3/viewtopic.php?t=3678
Is effectively an infinite loop -- you can't just drop it into the middle of the program, and hope that you threw enough source code into the file to make it work. i understand how loop works, but that time, i didn't know how GUI.ProcessEvent works and just put it in to my program. i feel bad for asking many questions. is there any tutorial that is related to my problem? Never mind. i found one tutorial about GUI.ProcessEvent. thanks (^__^) |
Author: | Tony [ Sat Jan 03, 2009 4:48 pm ] | ||
Post subject: | RE:Turning help with randint statement | ||
Fair enough. GUI module is kind of confusing. The basic point is that GUI.ProcessEvent needs to be called on regular enough basis. As long as the mouse button is held down when the ProcessEvent is fired off, your GUI will work. This means that it doesn't need a dedicated loop; this would be fine:
Though it seems that your guess <=> k comparison needs to be behind a button, so that you don't do this check every step of the loop. |
Author: | CHE.R.RY_YUI [ Sat Jan 03, 2009 5:24 pm ] | ||
Post subject: | Re: RE:Turning help with randint statement | ||
Tony @ Sun Jan 04, 2009 5:48 am wrote: Fair enough. GUI module is kind of confusing. The basic point is that GUI.ProcessEvent needs to be called on regular enough basis. As long as the mouse button is held down when the ProcessEvent is fired off, your GUI will work. This means that it doesn't need a dedicated loop; this would be fine:
Though it seems that your guess <=> k comparison needs to be behind a button, so that you don't do this check every step of the loop. but when i put "exit when GUI.ProcessEvent" behind the guess and k comparison, it says that guess has no value. i'm confused @_@ |
Author: | Tony [ Sat Jan 03, 2009 5:36 pm ] |
Post subject: | Re: RE:Turning help with randint statement |
you never initialize guess -- it has no value until a button is clicked. That's why I've mentioned Tony @ Sat Jan 03, 2009 4:48 pm wrote: Though it seems that your guess <=> k comparison needs to be behind a button, so that you don't do this check every step of the loop.
Your program checks guess against k, every time, even before any buttons are pressed. |
Author: | CHE.R.RY_YUI [ Sat Jan 03, 2009 7:15 pm ] |
Post subject: | RE:Turning help with randint statement |
how can i solve this? my program asks for only one guess and doesn't ask for another guess. it just keeps on checking whether i get the first guess right or wrong forever. |
Author: | Tony [ Sat Jan 03, 2009 7:24 pm ] |
Post subject: | RE:Turning help with randint statement |
you should move that check outside of the loop. I would suggest making a procedure, and calling that procedure from the procedures that are called by your GUI buttons. |
Author: | CHE.R.RY_YUI [ Mon Jan 05, 2009 5:41 am ] |
Post subject: | RE:Turning help with randint statement |
How do i reset GUI.ProcessEvent to make it false again, so that i can use it many time? my turing doesn't have GUI.ResetQuit. i need to use GUI.ProcessEvent again in my program. |
Author: | Tony [ Mon Jan 05, 2009 11:04 am ] |
Post subject: | RE:Turning help with randint statement |
one possible solution is to not use Quit, then you wouldn't need to reset it. |
Author: | CHE.R.RY_YUI [ Tue Jan 06, 2009 10:13 pm ] |
Post subject: | RE:Turning help with randint statement |
thank you for your help i have one more question how do i write the if statement that states "if the button is pressed"? |
Author: | Tony [ Tue Jan 06, 2009 10:37 pm ] |
Post subject: | RE:Turning help with randint statement |
if you are using GUI buttons -- they call a specific procedure when they are pressed. If the only way to get to that particular procedure is via a button, it can be assumed that it was pressed. |