Welcome to Dream.In.Code
Become a Java Expert!

Join 150,072 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,791 people online right now. Registration is fast and FREE... Join Now!




Game of "Life"

 
Reply to this topicStart new topic

Game of "Life", bubbles

morphous
12 Jun, 2008 - 11:02 PM
Post #1

New D.I.C Head
*

Joined: 12 Jun, 2008
Posts: 7

Hello guys. I gotta make this game called game of "Life".
I've tried but i'm kinda stuck because of a semantic mistake. I'll show you my code of the prototype!

CODE

/* Write an animation that lets the computer play the game of ``Life.''
The game goes as follows: the user specifies the size of game board,
a matrix, and also gives the starting positions of where some pebbles(``cells'') live.
Every second, the computer updates the board, creating and removing cells,
according to the following rules:
    - an empty board position that is surrounded by exactly three cells gets
      a cell placed on it. (The new cell ``comes to life.'')
    - a board position occupied by a cell retains the cell if the position was surrounded
      by exactly 2 other cells. (Otherwise the cell disappears---``dies''---due to
     ``loneliness'' or ``overcrowding.'')
Here is an example of two seconds of the animation on a 5-by-5 board, where an X denotes a cell and . denotes an empty space:
.X.XX     ...XX     ...XX
X...X     ....X     ....X
XXX..  => X.X..  => ...X.
..X.X     X.X..     ..XX.
X...X     ...X.     .....
*/
import java.awt.*;
import javax.swing.*;
public class LifeFinal
{    char[][] c = new char[5][5];
    char[][] m = new char[5][5];
    public LifeFinal()
    {     c[0][0] = '.'; c[0][1] = 'x'; c[0][2] = '.'; c[0][3] = 'x'; c[0][4] = 'x';  
        c[1][0] = 'x'; c[1][1] = '.'; c[1][2] = '.'; c[1][3] = '.'; c[1][4] = 'x';
        c[2][0] = '.'; c[2][1] = '.'; c[2][2] = 'x'; c[2][3] = '.'; c[2][4] = '.';
        c[3][0] = '.'; c[3][1] = '.'; c[3][2] = 'x'; c[3][3] = '.'; c[3][4] = 'x';
        c[4][0] = 'x'; c[4][1] = '.'; c[4][2] = '.'; c[4][3] = '.'; c[4][4] = '.';
     }
    
    public void save()
    {    m = c; }
    public void print()
    {    for(int i = 0; i < m.length; i++)
        {    for(int j = 0; j < m[0].length; j++)
            {    System.out.print(m[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }
    public void check_it()
    {    c = m;
        int three = 0;
        int two = 0;
        for(int i = 0; i < c.length; i++)
        {    for(int j = 0; j < c[0].length; j++)
            {    three = 0;
                two = 0;
                if(i>0 && j>0 && i<c.length-1 && j<c[0].length-1)
                {    three = 0;
                    two = 0;
                    for(int p = i-1; p <= i+1; p++)
                    {    for(int q = j-1; q <= j+1; q++)
                        {    if(!((p == i) && (q == j)))
                            {    
                                if(c[i][j] == '.' && c[p][q] == 'x') { three+=1; }
                            else if(c[i][j] == 'x' && c[p][q] == 'x') { two+=1; }
                                
                            }    
                        }
                    }
                    if(c[i][j] == '.' && three == 3) { m[i][j] = 'x'; }
                    else if(c[i][j] == 'x' && two != 2) { m[i][j] = '.'; }
                }
            }
        }
    }        
    public static void main(String[] args)
    {    LifeFinal f = new LifeFinal();
        f.save();
        f.print();
        f.check_it();
        f.print();
    }
}


I just checked the inside terms of the matrix, just to not get out of bounds cause I checked them by using algorithm i+1, i-1 etc... but it's not changing how it should the inner terms of the matrix(inner terms: from c[1][1] to c[3][3]).
The result after one check looks like this:
. x . x x
x . . . x
. . x . .
. . x . x
x . . . .

. x . x x
x x . . x
. . x . .
. x x x x
x . . . .
Maybe I'm not saving the matrix right. If anyone could analyze it i would appreciate! Thnx

** Edit ** code.gif
User is offlineProfile CardPM
+Quote Post

gl3thr0
RE: Game Of "Life"
12 Jun, 2008 - 11:03 PM
Post #2

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 209



Thanked: 3 times
My Contributions
dude code tags please!
User is online!Profile CardPM
+Quote Post

neotrumatrix
RE: Game Of "Life"
12 Jun, 2008 - 11:10 PM
Post #3

New D.I.C Head
Group Icon

Joined: 6 Oct, 2005
Posts: 49



Thanked: 3 times
Dream Kudos: 50
My Contributions
QUOTE(gl3thr0 @ 13 Jun, 2008 - 12:03 AM) *

dude code tags please!

^+1 as is your post isn't making much sense crazy.gif
User is offlineProfile CardPM
+Quote Post

mensahero
RE: Game Of "Life"
12 Jun, 2008 - 11:17 PM
Post #4

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
QUOTE(neotrumatrix @ 13 Jun, 2008 - 12:10 AM) *

QUOTE(gl3thr0 @ 13 Jun, 2008 - 12:03 AM) *

dude code tags please!

^+1 as is your post isn't making much sense crazy.gif




what he meant to say is.. use code tags.. like this.. if you don't know how.. the instruction is right in front of you..

example:
CODE
  
blink.gif
User is offlineProfile CardPM
+Quote Post

morphous
RE: Game Of "Life"
13 Jun, 2008 - 12:12 AM
Post #5

New D.I.C Head
*

Joined: 12 Jun, 2008
Posts: 7

QUOTE(neotrumatrix @ 13 Jun, 2008 - 12:10 AM) *

QUOTE(gl3thr0 @ 13 Jun, 2008 - 12:03 AM) *

dude code tags please!

^+1 as is your post isn't making much sense crazy.gif

sorry guys...i'm new and the power got down here...that's my state:D
User is offlineProfile CardPM
+Quote Post

morphous
RE: Game Of "Life"
13 Jun, 2008 - 03:42 PM
Post #6

New D.I.C Head
*

Joined: 12 Jun, 2008
Posts: 7

guys? blink.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: Game Of "Life"
13 Jun, 2008 - 06:54 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(morphous @ 13 Jun, 2008 - 04:42 PM) *

guys? blink.gif


Haven't look in depth but I guess what your problem is

I guess you are intending by doing:

c = m;
and
m = c;

to make a copy of your array so that you can check the "old" state of a cell before defining what is new state is ?

c = m or m = c does not copy an array into the other.... it just make c and m to point to the same array

Actually by doing c = m once both arrays point to the same area in memory
you can do:
c = m; or m = c:
a million times... these staments are completly useless c = m and m = c so doing c = m or m = c does nothing

To save your array you will have to manually copy each cell:

CODE

for(int i = 0; i < c.length; i++) {
    for(int j = 0; j < c[i].length; j++)
        m[i][j] = c[i][j];
}

User is offlineProfile CardPM
+Quote Post

morphous
RE: Game Of "Life"
13 Jun, 2008 - 09:18 PM
Post #8

New D.I.C Head
*

Joined: 12 Jun, 2008
Posts: 7

Thank you man. That was the exact mistake. I kinda almost notice it was working with the initial array.

Respect
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:03PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month