Dice game
Author |
Message |
bishop
|
Posted: Mon Apr 20, 2009 6:38 pm Post subject: Dice game |
|
|
Hello there!
I have to create a dice game where there are three dice. The user enters the number of times the three dice are rolled and what number he/she wants to bet on.
Here's what I have
Turing: | randomize
class Dice
export rollDie, valueOfDie, drawDie,setValue
var number : int
procedure rollDie
randint (number, 1, 6)
end rollDie
procedure setValue (number: int)
value:=number
end setValue
function valueOfDie : int
result value
end valueOfDie
procedure drawDie (x1, y1, x2, y2 : int)
Draw.Box (x1, y1, x2, y2, 7)
case number of
label 1 :
Draw.FillOval ( (x1 + x2 ) div 2, (y1 + y2 ) div 2, 5, 5, 7)
label 2 :
Draw.FillOval (x1 + 10, y2 - 10, 5, 5, 7)
Draw.FillOval (x2 - 10, y1 + 10, 5, 5, 7)
label 3 :
Draw.FillOval (x1 + 10, y2 - 10, 5, 5, 7)
Draw.FillOval ( (x1 + x2 ) div 2, (y1 + y2 ) div 2, 5, 5, 7)
Draw.FillOval (x2 - 10, y1 + 10, 5, 5, 7)
label 4 :
Draw.FillOval (x1 + 10, y2 - 10, 5, 5, 7)
Draw.FillOval (x1 + 10, y1 + 10, 5, 5, 7)
Draw.FillOval (x2 - 10, y1 + 10, 5, 5, 7)
Draw.FillOval (x2 - 10, y2 - 10, 5, 5, 7)
label 5 :
Draw.FillOval (x1 + 10, y2 - 10, 5, 5, 7)
Draw.FillOval (x1 + 10, y1 + 10, 5, 5, 7)
Draw.FillOval (x2 - 10, y1 + 10, 5, 5, 7)
Draw.FillOval (x2 - 10, y2 - 10, 5, 5, 7)
Draw.FillOval ( (x1 + x2 ) div 2, (y1 + y2 ) div 2, 5, 5, 7)
label 6 :
Draw.FillOval (x1 + 10, y2 - 10, 5, 5, 7)
Draw.FillOval (x1 + 10, y1 + 10, 5, 5, 7)
Draw.FillOval (x1 + 10, (y1 + y2 ) div 2, 5, 5, 7)
Draw.FillOval (x2 - 10, y1 + 10, 5, 5, 7)
Draw.FillOval (x2 - 10, y2 - 10, 5, 5, 7)
Draw.FillOval (x2 - 10, (y1 + y2 ) div 2, 5, 5, 7)
end case
end drawDie
end Dice
class Game
import Dice
export rollDice, drawDice
var dieOne, dieTwo, dieThree : ^Dice
new dieOne
new dieTwo
new dieThree
procedure Sort2 (var a, b : int)
var c : int
if a > b then
c := a
a := b
b := c
end if
end Sort2
procedure Sort
var number1, number2, number3 : int
number1 := dieOne -> valueOfDie
number2 := dieTwo -> valueOfDie
number3 := dieThree -> valueOfDie
Sort2 (number1, number2 )
Sort2 (number2, number3 )
Sort2 (number1, number2 )
dieOne->setValue (number1 )
dieTwo->setValue (number2 )
dieThree->setValue (number3 )
end Sort
procedure rollDice
dieOne -> rollDie
dieTwo -> rollDie
dieThree -> rollDie
Sort
end rollDice
procedure drawDice
dieOne -> drawDie (20, 20, 120, 120)
dieTwo -> drawDie (140, 20, 240, 120)
dieThree -> drawDie (260, 20, 360, 120)
end drawDice
end Game
var RollDice, displayDice: ^Game
new RollDice
new DrawDice
RollDice -> rollDice
DrawDice -> drawDice |
For some reason when it's about to draw each die,ie, when it reaches the case statement in the drawDie in the Dice class
it says "Variable has no value"
Right now I'm not concerned about the score just rolling and displaying the dice
Any help would be appreciated. Thanks.
Mod Edit: Remember to use syntax tags! Thanks code: | [syntax="turing"]Code Here[/syntax] |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Dusk Eagle
|
Posted: Mon Apr 20, 2009 6:53 pm Post subject: Re: Dice game |
|
|
When submitting code, please use [syntax="turing"]%code here[/syntax]. This makes it much easier to read.
The first thing I notice is that you forgot to define the class variable "value". Once I did that, I got an error at the line "new DrawDice" : "'DrawDice has not been declared." So, you're getting more errors with your code than the one you mentioned. But anyway, the problem you are having is that your variable "number" is not equal to anything. You must call your setValue procedure to assign a value to number before you can call your draw procedure. |
|
|
|
|
|
bishop
|
Posted: Mon Apr 20, 2009 7:33 pm Post subject: Re: Dice game |
|
|
Sorry about that. I made changes and forgot to do it throught the code
Anyway now it should show the error the i mentioned earlier.
It shows an error message "Variable has no value" when it gets to the drawDice in class Dice
Turing: |
randomize
class Dice
export rollDie, valueOfDie, drawDie, setValue
var number : int
procedure rollDie
randint (number, 1, 6)
end rollDie
procedure setValue (newnumber : int)
number := newnumber
end setValue
function valueOfDie : int
result number
end valueOfDie
procedure drawDie (x1, y1, x2, y2 : int)
Draw.Box (x1, y1, x2, y2, 7)
case number of
label 1 :
Draw.FillOval ( (x1 + x2 ) div 2, (y1 + y2 ) div 2, 5, 5, 7)
label 2 :
Draw.FillOval (x1 + 10, y2 - 10, 5, 5, 7)
Draw.FillOval (x2 - 10, y1 + 10, 5, 5, 7)
label 3 :
Draw.FillOval (x1 + 10, y2 - 10, 5, 5, 7)
Draw.FillOval ( (x1 + x2 ) div 2, (y1 + y2 ) div 2, 5, 5, 7)
Draw.FillOval (x2 - 10, y1 + 10, 5, 5, 7)
label 4 :
Draw.FillOval (x1 + 10, y2 - 10, 5, 5, 7)
Draw.FillOval (x1 + 10, y1 + 10, 5, 5, 7)
Draw.FillOval (x2 - 10, y1 + 10, 5, 5, 7)
Draw.FillOval (x2 - 10, y2 - 10, 5, 5, 7)
label 5 :
Draw.FillOval (x1 + 10, y2 - 10, 5, 5, 7)
Draw.FillOval (x1 + 10, y1 + 10, 5, 5, 7)
Draw.FillOval (x2 - 10, y1 + 10, 5, 5, 7)
Draw.FillOval (x2 - 10, y2 - 10, 5, 5, 7)
Draw.FillOval ( (x1 + x2 ) div 2, (y1 + y2 ) div 2, 5, 5, 7)
label 6 :
Draw.FillOval (x1 + 10, y2 - 10, 5, 5, 7)
Draw.FillOval (x1 + 10, y1 + 10, 5, 5, 7)
Draw.FillOval (x1 + 10, (y1 + y2 ) div 2, 5, 5, 7)
Draw.FillOval (x2 - 10, y1 + 10, 5, 5, 7)
Draw.FillOval (x2 - 10, y2 - 10, 5, 5, 7)
Draw.FillOval (x2 - 10, (y1 + y2 ) div 2, 5, 5, 7)
end case
end drawDie
end Dice
class Game
import Dice
export rollDice, drawDice
var dieOne, dieTwo, dieThree : ^Dice
new dieOne
new dieTwo
new dieThree
procedure Sort2 (var a, b : int)
var c : int
if a > b then
c := a
a := b
b := c
end if
end Sort2
procedure Sort
var number1, number2, number3 : int
number1 := dieOne -> valueOfDie
number2 := dieTwo -> valueOfDie
number3 := dieThree -> valueOfDie
Sort2 (number1, number2 )
Sort2 (number2, number3 )
Sort2 (number1, number2 )
dieOne -> setValue (number1 )
dieTwo -> setValue (number2 )
dieThree -> setValue (number3 )
end Sort
procedure rollDice
dieOne -> rollDie
dieTwo -> rollDie
dieThree -> rollDie
Sort
end rollDice
procedure drawDice
dieOne -> drawDie (20, 20, 120, 120)
dieTwo -> drawDie (140, 20, 240, 120)
dieThree -> drawDie (260, 20, 360, 120)
end drawDice
end Game
%Main program
var RollDice, displayDice : ^Game
new RollDice
new displayDice
RollDice -> rollDice
displayDice -> drawDice |
Mod Edit: Your code goes where it says code here |
|
|
|
|
|
Dusk Eagle
|
Posted: Mon Apr 20, 2009 8:02 pm Post subject: Re: Dice game |
|
|
Turing: |
RollDice -> rollDice
displayDice -> drawDice
|
Here's your problem. Your object RollDice has a value in it, named number, that has been initialized through calling the rollDice procedure. However, an entirely different object, displayDice, does not have value "number" initialized to anything. Think of it this way:
code: |
Bob is an instance of the class person.
Jill is an instance of the class person.
Bob's age is 15.
If Jill's age is > 50, print "Happy Birthday"
|
The problem above is that Jill's age is completely unknown; we have only set Bob's age. This is the same thing you have done in your program. You must use the same instance of an object throughout your code, as RollDice's values will not equal displayDice's values, just as Bob's age will not equal Jill's age. |
|
|
|
|
|
bishop
|
Posted: Tue Apr 21, 2009 4:49 pm Post subject: RE:Dice game |
|
|
lol I thought I was still in the Game class while typing the main. You were absolutely right, it works now.
Thank you so much for your help!! |
|
|
|
|
|
|
|