
-----------------------------------
CHE.R.RY_YUI
Thu Jan 01, 2009 4:45 pm

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!

-----------------------------------
Insectoid
Thu Jan 01, 2009 4:55 pm

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.
var foo := Rand.Int (1, 10)

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.

-----------------------------------
CHE.R.RY_YUI
Thu Jan 01, 2009 5:39 pm

RE:Turning help with randint statement
-----------------------------------
thank you. It helps me a lot even though i haven't learned about Rand.Int!!

-----------------------------------
syntax_error
Thu Jan 01, 2009 6:12 pm

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.

-----------------------------------
Insectoid
Thu Jan 01, 2009 6:14 pm

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.

-----------------------------------
CHE.R.RY_YUI
Thu Jan 01, 2009 6:23 pm

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...
procedure onehundred
    guess := 100
end onehundred
procedure seventy
    guess := 70
end seventy
procedure fifty
    guess := 50
end fifty
procedure thirty
    guess := 30
end thirty
procedure zero
    guess := 0
end zero

var button4 : int := GUI.CreateButton (100, 260, 0, "$100 million", onehundred)
var button5 : int := GUI.CreateButton (100, 220, 0, "$70 million", seventy)
var button6 : int := GUI.CreateButton (100, 180, 0, "$50 million", fifty)
var button7 : int := GUI.CreateButton (100, 140, 0, "$30 million", thirty)
var button8 : int := GUI.CreateButton (100, 100, 0, "$0 million", zero)

This is the one you taught me

var j := Rand.Int (1, 5)
var k : int
if j = 1 then
            k := 100
        elsif j = 2 then
            k := 70
        elsif j = 3 then
            k := 50
        elsif j = 4 then
            k := 30
        elsif j = 5 then
            k := 0
end if


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"
        if k = guess then
            put "same"
        else
            put "different"
        end if

thanks!

Mod Edit: Remember to use syntax tags! Thanks :) [syntax="turing"]Code Here[/syntax]

-----------------------------------
DarkRider
Fri Jan 02, 2009 10:45 pm

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:

loop
    exit when GUI.ProcessEvent ()
end loop

Also make sure you declare guess in your code as well:

var guess : int

-----------------------------------
CHE.R.RY_YUI
Sat Jan 03, 2009 7:29 am

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.

import GUI
setscreen ("graphics:1000;650")

var guess : int
procedure onehundred
    guess := 100
end onehundred
procedure seventy
    guess := 70
end seventy
procedure fifty
    guess := 50
end fifty
procedure thirty
    guess := 30
end thirty
procedure zero
    guess := 0
end zero

var button4 : int := GUI.CreateButton (100, 260, 0, "$100 million", onehundred)
var button5 : int := GUI.CreateButton (100, 220, 0, "$70 million", seventy)
var button6 : int := GUI.CreateButton (100, 180, 0, "$50 million", fifty)
var button7 : int := GUI.CreateButton (100, 140, 0, "$30 million", thirty)
var button8 : int := GUI.CreateButton (100, 100, 0, "$0 million", zero)

loop
    var j := Rand.Int (1, 5)
    var k : int
    if j = 1 then
        k := 100
    elsif j = 2 then
        k := 70
    elsif j = 3 then
        k := 50
    elsif j = 4 then
        k := 30
    elsif j = 5 then
        k := 0
    end if

***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.

    loop
        exit when GUI.ProcessEvent
    end loop
    if guess = k then
        put "Same"
    elsif guess < k then
        put "Larger"
    elsif guess > k then
        put "Smaller"
    end if
end loop



Mod Edit: Remember to use syntax tags! Thanks :) [syntax="turing"]Code Here[/syntax]

-----------------------------------
The_Bean
Sat Jan 03, 2009 9:12 am

Re: Turning help with randint statement
-----------------------------------
Your problem is with the:

    loop
        exit when GUI.ProcessEvent
    end loop

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.

-----------------------------------
Tony
Sat Jan 03, 2009 3:30 pm

RE:Turning help with randint statement
-----------------------------------
You seem to have trouble understanding how loops work. Here's a tutorial -- 
loop
   exit when GUI.ProcessEvent
end loop

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.

-----------------------------------
CHE.R.RY_YUI
Sat Jan 03, 2009 4:32 pm

Re: Turning help with randint statement
-----------------------------------
Your problem is with the:

    loop
        exit when GUI.ProcessEvent
    end loop

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.

-----------------------------------
CHE.R.RY_YUI
Sat Jan 03, 2009 4:37 pm

Re: RE:Turning help with randint statement
-----------------------------------
You seem to have trouble understanding how loops work. Here's a tutorial -- 
loop
   exit when GUI.ProcessEvent
end loop

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 (^__^)

-----------------------------------
Tony
Sat Jan 03, 2009 4:48 pm

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:

    if guess = k then
        put "Same"
    elsif guess < k then
        put "Larger"
    elsif guess > k then
        put "Smaller"
    end if
    exit when GUI.ProcessEvent
end loop


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.

-----------------------------------
CHE.R.RY_YUI
Sat Jan 03, 2009 5:24 pm

Re: 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:

    if guess = k then
        put "Same"
    elsif guess < k then
        put "Larger"
    elsif guess > k then
        put "Smaller"
    end if
    exit when GUI.ProcessEvent
end loop


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 @_@

-----------------------------------
Tony
Sat Jan 03, 2009 5:36 pm

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
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.

-----------------------------------
CHE.R.RY_YUI
Sat Jan 03, 2009 7:15 pm

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.

-----------------------------------
Tony
Sat Jan 03, 2009 7:24 pm

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.

-----------------------------------
CHE.R.RY_YUI
Mon Jan 05, 2009 5:41 am

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.

-----------------------------------
Tony
Mon Jan 05, 2009 11:04 am

RE:Turning help with randint statement
-----------------------------------
one possible solution is to not use Quit, then you wouldn't need to reset it.

-----------------------------------
CHE.R.RY_YUI
Tue Jan 06, 2009 10:13 pm

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"?

-----------------------------------
Tony
Tue Jan 06, 2009 10:37 pm

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.
