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

Username:   Password: 
 RegisterRegister   
 help with Rand.Int if statements
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic
Author Message
sci23




PostPosted: Sat Nov 24, 2007 9:46 pm   Post subject: help with Rand.Int if statements

i need help on my program...the if statements for the Rand.Int don't work(the numbers get screwed up)....so how can i make them work

code:

colour(blue)
put "Welcome to the game Dice"
colour(black)
procedure die1
drawbox(300,50,200,150,7)
drawfilloval(250,100,10,10,7)
end die1

procedure die2
drawbox(300,50,200,150,7)
drawfilloval(285,135,10,10,7)
drawfilloval(215,65,10,10,7)
end die2


procedure die3
drawbox(300,50,200,150,7)
drawfilloval(285,135,10,10,7)
drawfilloval(215,65,10,10,7)
drawfilloval(250,100,10,10,7)
end die3

procedure die4
drawbox(300,50,200,150,7)
drawfilloval(285,135,10,10,7)
drawfilloval(215,65,10,10,7)
drawfilloval(285,65,10,10,7)
drawfilloval(215,135,10,10,7)
end die4

procedure die5
drawbox(300,50,200,150,7)
drawfilloval(285,135,10,10,7)
drawfilloval(215,65,10,10,7)
drawfilloval(285,65,10,10,7)
drawfilloval(215,135,10,10,7)
drawfilloval(250,100,10,10,7)
end die5

procedure die6
drawbox(300,50,200,150,7)
drawfilloval(285,135,10,10,7)
drawfilloval(215,65,10,10,7)
drawfilloval(285,65,10,10,7)
drawfilloval(215,135,10,10,7)
drawfilloval(215,100,10,10,7)
drawfilloval(285,100,10,10,7)
end die6

procedure d1
drawbox(480,50,380,150,7)
drawfilloval(430,100,10,10,7)
end d1

procedure d2
drawbox(480,50,380,150,7)
drawfilloval(465,135,10,10,7)
drawfilloval(395,65,10,10,7)
end d2

procedure d3
drawbox(480,50,380,150,7)
drawfilloval(465,135,10,10,7)
drawfilloval(395,65,10,10,7)
drawfilloval(430,100,10,10,7)
end d3

procedure d4
drawbox(480,50,380,150,7)
drawfilloval(465,135,10,10,7)
drawfilloval(395,65,10,10,7)
drawfilloval(465,65,10,10,7)
drawfilloval(395,135,10,10,7)
end d4

procedure d5
drawbox(480,50,380,150,7)
drawfilloval(465,135,10,10,7)
drawfilloval(395,65,10,10,7)
drawfilloval(465,65,10,10,7)
drawfilloval(395,135,10,10,7)
drawfilloval(430,100,10,10,7)
end d5

procedure d6
drawbox(480,50,380,150,7)
drawfilloval(465,135,10,10,7)
drawfilloval(395,65,10,10,7)
drawfilloval(465,65,10,10,7)
drawfilloval(395,135,10,10,7)
drawfilloval(395,100,10,10,7)
drawfilloval(465,100,10,10,7)
end d6

var die : array 1 .. 6 of procedure dice
var p:int

die (1) := die1
die (2) := die2
die (3) := die3
die (4) := die4
die (5) := die5
die (6) := die6
die (Rand.Int (1, 6))

if die(Rand.Int (1, 6))=die1 then
p:=1
elsif
die(Rand.Int (1, 6))=die2 then
p:=2
elsif
die(Rand.Int (1, 6))=die3 then
p:=3
elsif
die(Rand.Int (1, 6))=die4 then
p:=4
elsif
die(Rand.Int (1, 6))= die5 then
p:=5
elsif
die(Rand.Int (1, 6))=die6 then
p:=6
end if
put p

var d : array 1..6 of procedure dice
var c:int
d(1):= d1
d(2):= d2
d(3):= d3
d(4):= d4
d(5):= d5
d(6):= d6
d (Rand.Int (1, 6))

if d(Rand.Int (1, 6))=d1 then
c:=1
elsif
d(Rand.Int (1, 6))=d2 then
c:=2
elsif
d(Rand.Int (1, 6))=d3 then
c:=3
elsif
d(Rand.Int (1, 6))=d4 then
c:=4
elsif
d(Rand.Int (1, 6))=d5 then
c:=5
elsif
d(Rand.Int (1, 6))=d6 then
c:=6
end if
put c
 
Sponsor
Sponsor
Sponsor
sponsor
Saad




PostPosted: Sat Nov 24, 2007 10:03 pm   Post subject: RE:help with Rand.Int if statements

2 things:
1: You could specify the location of the error
2: When using variables like c, it helps to tell in comments what it is.

But anyway, looking through the code I think the error is in the if statements. Everytime you goto an if-statement a new random number is generated, you should probably store it in a variable
 
HeavenAgain




PostPosted: Sat Nov 24, 2007 10:13 pm   Post subject: RE:help with Rand.Int if statements

loop
var myRandomNumber : int := Rand.Int(1,6)
put "you rolled ",myRandomNumber
end loop

you dont need an array, and you dont have to make a variable for 1 to 6, one variable to keep track of the random is enough
and the if statement is not necessary
 
Wolf_Destiny




PostPosted: Sun Nov 25, 2007 11:00 pm   Post subject: Re: RE:help with Rand.Int if statements

Saad @ Sat Nov 24, 2007 10:03 pm wrote:
But anyway, looking through the code I think the error is in the if statements. Everytime you goto an if-statement a new random number is generated, you should probably store it in a variable

That is in fact the exact error in your program. Now I do agree with HeavenAgain that you don't need an array, however you should probably get it working and discover the problem before optimizing your code. So in order to fix your problem, before the line "die(Rand.Int(1,6))" add a variable and define its value as Rand.Int(1,6). Then replace each instance of die(Rand.Int(1,6)) with die(YOUR_VAR_NAME_HERE)

Doing the same with d you'll get a bit of code much like the following which runs without error.
Turing:
colour(blue)
put "Welcome to the game Dice"
colour(black)
procedure die1
drawbox(300,50,200,150,7)
drawfilloval(250,100,10,10,7)
end die1

procedure die2
drawbox(300,50,200,150,7)
drawfilloval(285,135,10,10,7)
drawfilloval(215,65,10,10,7)
end die2


procedure die3
drawbox(300,50,200,150,7)
drawfilloval(285,135,10,10,7)
drawfilloval(215,65,10,10,7)
drawfilloval(250,100,10,10,7)
end die3

procedure die4
drawbox(300,50,200,150,7)
drawfilloval(285,135,10,10,7)
drawfilloval(215,65,10,10,7)
drawfilloval(285,65,10,10,7)
drawfilloval(215,135,10,10,7)
end die4

procedure die5
drawbox(300,50,200,150,7)
drawfilloval(285,135,10,10,7)
drawfilloval(215,65,10,10,7)
drawfilloval(285,65,10,10,7)
drawfilloval(215,135,10,10,7)
drawfilloval(250,100,10,10,7)
end die5

procedure die6
drawbox(300,50,200,150,7)
drawfilloval(285,135,10,10,7)
drawfilloval(215,65,10,10,7)
drawfilloval(285,65,10,10,7)
drawfilloval(215,135,10,10,7)
drawfilloval(215,100,10,10,7)
drawfilloval(285,100,10,10,7)
end die6

procedure d1
drawbox(480,50,380,150,7)
drawfilloval(430,100,10,10,7)
end d1

procedure d2
drawbox(480,50,380,150,7)
drawfilloval(465,135,10,10,7)
drawfilloval(395,65,10,10,7)
end d2

procedure d3
drawbox(480,50,380,150,7)
drawfilloval(465,135,10,10,7)
drawfilloval(395,65,10,10,7)
drawfilloval(430,100,10,10,7)
end d3

procedure d4
drawbox(480,50,380,150,7)
drawfilloval(465,135,10,10,7)
drawfilloval(395,65,10,10,7)
drawfilloval(465,65,10,10,7)
drawfilloval(395,135,10,10,7)
end d4

procedure d5
drawbox(480,50,380,150,7)
drawfilloval(465,135,10,10,7)
drawfilloval(395,65,10,10,7)
drawfilloval(465,65,10,10,7)
drawfilloval(395,135,10,10,7)
drawfilloval(430,100,10,10,7)
end d5

procedure d6
drawbox(480,50,380,150,7)
drawfilloval(465,135,10,10,7)
drawfilloval(395,65,10,10,7)
drawfilloval(465,65,10,10,7)
drawfilloval(395,135,10,10,7)
drawfilloval(395,100,10,10,7)
drawfilloval(465,100,10,10,7)
end d6

var die : array 1 .. 6 of procedure dice
var p:int

die (1) := die1
die (2) := die2
die (3) := die3
die (4) := die4
die (5) := die5
die (6) := die6

var ROLLED := Rand.Int (1, 6)
die(ROLLED)

if die(ROLLED)=die1 then
p:=1
elsif
die(ROLLED)=die2 then
p:=2
elsif
die(ROLLED)=die3 then
p:=3
elsif
die(ROLLED)=die4 then
p:=4
elsif
die(ROLLED)= die5 then
p:=5
elsif
die(ROLLED)=die6 then
p:=6
end if
put p

var d : array 1..6 of procedure dice
var c:int
d(1):= d1
d(2):= d2
d(3):= d3
d(4):= d4
d(5):= d5
d(6):= d6

var ROLLED2 := Rand.Int (1, 6)
d (ROLLED2)

if d(ROLLED2)=d1 then
c:=1
elsif
d(ROLLED2)=d2 then
c:=2
elsif
d(ROLLED2)=d3 then
c:=3
elsif
d(ROLLED2)=d4 then
c:=4
elsif
d(ROLLED2)=d5 then
c:=5
elsif
d(ROLLED2)=d6 then
c:=6
end if
put c

(Variable names that I used were ROLLED and ROLLED2 to make the changes more obvious)

Hope that helps!
 
Tony




PostPosted: Sun Nov 25, 2007 11:37 pm   Post subject: RE:help with Rand.Int if statements

It should probably be noted that
Turing:

var ROLLED := Rand.Int (1, 6)
die(ROLLED)

if die(ROLLED)=die1 then
p:=1
elsif
die(ROLLED)=die2 then
p:=2
elsif
die(ROLLED)=die3 then
p:=3
elsif
die(ROLLED)=die4 then
p:=4
elsif
die(ROLLED)= die5 then
p:=5
elsif
die(ROLLED)=die6 then
p:=6
end if
put p


is equivalent to

Turing:

var ROLLED := Rand.Int (1, 6)
die(ROLLED)

p := ROLLED

put p
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 pageAdd this topic to your bookmarks (CTRL-D) View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: