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

Username:   Password: 
 RegisterRegister   
 array element all have the same value
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bishop




PostPosted: Sat Jun 13, 2009 6:13 pm   Post subject: array element all have the same value

What is it you are trying to achieve?
I'm trying to make a domino game. Every time a domino is added to the right of the chain, it will become the last element of the table array and when a domino is added to the left
of the chain it will become the first element of the table array.


What is the problem you are having?
When I try to add a domino piece to the left of the chain, all the following elements that I add are the same as the previous element added to the left (in the table array only, the numbers are displayed properly. I'm wondering if there is a problem with my insertAt method inside the Table Class or if I'm inserting the element at the wrong location

Describe what you have tried to solve this problem
Here is my code



Turing:


View.Set ("graphics:960;560")
class Domino
    export setValue, Value, X, Y, displayValue, setCoordinates, Orientation

    var value : int := 0
    var x, y : int := 0
    var orientation : string (10) := "a"

    procedure setValue (dominoValue : int)
        value := dominoValue
    end setValue

    function Value : int
        result value
    end Value

    function X : int
        result x
    end X

    function Y : int
        result y
    end Y

    function Orientation : string (10)
        result orientation
    end Orientation

    procedure setCoordinates (a, b : int, c : string (10))
        x := a
        y := b
        orientation := c
    end setCoordinates

    procedure verticalOutline
        Draw.FillBox (x, y, x + 30, y + 60, white)
        Draw.Box (x, y, x + 30, y + 60, black)
        Draw.Line (x + 1, y + 30, x + 29, y + 30, gray)
    end verticalOutline

    procedure horizontalOutline
        Draw.FillBox (x, y, x + 60, y + 30, white)
        Draw.Box (x, y, x + 60, y + 30, black)
        Draw.Line (x + 30, y + 1, x + 30, y + 29, gray)
    end horizontalOutline

    procedure drawInside (x1, y1, number : int)
        var space : int := 5
        var size : int := 3
        var Colour : int := 7

        case number of
            label 0 :
                Draw.FillOval ( (x1 + x1 + 30) div 2, (y1 + y1 + 30)
                    div 2, size, size, white)
            label 1 :
                Draw.FillOval ( (x1 + x1 + 30) div 2, (y1 + y1 + 30)
                    div 2, size, size, Colour)

            label 2 :
                Draw.FillOval (x1 + space, y1 + 30 - space, size,
                    size, Colour)
                Draw.FillOval (x1 + 30 - space, y1 + space, size,
                    size, Colour)

            label 3 :
                Draw.FillOval (x1 + space, y1 + 30 - space, size,
                    size,
                    Colour)
                Draw.FillOval ( (x1 + x1 + 30) div 2, (y1 + y1 + 30)
                    div 2, size, size, Colour)
                Draw.FillOval (x1 + 30 - space, y1 + space, size,
                    size, Colour)

            label 4 :
                Draw.FillOval (x1 + space, y1 + 30 - space, size,
                    size, Colour)
                Draw.FillOval (x1 + space, y1 + space, size, size,
                    Colour)
                Draw.FillOval (x1 + 30 - space, y1 + space, size,
                    size, Colour)
                Draw.FillOval (x1 + 30 - space, y1 + 30 - space,
                    size, size, Colour)

            label 5 :
                Draw.FillOval (x1 + space, y1 + 30 - space, size, size,
                    Colour)
                Draw.FillOval (x1 + space, y1 + space, size, size,
                    Colour)
                Draw.FillOval ( (x1 + x1 + 30) div 2, (y1 + y1 + 30)
                    div 2, size, size, Colour)
                Draw.FillOval (x1 + 30 - space, y1 + space, size,
                    size, Colour)
                Draw.FillOval (x1 + 30 - space, y1 + 30 - space,
                    size, size, Colour)

            label 6 :
                Draw.FillOval (x1 + space, y1 + 30 - space, size,
                    size, Colour)
                Draw.FillOval (x1 + space, y1 + space, size, size,
                    Colour)
                Draw.FillOval (x1 + space, (y1 + y1 + 30) div 2,
                    size, size, Colour)
                Draw.FillOval (x1 + 30 - space, y1 + space, size,
                    size, Colour)
                Draw.FillOval (x1 + 30 - space, y1 + 30 - space,
                    size, size, Colour)
                Draw.FillOval (x1 + 30 - space, (y1 + y1 + 30)
                    div 2, size, size, Colour)
        end case
    end drawInside

    procedure drawDots (turnAround : boolean)
        var number : int := 0
        var space, a, b : int
        a := x
        b := y

        if orientation = "vertical" then
            for i : 1 .. 2
                if turnAround then
                    if i = 1 then
                        number := value mod 10
                    else
                        number := value div 10
                    end if
                else
                    if i = 1 then
                        number := value div 10
                    else
                        number := value mod 10
                    end if
                end if
                drawInside (a, b, number)
                b := b + 30
            end for

        else
            for i : 1 .. 2
                if turnAround then
                    if i = 1 then
                        number := value mod 10
                    else
                        number := value div 10
                    end if
                else
                    if i = 1 then
                        number := value div 10
                    else
                        number := value mod 10
                    end if
                end if
                drawInside (a, b, number)
                a := a + 30
            end for
        end if
    end drawDots


    procedure displayValue (faceup : boolean, turnAround : boolean)
        if orientation = "vertical" then
            verticalOutline
        else
            horizontalOutline
        end if

        if faceup then
            drawDots (turnAround)
        end if
    end displayValue

end Domino

class Hand
    import Domino
    export valueOfDomino, sizeOfHand, addDomino, displayHand, findValue,
        dropDomino

    var hand : array 1 .. 7 of ^Domino
    var size : int := 0

    for i : 1 .. 7
        new hand (i)
    end for

    function valueOfDomino (number : int) : int
        result hand (number) -> Value
    end valueOfDomino

    function sizeOfHand : int
        result size
    end sizeOfHand

    procedure addDomino (number : int)
        if size + 1 <= 7 then
            hand (size + 1) -> setValue (number)
            size := size + 1
        end if
    end addDomino

    procedure displayHand (x, y : int, orientation : string (10),
            faceup : boolean)
        var turnAround : boolean := false
        var x1, y1 : int
        x1 := x
        y1 := y
        for i : 1 .. size
            hand (i) -> setCoordinates (x1, y1, orientation)
            hand (i) -> displayValue (faceup, turnAround)
            if orientation = "horizontal" then
                x1 := x1 + 70
            else
                y1 := y1 + 70
            end if
        end for
    end displayHand

    function findValue (number : int) : int
        var i : int := 1
        loop
            exit when hand (i) -> Value = number or i > size
            i := i + 1
        end loop

        result i
    end findValue

    procedure dropDomino (number : int)
        var location : int := findValue (number)

        for i : location .. size
            if i < size then
                hand (i) -> setValue (hand (i + 1) -> Value)
            end if
        end for
        size := size - 1
        if hand (location) -> Orientation = "vertical" then
            Draw.FillBox (hand (location) -> X, hand (location) -> Y,
                hand (location) -> X + 30, hand (location) -> Y + 60, white)
        else
            Draw.FillBox (hand (location) -> X, hand (location) -> Y,
                hand (location) -> X + 60, hand (location) -> Y + 30, white)
        end if
    end dropDomino

        procedure sortHand
        var i : int := 0
        var temp : ^Domino

        for pass : 2 .. size
            temp := hand (pass)
            i := pass - 1
            loop
                exit when i = 0 or temp -> Value >= hand (i) -> Value
                hand (i + 1) := hand (i)
                i := i - 1
            end loop
            hand (i + 1) := temp
        end for
    end sortHand

end Hand

class Table
    import Domino
    export leftTable, rightTable, putTable, addToTable, drawTable

    var table : array 1 .. 28 of ^Domino
    var size : int := 0
    var left, right : ^Domino
    new left
    new right

    for i : 1 .. 28
        new table (i)
    end for

    function leftTable : int
        result left -> Value div 10
    end leftTable

    function rightTable : int
        result right -> Value mod 10
    end rightTable

    procedure insertAt (location, value : int)
        for decreasing i : size + 1 .. location + 1
            table (i) := table (i - 1)
        end for
        table (location) -> setValue (value)
        size := size + 1
    end insertAt

    procedure drawTable
        Draw.FillBox (60, 60, 900, 500, green)
    end drawTable

    procedure putTable (dominopiece : ^Domino, orientation : string (10),
            faceup, turnAround : boolean)
        dominopiece -> displayValue (faceup, turnAround)
    end putTable

    procedure coordinatesVertical (dominopiece : ^Domino, var x, y : int,
            var orientation : string (10), var turnAround : boolean, isFine
            : boolean)

        var x2, y2 : int := 0
        x2 := dominopiece -> X
        y2 := dominopiece -> Y

        if whatdotcolor (x2 + 60, y2 - 30) = green and
                whatdotcolor (x2 + 90, y2 - 30) = green and
                whatdotcolor (x2 + 61, y2 + 30) = green and
                whatdotcolor (x2 + 90, y2 + 30) = green then
            x := x2 + 60
            y := y2 - 30
            orientation := "vertical"
            turnAround := true

        elsif whatdotcolor (x2 + 30, y2 - 60) = green and
                whatdotcolor (x2 + 60, y2 - 60) = green and
                whatdotcolor (x2 + 30, y2 - 1) = green and
                whatdotcolor (x2 + 60, y2 - 1) = green then
            x := x2 + 30
            y := y2 - 60
            orientation := "vertical"
            turnAround := true

        elsif whatdotcolor (x2 + 61, y2) = green and
                whatdotcolor (x2 + 90, y2) = green and
                whatdotcolor (x2 + 60, y2 + 60) = green and
                whatdotcolor (x2 + 90, y2 + 60) = green then
            x := x2 + 60
            y := y2
            orientation := "vertical"

        elsif whatdotcolor (x2 + 30, y2 + 31) = green and
                whatdotcolor (x2 + 60, y2 + 31) = green and
                whatdotcolor (x2 + 30, y2 + 90) = green and
                whatdotcolor (x2 + 60, y2 + 90) = green then
            x := x2 + 30
            y := y2 + 30
            orientation := "vertical"
        end if

    end coordinatesVertical

    procedure coordinatesVerticalLeft (dominopiece : ^Domino, var x, y : int,
            var orientation : string (10), var turnAround : boolean,
            isFine : boolean)

        var x2, y2 : int := 0
        x2 := dominopiece -> X
        y2 := dominopiece -> Y

        if whatdotcolor (x2 - 1, y2) = green and
                whatdotcolor (x2 - 30, y2) = green and
                whatdotcolor (x2 - 1, y2 + 60) = green and
                whatdotcolor (x2 - 30, y2 + 60) = green then
            x := x2 - 30
            y := y2
            orientation := "vertical"
            turnAround := true

        elsif whatdotcolor (x2, y2 + 31) = green and
                whatdotcolor (x2 + 30, y2 + 31) = green and
                whatdotcolor (x2, y2 + 90) = green and
                whatdotcolor (x2 + 30, y2 + 90) = green then
            x := x2
            y := y2 + 30
            orientation := "vertical"
            turnAround := true

        elsif whatdotcolor (x2 - 30, y2 + 30) = green and
                whatdotcolor (x2 - 1, y2 + 30) = green and
                whatdotcolor (x2 - 30, y2 - 30) = green and
                whatdotcolor (x2 - 1, y2 - 30) = green then
            x := x2 - 30
            y := y2 - 30
            orientation := "vertical"

        elsif whatdotcolor (x2, y2 - 1) = green and
                whatdotcolor (x2 + 30, y2 - 1) = green and
                whatdotcolor (x2, y2 - 60) = green and
                whatdotcolor (x2 + 30, y2 + 60) = green then
            x := x2
            y := y2 - 60
            orientation := "vertical"

        end if

    end coordinatesVerticalLeft

    procedure coordinatesRight (dominopiece : ^Domino,
            var x, y : int, var orientation : string (10), var turnAround :
            boolean, isFine : boolean, direction : char)

        var x2, y2 : int
        var a : string (10)

        x2 := dominopiece -> X
        y2 := dominopiece -> Y
        a := dominopiece -> Orientation

        %%%domino towards right
        if a = "horizontal" and whatdotcolor (x2 + 61, y2) = green and
                whatdotcolor (x2 + 120, y2) = green and
                whatdotcolor (x2 + 61, y2 + 30) = green and
                whatdotcolor (x2 + 120, y2 + 30) = green then
            x := x2 + 60
            y := y2
            orientation := "horizontal"

            %%%bottom
        elsif a = "vertical" and whatdotcolor (x2, y2 - 1) = green and
                whatdotcolor (x2 + 60, y2) = green and
                whatdotcolor (x2, y2 - 30) = green and
                whatdotcolor (x2 + 60, y2 - 30) = green then
            x := x2
            y := y2 - 30
            orientation := "horizontal"

            %%%top
        elsif a = "vertical" and whatdotcolor (x2, y2 + 61) = green and
                whatdotcolor (x2 + 60, y2 + 61) = green and
                whatdotcolor (x2, y2 + 90) = green and
                whatdotcolor (x2 + 60, y2 + 90) = green then
            x := x2
            y := y2 + 60
            orientation := "horizontal"

            %%close to bottom
        elsif a = "vertical" and whatdotcolor (x2 + 31, y2) = green and
                whatdotcolor (x2 + 90, y2) = green and
                whatdotcolor (x2 + 31, y2 + 30) = green and
                whatdotcolor (x2 + 90, y2 + 30) = green then
            x := x2 + 30
            y := y2
            orientation := "horizontal"

            %%%close to top
        elsif a = "vertical" and whatdotcolor (x2 + 31, y2 + 30) = green and
                whatdotcolor (x2 + 90, y2 + 30) = green and
                whatdotcolor (x2 + 31, y2 + 60) = green and
                whatdotcolor (x2 + 90, y2 + 60) = green then
            x := x2 + 30
            y := y2 + 30
            orientation := "horizontal"
        end if

        if direction = chr (203) then
            turnAround := true
        end if

    end coordinatesRight


    procedure coordinatesLeft (dominopiece : ^Domino,
            var x, y : int, var orientation : string (10), var turnAround :
            boolean, isFine : boolean, direction : char)

        var x2, y2 : int
        var a : string (10)

        x2 := dominopiece -> X
        y2 := dominopiece -> Y
        a := dominopiece -> Orientation

        %%domino towards left
        if a = "horizontal" and whatdotcolor (x2 - 1, y2) = green and
                whatdotcolor (x2 - 60, y2) = green and
                whatdotcolor (x2 - 1, y2 + 30) = green and
                whatdotcolor (x2 - 60, y2 + 30) = green then
            x := x2 - 60
            y := y2
            orientation := "horizontal"

            %%%bottom
        elsif a = "vertical" and whatdotcolor (x2 + 30, y2 - 1) = green and
                whatdotcolor (x2 + 30, y2 - 30) = green and
                whatdotcolor (x2 - 30, y2 - 30) = green and
                whatdotcolor (x2 - 30, y2) = green then
            x := x2 - 30
            y := y2 - 30
            orientation := "horizontal"
            turnAround := true

            %%close to bottom
        elsif a = "vertical" and whatdotcolor (x2 - 1, y2) = green and
                whatdotcolor (x2 - 1, y2 + 30) = green and
                whatdotcolor (x2 - 60, y2) = green and
                whatdotcolor (x2 - 60, y2 + 30) = green then
            x := x2 - 60
            y := y2
            orientation := "horizontal"
            turnAround := true

            %%%close to top
        elsif a = "vertical" and whatdotcolor (x2 - 1, y2 + 30) = green and
                whatdotcolor (x2 - 1, y2 + 60) = green and
                whatdotcolor (x2 - 60, y2 + 30) = green and
                whatdotcolor (x2 - 60, y2 + 60) = green then
            x := x2 - 60
            y := y2 + 30
            orientation := "horizontal"
            turnAround := true

            %%%top
        elsif whatdotcolor (x2 + 30, y2 + 61) = green and
                whatdotcolor (x2 + 30, y2 + 90) = green and
                whatdotcolor (x2 - 30, y2 + 90) = green and
                whatdotcolor (x2 - 30, y2 + 60) = green then
            x := x2 - 30
            y := y2 + 60
            orientation := "horizontal"
            turnAround := false

        end if
        if direction = chr (205) then
            turnAround := true
        end if

    end coordinatesLeft

    procedure addToTable (number : int, direction : char)
        var faceup : boolean := true
        var orientation : string (10) := "horizontal"
        var turnAround : boolean := false
        var x, y : int := 0
        var Xprevious, Yprevious : int := 0
        var isFine : boolean := true

        if number = 66 then
            insertAt (1, number)
            x := 450
            y := 250
            table (1) -> setCoordinates (x, y, orientation)
            table (1) -> displayValue (faceup, turnAround)
            left := table (1)
            right := table (1)

            % add domino to the left
        elsif direction = chr (203) then
            if number mod 10 = leftTable then
                insertAt (1, number)
            else
                insertAt (1, ( (number mod 10) * 10 + number div 10))
            end if

            Xprevious := left -> X
            Yprevious := left -> Y

            if (whatdotcolor (Xprevious - 1, Yprevious) = green and
                    whatdotcolor (Xprevious - 60, Yprevious) = green) or
                    (left -> Orientation = "vertical" and
                    whatdotcolor (Xprevious - 31, Yprevious) = green) then

                coordinatesLeft (left, x, y,
                    orientation, turnAround, isFine, direction)

            elsif whatdotcolor (Xprevious - 1, Yprevious) = green and
                    whatdotcolor (Xprevious - 60, Yprevious) not= green and
                    left -> Orientation = "horizontal" then
                coordinatesVerticalLeft (left, x, y,
                    orientation, turnAround, isFine)

            else

                coordinatesRight (left, x, y,
                    orientation, turnAround, isFine, direction)

            end if

            table (1) -> setCoordinates (x, y, orientation)
            putTable (table (1), orientation, faceup, turnAround)
            left := table (1)

            %add domino to the right
        elsif direction = chr (205) then
            if number div 10 = rightTable then
                insertAt (size + 1, number)
                isFine := true
            else
                insertAt (size + 1, (number mod 10) * 10 + number div 10)
                isFine := false
            end if

            Xprevious := right -> X
            Yprevious := right -> Y

            if (whatdotcolor (Xprevious + 61, Yprevious) = green and
                    whatdotcolor (Xprevious + 120, Yprevious) = green) or
                    (right -> Orientation = "vertical" and
                    whatdotcolor (Xprevious + 31, Yprevious) = green) then

                coordinatesRight (right, x, y,
                    orientation, turnAround, isFine, direction)

            elsif (whatdotcolor (Xprevious - 61, Yprevious) = green and
                    whatdotcolor (Xprevious - 120, Yprevious) = green) or
                    (right -> Orientation = "vertical" and
                    whatdotcolor (Xprevious - 31, Yprevious) = green) then

                coordinatesLeft (right, x, y,
                    orientation, turnAround, isFine, direction)

            else
                coordinatesVertical (right, x, y,
                    orientation, turnAround, isFine)
            end if

            table (size) -> setCoordinates (x, y, orientation)
            putTable (table (size), orientation, faceup,
                turnAround)

            right := table (size)

        end if
    end addToTable

end Table


%temporal Main to test the current classes
var hand : ^Hand
var table : ^Table
new hand
new table

var orientation : string := "vertical"
var faceup : boolean := true
var number : int := 0

for i : 1 .. 5

    loop
        randint (number, 0, 66)
        exit when number mod 10 <= 6 and number div 10 <= 6
    end loop

    hand -> addDomino (number)
end for

hand -> addDomino (23)
hand -> dropDomino (23)
hand -> addDomino (33)
hand -> dropDomino (33)

hand -> displayHand (10, 10, orientation, faceup)

table -> drawTable
table -> addToTable (66, chr (203))
table -> addToTable (36, chr (203))
table -> addToTable (33, chr (203))
table -> addToTable (53, chr (203))
table -> addToTable (54, chr (203))
table -> addToTable (34, chr (203))
table -> addToTable (23, chr (203))
table -> addToTable (22, chr (203))
table -> addToTable (21, chr (203))
table -> addToTable (16, chr (205))



Any help would be appreciated. Thnx in advance
Sponsor
Sponsor
Sponsor
sponsor
TheGuardian001




PostPosted: Sat Jun 13, 2009 8:14 pm   Post subject: Re: array element all have the same value

Well, currently your program doesn't actually run a game, it just draws the table, so I can't actually see how it runs, but I do see one flaw in the insertAt procedure.

Turing:

for decreasing i : size + 1 .. location + 1
            table (i) := table (i - 1)
end for


Now, I could be wrong here (I didn't look into how "size" or "location" are actually set), but your program is currently doing something like this (I think)

code:

%Original Array(example)
12, 32, 15, 18, 90
%first run through
12, 12, 15, 18, 90
%second run through
12, 12, 12, 18, 90
%etc


each time it runs through the for loop, it will set the number to the value to it's immediate left. But wait, in our last repetition, we changed that number to a new value, so the original value was destroyed! what you need to do is to have a TEMP variable that will hold the value of the number you are resetting, so you don't get rid of that value until after you set the next one.
Example:

Turing:

var Original : array 1 .. 6 of int
var TEMP : int
var TEMP2 : int

%set base values
for i : 1 .. 6
    Original(i) := i
    put Original(i)..
end for
put "\n"
%lets put a zero in front and shift all the other numbers right one, bumping the 6.
TEMP := 0
for i : 1 .. 6
    TEMP2 := Original(i) %preserve our value
    Original(i) := TEMP %set the new value
    TEMP := TEMP2 %set the next value to be used to our preserved value.
end for

for i : 1 .. 6
    put Original(i)..
end for
bishop




PostPosted: Sat Jun 13, 2009 10:37 pm   Post subject: Re: array element all have the same value

Thank you for taking the time to read this.
Yes right now I haven't finished the game but the positioning of the dominos is what I am having trouble with.
The way I insertAt works (or at least the way I think it is working) is that it starts replacing values from the end of the array + 1, so the position next to the logical size being used.
It works its way down until the location next to the location (location +1) that I want want to replace with a new value. Then, when it exits the for loop it, that is when the wanted change happens and the logical size is increased by 1.
bishop




PostPosted: Sun Jun 14, 2009 3:17 pm   Post subject: Re: array element all have the same value

I just noticed something. The parameter isFine in the addToTable method is useless in case anyone was wondering about it
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  [ 4 Posts ]
Jump to:   


Style:  
Search: