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

Username:   Password: 
 RegisterRegister   
 Dice program
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Leafs Fan




PostPosted: Thu May 29, 2003 5:59 pm   Post subject: Dice program

i have been doing this program for my comp sci class.. and i can not figure why it does not work. bascially all you do is roll three, random numbers and then you decide if its a winner,loser or if you get points.
(you can only have one catergorie that u get. e.g. u can win and get points)
win= 4-5-6 or 3 of a kind or a pair and a six, mean you win
lose= 1-2-3, pair and 1
point= pair and a 2,3,4,5, the number between 2-5 is the point value

i.e. 4,5,6 wins, 1,5,5 loses, 3,6,6 get 3 points

now you have to continue to roll three dice until the total of all the wins, poitns and loses is 1000. but for some reason i got like 1000 points, i have tried to fix for a while so some help would be appreicated


code:

var diceValue : int

%This is a Dice class
%Fields:
%   x,y - Coordinates of where the dice should be drawn
%   diceValue-  The random value that the dice has
%Methods:
%   Roll- sets diceValue to a random number
%   draw- Draws the dice based on the coordinates given

class Dice

    import diceValue
    export Roll, Value, draw
    var x, y : int := 0



    function Roll : int

        result Rand.Int (1, 6)
    end Roll

    function Value (value : int) : int
        result Roll
    end Value


    procedure draw (diceValue : int)

        Draw.FillBox (x, y, x + 80, y + 80, 255)

        case diceValue of
            label 1 :

                Draw.FillOval (40 + x, 40, 5, 5, 48)
            label 2 :
                Draw.FillOval (9 + x, 67, 5, 5, 48)
                Draw.FillOval (70 + x, 14, 5, 5, 48)

            label 3 :
                Draw.FillOval (9 + x, 8, 5, 5, 48)
                Draw.FillOval (70 + x, 70, 5, 5, 48)
                Draw.FillOval (40 + x, 40, 5, 5, 48)
            label 4 :
                Draw.FillOval (9 + x, 8, 5, 5, 48)
                Draw.FillOval (70 + x, 70, 5, 5, 48)
                Draw.FillOval (9 + x, 70, 5, 5, 48)
                Draw.FillOval (70 + x, 7, 5, 5, 48)
            label 5 :
                Draw.FillOval (9 + x, 8, 5, 5, 48)
                Draw.FillOval (70 + x, 70, 5, 5, 48)
                Draw.FillOval (9 + x, 70, 5, 5, 48)
                Draw.FillOval (70 + x, 7, 5, 5, 48)
                Draw.FillOval (40 + x, 40, 5, 5, 48)
            label 6 :
                Draw.FillOval (9 + x, 8, 5, 5, 48)
                Draw.FillOval (70 + x, 70, 5, 5, 48)
                Draw.FillOval (9 + x, 70, 5, 5, 48)
                Draw.FillOval (70 + x, 7, 5, 5, 48)
                Draw.FillOval (9 + x, 40, 5, 5, 48)
                Draw.FillOval (70 + x, 40, 5, 5, 48)






            label :

        end case
        x := x + 92
    end draw

end Dice
%This a Hand456 Class:
%Fields:
%   a,b,c   -The values for the three dice rolled
%   x,y     -Temporary values used to help sort the numbers
%   win     -States if you have won the game
%   point   -Stores the number of points that you have won while playing the game
%Methods:
%   Roll    -Rolls three dice and stores the values in a,b, and c
%   Sort2   -Sorts two given numbers from smallest to greatest
%   Sort    -Uses the Sort2 method to sort all three numbers
%   draw    -Draws the three dice, in order
%   Win     -Calculates if the hand that you rolled is a winner or not
%   Lose    -Calculates if the hand that you rolled is a loser or not
%   Point   -States the nnumber of points
class Hand456
    import Dice, diceValue
    export Roll, Sort, draw, Win, Lose, Point, point
    var a, b, c : int := 0
    var x : int
    var y : int
    var point := 0
    var winNum, loseNum : int := 0

    function Roll : int
        var dice : ^Dice
        var num : int
        new dice

        result dice -> Roll
    end Roll

    procedure Sort2 (num1, num2 : int)
        var temp : int
        x := num1
        y := num2
        if num1 > num2 then
            temp := num1
            x := y
            y := temp
        end if
    end Sort2

    procedure Sort
        Sort2 (a, b)
        a := x
        b := y
        Sort2 (b, c)
        b := x
        c := y
        Sort2 (a, c)
        a := x
        c := y
        Sort2 (a, b)
        a := x
        b := y
        Sort2 (b, c)
        b := x
        c := y
    end Sort

    procedure draw (a, b, c : int)
        var dice : ^Dice
        new dice
        dice -> draw (a)
        dice -> draw (b)
        dice -> draw (c)
    end draw
    function Win (a, b, c : int) : boolean
        if a = 4 and b = 5 and c = 6 then

            locate (22, 38)
            put "Wins"
            result true
        elsif a = b and b = c then

            locate (22, 38)
            put "Wins   "
            result true
        elsif c = 6 and a = b then

            locate (22, 38)
            put "Wins       "
            result true
        end if
        result false
    end Win
    function Lose (a, b, c : int) : boolean
        if a = 1 and b = 2 and c = 3 then

            result true
        elsif (a = 1 and b = c) or (b = 1 and a = c) or (c = 1 and b = a)
                then
            result true
        end if
        result false
    end Lose
    function Point (a, b, c : int) : int
        if (a = b and (c < 6 and c > 1)) then
            point := point + c

        elsif (b = c and (a < 6 and a > 1)) then
            point := point + a

        end if
        result point
    end Point
end Hand456

%MAIN
var dummy : int
var wins, loses, points : int := 0
var dice : ^Hand456
var pointNum : ^Hand456
new dice


loop
    var a, b, c : int

    a := dice -> Roll
    b := dice -> Roll
    c := dice -> Roll
    dice -> Sort
    dice -> draw (a, b, c)



    if dice -> Win (a, b, c) = true then

        wins := wins + 1

        locate (22, 38)
        put "Wins       "

    elsif dice -> Lose (a, b, c) = true then
        loses := loses + 1
        locate (22, 38)
        put "Lose           "

    elsif dice -> Point (a, b, c) > 0 then
        locate (22, 38)
        put "Points  =   ", points
        points := dice -> Point (a, b, c) + points

    else
        locate (22, 38)
        put "Nothing"

    end if
    exit when points + loses + wins = 10

    locate (1, 1)
    put "Wins   = ", wins
    put "Loses  = ", loses
    put "Points = ", points

    if (points + loses + wins) > 1000 then
        wins := 0
        loses := 0
        points := 0
    end if

    delay (1)
    cls

end loop

put "Congratulations "
put "You have ", wins, " Wins"
put "You have ", loses, " Loses"
put "You have ", points, " Points "
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Thu May 29, 2003 6:36 pm   Post subject: (No subject)

dude man, ur making this way too hard

code:

var dice : array 1 .. 3 of int
var wins, loses, temp, points, count := 0
var results : string
loop

    randint (dice (1), 1, 6)
    randint (dice (2), 1, 6)
    randint (dice (3), 1, 6)

    for i : 1 .. 2
        for j : i + 1 .. 3
            if dice (j) < dice (i) then
                temp := dice (j)
                dice (j) := dice (i)
                dice (i) := temp
            end if
        end for
    end for

    if dice (1) = dice (2) and dice (3) = 6 then
        put "win"
        wins += 1
    elsif dice (1) = dice (2) and dice (2) = dice (3) then
        put "win"
        wins += 1
    elsif dice (1) = 4 and dice (2) = 5 and dice (3) = 6 then
        put "win"
        wins += 1
    elsif dice (1) = 1 and dice (2) = 2 and dice (3) = 3 then
        put "loose"
        loses += 1
    elsif dice (1) = 1 and dice (2) = dice (3) then
        put "loose"
        loses += 1
    elsif dice (1) = dice (2) then
        points += dice (3)
        put dice (3), " points"
    elsif dice (2) = dice (3) then
        points += dice (1)
        put dice (1), " points"
    end if
    delay (10)
    exit when wins + loses + points = 1000
end loop
put wins
put loses
put points
Homer_simpson




PostPosted: Thu May 29, 2003 7:48 pm   Post subject: (No subject)

he aint making it hard... he's making it llook kool... it's like the difference betweeen gta 2 and gta 3 3D vs 2D
Andy




PostPosted: Thu May 29, 2003 8:00 pm   Post subject: (No subject)

there are still alot of useless lines tho
and what good is it if it doesn't work?
i'd rather have a working copy of GTA2 than a non-working copy of GTA3
and GTA2 is good man, what are u talking about...
Dark Mist




PostPosted: Thu May 29, 2003 8:10 pm   Post subject: (No subject)

u need to put a count in the main program that is till 1000
Dark Mist




PostPosted: Thu May 29, 2003 8:13 pm   Post subject: (No subject)

it sort of works when u say points + loses + wins > 1000
Dark Mist




PostPosted: Thu May 29, 2003 8:34 pm   Post subject: (No subject)

but again i am not sure
Andy




PostPosted: Thu May 29, 2003 8:42 pm   Post subject: (No subject)

dude, you know u can edit ur messages, instead of spamming other ppl's forums, just click edit on ur message
Dark Mist




PostPosted: Thu May 29, 2003 9:32 pm   Post subject: (No subject)

will remeber that next time
tum_twish




PostPosted: Fri May 30, 2003 10:28 am   Post subject: (No subject)

try putting 'randomize' at the beginning of the main program
ThunderChuncky




PostPosted: Fri May 30, 2003 2:18 pm   Post subject: (No subject)

Here's the program I used.
code:

var diceValue : int
var winTotal, loseTotal, pointTotal, totalPoints : int := 0
var w, z : int := 0
%---------------------------------------------------------DICE CLASS---------------------------------------------------------

class Dice
    import diceValue
    export Roll, Value, draw

    var x, y : int := 10

    procedure Roll
        randint (diceValue, 1, 6)
    end Roll

    function Value (value : int) : int
        Roll
    end Value

    procedure draw (diceValue : int)
        Draw.Box (x, y, x + 65, y + 65, black)
        case diceValue of
            label 1 :
                Draw.FillOval (x + 32, 42, 4, 4, black)
            label 2 :
                Draw.FillOval (x + 10, 20, 4, 4, black)
                Draw.FillOval (x + 55, 65, 4, 4, black)
            label 3 :
                Draw.FillOval (x + 10, 20, 4, 4, black)
                Draw.FillOval (x + 32, 42, 4, 4, black)
                Draw.FillOval (x + 55, 65, 4, 4, black)
            label 4 :
                Draw.FillOval (x + 10, 20, 4, 4, black)
                Draw.FillOval (x + 55, 20, 4, 4, black)
                Draw.FillOval (x + 55, 65, 4, 4, black)
                Draw.FillOval (x + 10, 65, 4, 4, black)
            label 5 :
                Draw.FillOval (x + 10, 20, 4, 4, black)
                Draw.FillOval (x + 55, 20, 4, 4, black)
                Draw.FillOval (x + 32, 42, 4, 4, black)
                Draw.FillOval (x + 55, 65, 4, 4, black)
                Draw.FillOval (x + 10, 65, 4, 4, black)
            label 6 :
                Draw.FillOval (x + 10, 20, 4, 4, black)
                Draw.FillOval (x + 55, 20, 4, 4, black)
                Draw.FillOval (x + 55, 42, 4, 4, black)
                Draw.FillOval (x + 55, 65, 4, 4, black)
                Draw.FillOval (x + 10, 65, 4, 4, black)
                Draw.FillOval (x + 10, 42, 4, 4, black)
        end case
        x := x + 75
    end draw
end Dice

%-------------------------------------------------------HAND456 CLASS--------------------------------------------------------

class Hand456
    import Dice, diceValue, winTotal, loseTotal, pointTotal, totalPoints, w,
        z
    export Roll, draw, Sort, Win, Lose, Point

    var a, b, c : int := 0

   procedure Roll
        var dice : ^Dice
        new dice
        dice -> Roll
        a := diceValue
        dice -> Roll
        b := diceValue
        dice -> Roll
        c := diceValue
    end Roll

    procedure Sort2 (number1, number2 : int)
        var temp : int
        w := number1
        z := number2
        if number1 > number2 then
            temp := w
            w := z
            z := temp
        end if
    end Sort2

    procedure Sort
        Sort2 (a, b)
        a := w
        b := z
        Sort2 (b, c)
        b := w
        c := z
        Sort2 (a, b)
        a := w
        b := z
        Sort2 (b, c)
        b := w
        c := z
        Sort2 (a, b)
        a := w
        b := z
        Sort2 (b, c)
        b := w
        c := z
    end Sort

    procedure draw
        var dice : ^Dice
        new dice

        dice -> draw (a)
        dice -> draw (b)
        dice -> draw (c)
    end draw

    procedure Win
        if a = 4 and b = 5 and c = 6 then
            locate (22, 35)
            put "You Win!"
            winTotal := winTotal + a + b + c
            totalPoints := totalPoints + a + b + c
        elsif a = b and b = c and c = a then
            locate (22, 35)
            put "You Win!"
            winTotal := winTotal + a + b + c
            totalPoints := totalPoints + a + b + c
        elsif a = 6 and b = c then
            locate (22, 35)
            put "You Win!"
            winTotal := winTotal + a + b + c
            totalPoints := totalPoints + a + b + c
        elsif a = b and c = 6 then
            locate (22, 35)
            put "You Win!"
            winTotal := winTotal + a + b + c
            totalPoints := totalPoints + a + b + c
        end if
    end Win

    procedure Lose
        if a = 1 and b = 2 and c = 3 then
            locate (22, 35)
            put "You Lose!"
            loseTotal := loseTotal + a + b + c
            totalPoints := totalPoints + a + b + c
        elsif a = 1 and b = c and not (a = 1 and b = 1 and c = 1) then
            locate (22, 35)
            put "You Lose!"
            loseTotal := loseTotal + a + b + c
            totalPoints := totalPoints + a + b + c
        elsif a = b and c = 1 and not (a = 1 and b = 1 and c = 1) then
            locate (22, 35)
            put "You Lose!"
            loseTotal := loseTotal + a + b + c
            totalPoints := totalPoints + a + b + c
        end if
    end Lose

    procedure Point
        if (a = 2 or a = 3 or a = 4 or a = 5) and b = c and not (a = b and b
                = c and c = a) then
            locate (22, 35)
            put "Point = ", a
            pointTotal := pointTotal + a
            totalPoints := totalPoints + a
        elsif a = b and (c = 2 or c = 3 or c = 4 or c = 5) and not (a = b
                and b = c and c = a) then
            locate (22, 35)
            put "Point = ", c
            pointTotal := pointTotal + c
            totalPoints := totalPoints + c
        end if
    end Point
end Hand456

%-------------------------------------------------------MAIN PROGRAM---------------------------------------------------------

var roll : string (1)

var dice : ^Hand456
new dice

loop
    dice -> Roll
    dice -> Sort
    dice -> draw
    locate (22, 35)
    put "Nothing"
    dice -> Win
    dice -> Lose
    dice -> Point
    delay (1000)
    exit when totalPoints >= 1000
    locate (10, 30)
    put "Roll again? (y/n)" ..
    getch (roll)
    exit when roll = "n" or roll = "N"
    cls
end loop

cls

loop
    exit when totalPoints >= 1000
    dice -> Roll
    dice -> Win
    dice -> Lose
    dice -> Point
end loop

cls

locate (10, 35)
put "Game Over"
locate (12, 36)
put "Results"
locate (13, 33)
put "Wins   = ", winTotal
locate (14, 33)
put "Loses  = ", loseTotal
locate (15, 33)
put "Points = ", pointTotal
vexd




PostPosted: Mon Nov 10, 2003 6:41 pm   Post subject: (No subject)

HOLLY SHIT!
U GOTO MY SCHOOL DONT U?

IM DOING THAT TODAY IN SCHOOL!


also for the others: the assignment was to make a class out of this

and O m G, dis is gunna save /\/\E HELL-A TiME! luv compsci.ca Razz
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 1  [ 12 Posts ]
Jump to:   


Style:  
Search: