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

Username:   Password: 
 RegisterRegister   
 Conway's Game of Life
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nathan4102




PostPosted: Mon Jun 03, 2013 1:14 pm   Post subject: Conway's Game of Life

I'm trying to make Conway's game of life, since I have nothing else to do in ICS, but I'm having a problem. For some reason, as soon as I hit go, everything dies, no matter what seed I enter. I've been trying to fix it for a couple hours now, and I've made no progress. Could someone point me in the right direction? Thanks.

Turing:
%Nathaniel Woodthorpe
%May 31, 2013
%Life.t
%Recreate Conway's Game of Life

var x, y, button, num : int := 0
var font1 : int := Font.New ("Arial:15")

type blocks :
    record
        x, y : int
        On : boolean
    end record

var display : array 1 .. 100, 1 .. 100 of blocks
var buffer : array 1 .. 100, 1 .. 100 of blocks

procedure CheckSurrounding %Proc creates/kills cells based on surroundings
    for i : 1 .. 100
        for ii : 1 .. 100
            for j : max (i - 1, 1) .. min (i + 1, 100)
                for k : max (ii - 1, 1) .. min (ii + 1, 100)
                    if i not= j and ii not= k then
                        if display(j, k).On = true then
                            num += 1
                        end if
                    end if
                    if num < 2 then
                        buffer(i, ii).On := false
                    elsif num = 3 and display(i, ii).On = false then
                        buffer(i, ii).On := true
                    elsif num < 4 and display(i, ii).On = true then
                        buffer(i, ii).On := true
                    elsif num > 3 then
                        buffer(i, ii).On := false
                    end if
                    num := 0
                end for
            end for
        end for
    end for
end CheckSurrounding

setscreen ("graphics:510;550, offscreenonly")

for i : 1 .. 100 %Initialise properties
    for ii : 1 .. 100
        display (i, ii).x := i * 5
        display (i, ii).y := ii * 5
        drawbox (display (i, ii).x, display (i, ii).y, display (i, ii).x + 5, display (i, ii).y + 5, 29)
        display (i, ii).On := false
        buffer (i, ii).On := false
    end for
end for

drawfillbox (30, 515, 130, 540, grey) %Reset button
drawbox (30, 515, 130, 540, black)
Font.Draw ("Reset", 55, 520, font1, black)

drawfillbox (370, 515, 470, 540, grey) %Go button
drawbox (370, 515, 470, 540, black)
Font.Draw ("Go!", 400, 520, font1, black)

loop
    mousewhere (x, y, button)
    for i : 1 .. 100
        for ii : 1 .. 100
            if x >= display (i, ii).x and x <= display (i, ii).x + 5 and y > display (i, ii).y and y < display (i, ii).y + 5 and button = 1 then
                display (i, ii).On := true
            end if

            if display (i, ii).On = true then
                drawfillbox (display (i, ii).x, display (i, ii).y, display (i, ii).x + 5, display (i, ii).y + 5, black)
            end if
        end for
    end for

    if x > 30 and x < 130 and y > 515 and y < 540 and button = 1 then
        cls

        drawfillbox (370, 515, 470, 540, grey)
        drawbox (370, 515, 470, 540, black)
        Font.Draw ("Go!", 400, 520, font1, black)

        drawfillbox (30, 515, 130, 540, grey)
        drawbox (30, 515, 130, 540, black)
        Font.Draw ("Reset", 55, 520, font1, black)

        for i : 1 .. 100
            for ii : 1 .. 100
                buffer (i, ii).On := false
                display (i, ii).On := false
                drawbox (display (i, ii).x, display (i, ii).y, display (i, ii).x + 5, display (i, ii).y + 5, 29)
            end for
        end for
    end if

    View.Update

    exit when x > 370 and x < 470 and y > 515 and y < 540 and button = 1
end loop

loop
    cls
    CheckSurrounding
    for i : 1 .. 100
        for ii : 1 .. 100
            display(i, ii).On := buffer(i, ii).On
            drawbox (display (i, ii).x, display (i, ii).y, display (i, ii).x + 5, display (i, ii).y + 5, 29)
            if display(i, ii).On = true then
                drawfillbox (display (i, ii).x, display (i, ii).y, display (i, ii).x + 5, display (i, ii).y + 5, black)
            end if
        end for
    end for
    View.Update
    delay(10)

end loop
Sponsor
Sponsor
Sponsor
sponsor
Panphobia




PostPosted: Mon Jun 03, 2013 2:29 pm   Post subject: RE:Conway\'s Game of Life

Gives you code in all different languages, check what you did compared to their code.
[url]http://rosettacode.org/wiki/Conway's_Game_of_Life#Java[/url]
Raknarg




PostPosted: Mon Jun 03, 2013 7:30 pm   Post subject: Re: Conway's Game of Life

Ok. First of all, I've noticed this in some of your other programs: There's no need to make an x and y record if the position of your stuff has a pattern. In this case, you can just use i and ii to keep track of the x and y's. You do it when you initialize your x and y's anyways.

Also, I've been at it for a while too. I cannot for the life of me find the issue
Raknarg




PostPosted: Mon Jun 03, 2013 7:48 pm   Post subject: RE:Conway\'s Game of Life

Well, I've stripped you code a lot and turns out I'm a complete idiot. I changed a piece of code and messed up, but I have it working now and I have absolutely no idea why yours wasn't working.

Some things I tried first: The only thing in your for k and for j statements are seeing how many pieces are surrounding. All that other stuff that changes the state of a piece should be outside that.

genius edit: Nathan look at your for j and k statements, namely the order of min and max...
Dreadnought




PostPosted: Mon Jun 03, 2013 8:27 pm   Post subject: Re: Conway's Game of Life

Raknarg wrote:

genius edit Nathan look at your for j and k statements, namely the order of min and max...

Assuming the intent is to look only at adjacent squares I don't see anything wrong with those...

I think that you probably want the if num (<,>,=) # then statement outside the for k for j loops, (right now you will always set display(i, ii).On to false since you set num to 0 every time you iterate through your for j loop.

Something like
Turing:
procedure CheckSurrounding %Proc creates/kills cells based on surroundings
    for i : 1 .. 100
        for ii : 1 .. 100
            for j : max (i - 1, 1) .. min (i + 1, 100)
                for k : max (ii - 1, 1) .. min (ii + 1, 100)
                    if i not= j and ii not= k then
                        if display (j, k).On = true then
                            num += 1
                        end if
                    end if

                end for
            end for
            if num < 2 then
                buffer (i, ii).On := false
            elsif num = 3 and display (i, ii).On = false then
                buffer (i, ii).On := true
            elsif num < 4 and display (i, ii).On = true then
                buffer (i, ii).On := true
            elsif num > 3 then
                buffer (i, ii).On := false
            end if
            num := 0
        end for
    end for
end CheckSurrounding

However, you need to change your logic, the way it's setup now almost anything dies off (diamonds are one of the few things that don't disappear).
Raknarg




PostPosted: Mon Jun 03, 2013 8:34 pm   Post subject: RE:Conway\'s Game of Life

Oh right, they;re the right direction... I spent too much time on this already
Zren




PostPosted: Mon Jun 03, 2013 9:00 pm   Post subject: RE:Conway\'s Game of Life

Suggestion:
Instead of two game loops, representing the setup game state and the 'active' game state, merge the two into a single loop. Then have a toggle button to play/pause advancing the game's update. Thus you can draw while the game runs. Making it more interactive once the thing has started.

Tip:
With if booleanVariable = true then, the = true is unnecessary.

Eg: if booleanVariable then

You can also write if booleanVariable = false then with the negation operator as if not booleanVariable then.
Nathan4102




PostPosted: Tue Jun 04, 2013 8:45 am   Post subject: Re: RE:Conway\'s Game of Life

Panphobia @ Mon Jun 03, 2013 3:29 pm wrote:
Gives you code in all different languages, check what you did compared to their code.
[url]http://rosettacode.org/wiki/Conway's_Game_of_Life#Java[/url]


I took a look at this, but because I didn't know any of the languages on the page, it was tough to pick up differences.

Quote:

I think that you probably want the if num (<,>,=) # then statement outside the for k for j loops, (right now you will always set display(i, ii).On to false since you set num to 0 every time you iterate through your for j loop.

However, you need to change your logic, the way it's setup now almost anything dies off (diamonds are one of the few things that don't disappear).


Thanks! It's finally doing something! But now, as you said, only diamonds (And some other odd shapes) are surviving, which is weird, because as far as I can tell, the in the procedure is fine.

For example, I draw a block. The "CheckSurroundings" procedure checks these:

1. If adjacent < 2, cell is now dead
2. If adjacent = 3 and cell is dead, cell is now living
3. If adjacent < 4 and cell is living, cell is still living
4. If adjacent > 3, cell is now dead

So for every cell in the block, 1 is false, 2 is false, but 3 is true. So I'm not too sure why blocks are dying out. I'll look at it a bit more, thanks for the help so far.

Zren @ Mon Jun 03, 2013 10:00 pm wrote:
Suggestion:
Instead of two game loops, representing the setup game state and the 'active' game state, merge the two into a single loop. Then have a toggle button to play/pause advancing the game's update. Thus you can draw while the game runs. Making it more interactive once the thing has started.

Tip:
With if booleanVariable = true then, the = true is unnecessary.

Eg: if booleanVariable then

You can also write if booleanVariable = false then with the negation operator as if not booleanVariable then.


I'll definitely add that first suggestion after I get the actual mechanics working, thanks for the suggestion. And I didn't know you could use "if boolean then" in Turing, thanks!

Edit:
code:
if j not= i and ii not= k then


That was the problem, "and" needed to be "or". Works perfectly now, thanks guys!
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Tue Jun 04, 2013 11:07 am   Post subject: RE:Conway\'s Game of Life

No, what he's saying is that diamonds shouldn't disappear (I think). The interesting thing about this simulator is that you end up with patterns that loop, die off or resolve into shapes that remain the same permanently. Such as diamonds (I think). Glad you fixed it xD
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  [ 9 Posts ]
Jump to:   


Style:  
Search: