Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Turning help with randint statement
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
CHE.R.RY_YUI




PostPosted: 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!
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: 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.
code:
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




PostPosted: 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!!
syntax_error




PostPosted: 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.
Insectoid




PostPosted: 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.
CHE.R.RY_YUI




PostPosted: 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...
Turing:
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

Turing:
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"
Turing:
        if k = guess then
            put "same"
        else
            put "different"
        end if

thanks!

Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="turing"]Code Here[/syntax]
DarkRider




PostPosted: 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:

Turing:
loop
    exit when GUI.ProcessEvent ()
end loop


Also make sure you declare guess in your code as well:

Turing:
var guess : int
CHE.R.RY_YUI




PostPosted: 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.

Turing:
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.

Turing:
    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 Smile
code:
[syntax="turing"]Code Here[/syntax]
Sponsor
Sponsor
Sponsor
sponsor
The_Bean




PostPosted: Sat Jan 03, 2009 9:12 am   Post subject: Re: Turning help with randint statement

Your problem is with the:
Turing:

    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




PostPosted: 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

code:

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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
CHE.R.RY_YUI




PostPosted: 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:
Turing:

    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




PostPosted: 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

code:

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




PostPosted: 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:
Turing:

    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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
CHE.R.RY_YUI




PostPosted: 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:
Turing:

    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




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 21 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: