Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Passing 2D arrays + Game of Life (if u ever heard of it)

843789Nov 19 2009 — edited Dec 3 2009
Classes:
GameOfLifeTester
GameOfLife

I got a 2d array in one class and I'm trying to retrieve it in another using the constructor. Having problems, this is my code:
// all this is within the GameofLife class

int[][] generation1;
    int[][] generation2;
    int[][] initialfill;
    int row;
    int col;
    int n;
    
    public GameOfLife(int rows, int columns, int generations, int[][] arrays) {
    
        row = rows;
        col = columns;
        n = generations;
        generation1 = new int[rows][columns];
        initialfill = new int[][] arrays   // this line is the problem, i don't know how to pass the 2d array to this array...
        // also tried:   initialfill = new arrays    and      initialfill = int[][] arrays
    }
Question 2:
As you may have guessed by now, I've gotta make this 'game of life' for my assignment and have another question if you don't mind. We're supposed to ask the user to input how many rows, columns etc. they need and then we ask them which spots on the board should initially be filled. Now I have to keep track of the spots which are filled, and I could do that with a 2D array, but then I only need specific spots so how would I do that?

Example,
The User enters:

number of occupied spots: 2
row: 5
column: 6
row: 3
column: 2

Now I have to save these two values [(5,6) & (3,2)] because they have to be passed later on to the other 2D array in the Gameoflife class so the 'generation1' 2D array in it knows what spots are filled on the board initially....I used this code within the Tester class up till now:
import java.util.Scanner;

/**
 *
 * @author me
 */
public class GameOfLifeTester {

    public static void main(String[] args) {


        Scanner boardinfo = new Scanner(System.in);
        System.out.println("Enter the maximum number of generations");
        int numgen = boardinfo.nextInt();
        System.out.println("Enter the character displayed in occupied spots");
        char filltype = boardinfo.next().charAt(0);
        System.out.println("Enter the max. number of rows");
        int maxr = boardinfo.nextInt();
        System.out.println("Enter the max. number of columns");
        int maxc = boardinfo.nextInt();
        System.out.println("Enter the number of occupied spots");
        int occupied = boardinfo.nextInt();
        
        
        int[][] genoccupied = new int[maxr][maxc];  // so now its maxrows, maxcolumns
        
        
        for (int i=0; i<(occupied);i++) {   // occupied is how many spots are filled
            System.out.println("enter row and column numbers!");
            int row = boardinfo.nextInt();
            int col = boardinfo.nextInt();
            genoccupied[row][col] = genoccupied[row][col];  
/// This line above is the problem, would this work if I set the values this way? 
// As in, I'd have to loop through this array later and find out what values were null and if 
// they were, I would ignore those ones. And whichever spots are not null within this 
// array would equal the values //of the 'generation1' (class Gameoflife) array. 
// Am I right?? does this make any sense to you...
        }
                 
        
    }

}
Any help is appreciated, I'm a beginner to Java!

Edited by: A_A_M_I_R on Nov 19, 2009 8:10 PM

Edited by: A_A_M_I_R on Nov 19, 2009 8:11 PM

Edited by: A_A_M_I_R on Nov 19, 2009 8:12 PM

Edited by: A_A_M_I_R on Nov 19, 2009 8:13 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 31 2009
Added on Nov 19 2009
55 comments
1,144 views