Printing not as expected

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 653 Views - Last Post: 02 December 2011 - 05:53 PM Rate Topic: -----

#1 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 12-October 11

Printing not as expected

Posted 30 November 2011 - 02:51 PM

Basically, I've been working on this game of life project and got a little stuck. The first generation prints fine, but when I go to print my next generation prints out this continously: [[I@e0cc23. I've gone terribly wrong somewhere. Help would much be appreciated.



/**
 * Write a description of class Cell here.
 * 
 * @author Mark Goddard
 * @version 20/11/2011
 */
public class Cell
{
    // instance variables
    private final int ROW = 5;
    private final int COL = 4;
    int[][] currentGeneration = new int[ROW+1][COL+1];
    int[][] nextGeneration = new int[ROW+1][COL+1];
    int row = 0;
    int col = 0;
    
    /**
    * Constructor for objects of class Cell
    */
    public Cell()
    {
        // initialise instance variables
        
    }
    
    /**
     * Intialising/Setting generation 1
     * 
     * @param  None
     * @return  None  
     */
    public void generation1()
    {
        for (int row = 0; row < currentGeneration.length; row++)
            for(int col = 0; col < currentGeneration[row].length; col++)
            {
                {
                    currentGeneration[1][1] = 1;
                    currentGeneration[2][3] = 1;
                    currentGeneration[3][2] = 1;
                    currentGeneration[4][1] = 1;
                    currentGeneration[3][3] = 1;
                    currentGeneration[2][3] = 1;
                    currentGeneration[3][1] = 1;
                    currentGeneration[row][col] = 0;
                }
            }
            for (int row = 0; row < currentGeneration.length; row++)
            {
                for(int col = 0; col < currentGeneration[row].length; col++)
                {
                    System.out.print(currentGeneration[row][col]);
                }
                System.out.println();
        }
    }
    
    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    public int countNeighbour(int count) 
    {
        count = 0;
        if (currentGeneration[row-1][col] == 1)
        {
            count++;
        }
        if (currentGeneration[row][col-1] == 1)
        {
            count++;
        }
        if (currentGeneration[row-1][col-1] == 1)
        {
            count++;
        }
        if (currentGeneration[row-1][col+1] ==1)
        {
            count++;
        }
        if (currentGeneration[row+1][col] ==1)
        {
            count++;
        }
        if (currentGeneration[row+1][col+1] ==1)
        {
            count++;
        }
        if (currentGeneration[row][col+1] ==1)
        {
            count++;
        }
        if (currentGeneration[row-1][col+1] ==1)
        {
            count++;
        }
        return count;
    }
    
    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    public void rules(int count) 
    {
        for (int row = 1; row < currentGeneration.length - 1; row++)
        {
            for (int col = 1; col < currentGeneration.length - 1; col++) 
            {
                if(currentGeneration[row][col] == 1) 
                {              
                    // Any live cell with fewer than two live neighbours dies
                    if(countNeighbour(count) < 2) 
                    {
                        nextGeneration[row][col] = 0;
                    }
                    // Any live cell with more than three live neighbours dies
                    if(countNeighbour(count) > 3)  
                    {
                        nextGeneration[row][col] = 0;
                    }
                }
                else 
                {
                    // Any dead cell with exactly three live neighbours becomes a live cell
                    if(countNeighbour(count)  == 3)  
                    {
                        nextGeneration[row][col] = 1;
                    }
                } 
            }
        }
    }
    
    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    private void copyBoard() 
    {
        // copies all the nextBoard values to the currBoard values
        for (int row = 0; row < nextGeneration.length - 1; row++) 
        {
            for (int col = 0; col < nextGeneration.length - 1; col++) 
            {
                nextGeneration[row][col] = currentGeneration[row][col];
            }
        }
    }
    
    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    public void print()
    {
        for (int row = 0; row <= row; row++) 
        {
            for (int col = 0; col <= col; col++) 
            {
                System.out.println(nextGeneration);
            }
        }
    }    
}




/**
 * A class which presents the user with a menu from which 
 * they can choose and demonstrates the game of life.
 * @author Mark Goddard
 * @version 31/10/2011
 */
public class Menu
{
    // instance variables
    
    /**
     * Constructor for objects of class Menu
     */
    public Menu()
    {
        // initialise instance variables
        
    }

    /**
     * Method to display the menu
     * 
     * @param  None
     * @return  None      
     */
    public static void displayMenu()
    {
        // Printing the individual choices to the screen
        System.out.println("GAME OF LIFE");
        System.out.println("");
        System.out.println("Main Menu: ");
        System.out.println("1 - Load a Generation");
        System.out.println("2 - Save game");
        System.out.println("3 - Load saved game");
        System.out.println("0 - Exit");  
        
        // Class the menu method as it defines what happens for each number chosen by the user
        Menu();
    }
    
    /**
     * Method to define the menu choices
     * 
     * @param  None
     * @return  None  
     */
    public static void Menu()
    {
        // Creating a local variable
        int choice;
        
        // Printing a message to the user
        System.out.println("");
        System.out.print("Please enter either number 1, 2, 3 or 4 for a particular option: ");
        
        // Using Genio method to get choice
        choice = Genio.getInteger();
        
        // Code for defining the choices available
        do if (choice == 1)
        {
            Cell Menu = new Cell();
            Menu.generation1();
            Menu1();
        }
        else if (choice == 2)
        {
            
        }
        else if (choice == 3)
        {
            
        }
        else if (choice == 4)
        {   
            
        }
        else if (choice == 0)
        {
            System.out.println("Goodbye, program is closing");
        }
        else
        {   
            System.out.println("Command not recognised");
        }
        while (false);
    } 

    /**
    * Method to define the menu choices
    * 
    * @param  None
    * @return  None  
    */
   public static void Menu1()
   {
       // Creating a local variable
       int choice;
        
       // Printing a message to the user
       System.out.println("");
       System.out.println("Please 1 for the next generation: ");
        
       // Using Genio method to get choice
       choice = Genio.getInteger();
        
       // Code for defining the choices available
       do if (choice == 1)
       {
            Cell Menu = new Cell();
            Menu.print();  
       }
       else
       {   
           System.out.println("Command not recognised");
       }
       while (false);
   } 
}



Is This A Good Question/Topic? 0
  • +

Replies To: Printing not as expected

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Printing not as expected

Posted 30 November 2011 - 02:55 PM

You cannot print a whole array like that

System.out.println(nextGeneration);

you will have top loop row by row column by column as you did for currentGeneration in generation1()
Was This Post Helpful? 1
  • +
  • -

#3 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 12-October 11

Re: Printing not as expected

Posted 30 November 2011 - 03:25 PM

I've corrected it so it actually prints the array but not the right one. Am I not passing the correct parameters or something.
/**
 * Write a description of class Cell here.
 * 
 * @author Mark Goddard
 * @version 20/11/2011
 */
public class Cell
{
    // instance variables
    private final int ROW = 5;
    private final int COL = 4;
    int[][] currentGeneration = new int[ROW+1][COL+1];
    int[][] nextGeneration = new int[ROW+1][COL+1];
    int row = 0;
    int col = 0;
    
    /**
    * Constructor for objects of class Cell
    */
    public Cell()
    {
        // initialise instance variables
        
    }
    
    /**
     * Intialising/Setting generation 1
     * 
     * @param  None
     * @return  None  
     */
    public void generation1()
    {
        for (int row = 0; row < currentGeneration.length; row++)
            for(int col = 0; col < currentGeneration[row].length; col++)
            {
                {
                    currentGeneration[1][1] = 1;
                    currentGeneration[2][3] = 1;
                    currentGeneration[2][4] = 1;
                    currentGeneration[4][1] = 1;
                    currentGeneration[3][3] = 1;
                    currentGeneration[2][3] = 1;
                    currentGeneration[3][1] = 1;
                    currentGeneration[row][col] = 0;
                }
            }
            for (int row = 0; row < currentGeneration.length; row++)
            {
                for(int col = 0; col < currentGeneration[row].length; col++)
                {
                    System.out.print(currentGeneration[row][col]);
                }
                System.out.println();
        }
    }
    
    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    public int countNeighbour(int count) 
    {
        count = 0;
        if (currentGeneration[row-1][col] == 1)
        {
            count++;
        }
        if (currentGeneration[row][col-1] == 1)
        {
            count++;
        }
        if (currentGeneration[row-1][col-1] == 1)
        {
            count++;
        }
        if (currentGeneration[row-1][col+1] ==1)
        {
            count++;
        }
        if (currentGeneration[row+1][col] ==1)
        {
            count++;
        }
        if (currentGeneration[row+1][col+1] ==1)
        {
            count++;
        }
        if (currentGeneration[row][col+1] ==1)
        {
            count++;
        }
        if (currentGeneration[row-1][col+1] ==1)
        {
            count++;
        }
        return count;
    }
    
    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    public void rules(int count) 
    {
        for (int row = 1; row < currentGeneration.length - 1; row++)
        {
            for (int col = 1; col < currentGeneration.length - 1; col++) 
            {
                if(currentGeneration[row][col] == 1) 
                {              
                    // Any live cell with fewer than two live neighbours dies
                    if(countNeighbour(count) < 2) 
                    {
                        nextGeneration[row][col] = 0;
                    }
                    // Any live cell with more than three live neighbours dies
                    if(countNeighbour(count) > 3)  
                    {
                        nextGeneration[row][col] = 0;
                    }
                }
                else 
                {
                    // Any dead cell with exactly three live neighbours becomes a live cell
                    if(countNeighbour(count)  == 3)  
                    {
                        nextGeneration[row][col] = 1;
                    }
                } 
            }
        }
    }
    
    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    private void copyBoard() 
    {
        // copies all the nextBoard values to the currBoard values
        for (int row = 0; row < nextGeneration.length - 1; row++) 
        {
            for (int col = 0; col < nextGeneration.length - 1; col++) 
            {
                nextGeneration[row][col] = currentGeneration[row][col];
            }
        }
    }
    
    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    public void print()
    {
        for (int row = 0; row < currentGeneration.length; row++)
        {
            for(int col = 0; col < currentGeneration[row].length; col++)
            {
                System.out.print(nextGeneration[row][col]);
            }
            System.out.println();
        }
    }   
}   



Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Printing not as expected

Posted 30 November 2011 - 04:00 PM

What do you men by not the right one? what do you get and what was your desire?
Was This Post Helpful? 0
  • +
  • -

#5 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 12-October 11

Re: Printing not as expected

Posted 30 November 2011 - 04:26 PM

Basically, I print this first.

0000000
0100000
0001100
0101000
0100000
0000000

Then I want it to follow some rules as stated in the code so that it creates another pattern, but instead I get,

0000000
0000000
0000000
0000000
0000000
0000000
but it should be:
0000000
0000000
0001000
0000000
0000000
0000000
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Printing not as expected

Posted 30 November 2011 - 04:40 PM

This is because you are generating another menu which was not the first one, so its values are default to zero.
 Cell Menu = new Cell();//you create another object which was not the first.
Menu.print();  


So your menu1() needs to know the first cell generated in the first call, and so it should know what was in it..
Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Printing not as expected

Posted 30 November 2011 - 06:10 PM

View Postcrapmyster, on 30 November 2011 - 06:25 PM, said:

I've corrected it so it actually prints the array but not the right one. Am I not passing the correct parameters or something.

You don't pass anny parameter to your print() method.
Actually, would be a really good idea to do so, so you could also that method to print your in generation1() to print the currentGeneration :)
:^:
Was This Post Helpful? 1
  • +
  • -

#8 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 12-October 11

Re: Printing not as expected

Posted 01 December 2011 - 04:19 AM

Hey, it's still not doing what I want it to. I've changed it to what you have said to, but it still isn't working. It still prints the same as did before, being just an array of zero's.

The code is:

/**
 * Write a description of class Cell here.
 * 
 * @author Mark Goddard
 * @version 20/11/2011
 */
public class Cell
{
    // instance variables
    private final int ROW = 5;
    private final int COL = 4;
    int[][] currentGeneration = new int[ROW+1][COL+1];
    int[][] nextGeneration = new int[ROW+1][COL+1];
    int row = 0;
    int col = 0;
    
    /**
    * Constructor for objects of class Cell
    */
    public Cell()
    {
        // initialise instance variables
        
    }
    
    /**
     * Intialising/Setting generation 1
     * 
     * @param  None
     * @return  None  
     */
    public void generation1()
    {
        for (int row = 0; row < currentGeneration.length; row++)
            for(int col = 0; col < currentGeneration[row].length; col++)
            {
                {
                    currentGeneration[1][1] = 1;
                    currentGeneration[2][3] = 1;
                    currentGeneration[2][4] = 1;
                    currentGeneration[4][1] = 1;
                    currentGeneration[3][3] = 1;
                    currentGeneration[2][3] = 1;
                    currentGeneration[3][1] = 1;
                    currentGeneration[row][col] = 0;
                }
            }
            for (int row = 0; row < currentGeneration.length; row++)
            {
                for(int col = 0; col < currentGeneration[row].length; col++)
                {
                    System.out.print(currentGeneration[row][col]);
                }
                System.out.println();
        }
    }
    
    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    public int countNeighbour(int count) 
    {
        count = 0;
        if (currentGeneration[row-1][col] == 1)
        {
            count++;
        }
        if (currentGeneration[row][col-1] == 1)
        {
            count++;
        }
        if (currentGeneration[row-1][col-1] == 1)
        {
            count++;
        }
        if (currentGeneration[row-1][col+1] ==1)
        {
            count++;
        }
        if (currentGeneration[row+1][col] ==1)
        {
            count++;
        }
        if (currentGeneration[row+1][col+1] ==1)
        {
            count++;
        }
        if (currentGeneration[row][col+1] ==1)
        {
            count++;
        }
        if (currentGeneration[row-1][col+1] ==1)
        {
            count++;
        }
        return count;
    }
    
    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    public void rules(int count) 
    {
        for (int row = 1; row < currentGeneration.length - 1; row++)
        {
            for (int col = 1; col < currentGeneration.length - 1; col++) 
            {
                // Any live cell with fewer than two live neighbours dies
                if(countNeighbour(count) < 2) 
                {
                    nextGeneration[row][col] = 0;
                }
                // Any live cell with more than three live neighbours dies
                if(countNeighbour(count) > 3)  
                {
                    nextGeneration[row][col] = 0;
                }
                // Any dead cell with exactly three live neighbours becomes a live cell
                if(countNeighbour(count)  == 3)  
                {
                    nextGeneration[row][col] = 1;
                }
                // Any live cell with two neighbours live on to the next generation
                if(countNeighbour(count)  == 2)  
                {
                    nextGeneration[row][col] = 1;
                }
            }
        }
    } 

    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    private void copyBoard() 
    {
        // copies all the nextBoard values to the currBoard values
        for (int row = 0; row < nextGeneration.length - 1; row++) 
        {
            for (int col = 0; col < nextGeneration.length - 1; col++) 
            {
                nextGeneration[row][col] = currentGeneration[row][col];
            }
        }
    }
    
    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    public void print(int[][] nextGeneration)
    {
        for (int row = 1;  row < nextGeneration.length; row++)
        {
            for(int col = 0; col < nextGeneration[row].length; col++)
            {
                System.out.print(nextGeneration[row][col]);
            }
            System.out.println();
        }
    }   
    
    public void print1()
    {
       print(nextGeneration);
    }   
}   






/**
 * A class which presents the user with a menu from which 
 * they can choose and demonstrates the game of life.
 * @author Mark Goddard
 * @version 31/10/2011
 */
public class Menu
{
    // instance variables
    
    /**
     * Constructor for objects of class Menu
     */
    public Menu()
    {
        // initialise instance variables
        
    }

    /**
     * Method to display the menu
     * 
     * @param  None
     * @return  None      
     */
    public static void displayMenu()
    {
        // Printing the individual choices to the screen
        System.out.println("GAME OF LIFE");
        System.out.println("");
        System.out.println("Main Menu: ");
        System.out.println("1 - Load a Generation");
        System.out.println("2 - Save game");
        System.out.println("3 - Load saved game");
        System.out.println("0 - Exit");  
        
        // Class the menu method as it defines what happens for each number chosen by the user
        Menu();
    }
    
    /**
     * Method to define the menu choices
     * 
     * @param  None
     * @return  None  
     */
    public static void Menu()
    {
        // Creating a local variable
        int choice;
        
        // Printing a message to the user
        System.out.println("");
        System.out.print("Please enter either number 1, 2, 3 or 4 for a particular option: ");
        
        // Using Genio method to get choice
        choice = Genio.getInteger();
        
        // Code for defining the choices available
        do if (choice == 1)
        {
            Cell Menu = new Cell();
            Menu.generation1();
            Menu1();
        }
        else if (choice == 2)
        {
            
        }
        else if (choice == 3)
        {
            
        }
        else if (choice == 4)
        {   
            
        }
        else if (choice == 0)
        {
            System.out.println("Goodbye, program is closing");
        }
        else
        {   
            System.out.println("Command not recognised");
        }
        while (false);
    } 

    /**
    * Method to define the menu choices
    * 
    * @param  None
    * @return  None  
    */
   public static void Menu1()
   {
       // Creating a local variable
       int choice;
        
       // Printing a message to the user
       System.out.println("");
       System.out.print("Please press 1 for the next generation: ");
        
       // Using Genio method to get choice
       choice = Genio.getInteger();
        
       // Code for defining the choices available
       do if (choice == 1)
       {
             
            Cell Menu = new Cell();
            Menu.print1();
       }
       else
       {   
           System.out.println("Command not recognised");
       }
       while (false);
   } 
}


Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Printing not as expected

Posted 01 December 2011 - 09:54 AM

As I said before you are creating different object every time you call Menu() and Menu1().
Also you never call rules() method which is the one to do the rules.
Lets list some ideas to go with:
- You call Menu(), it created a new cell and print it,
- Then pass the new cell created to menu1(), this will make sure we have the same cell. So this part:
Cell Menu = new Cell();
Menu.generation1();
Menu1();

Now call Menu1() and pass the new cell to it:
Menu1(Menu); // this means you have to change the definition of your Menu1() method

After that we come to Menu1().

- In your Menu1() dont create another object of Cell, instead use the passed one.
- Now if user select 1, call the method to generate next generation. This means you need to call rules() and pass count(i dont know what it does) to it. You have never called rules any where so dont expect any changes.

Think of your self how rules() will work if it has not been called, so make sure it is called somewhere.

- Another thing to take care of is your print1() and print() methods. Only one of them is needed, means You can just have print() with no parameters because you can access nextGenaration array in it since it is class variable.
Was This Post Helpful? 1
  • +
  • -

#10 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 12-October 11

Re: Printing not as expected

Posted 01 December 2011 - 11:08 AM

Right I've done like you have said but it one method.
The neighbourCount method counts all the neighbours around a particular cell.
The rules method is passed the neighbour count and that number is put through the rules.
The copyBoard method copies the currentGeneration to the nextGeneration array and then there is a print method.

/**
 * A class which presents the user with a menu from which 
 * they can choose and demonstrates the game of life.
 * @author Mark Goddard
 * @version 31/10/2011
 */
public class Menu
{
    // instance variables
    
    /**
     * Constructor for objects of class Menu
     */
    public Menu()
    {
        // initialise instance variables
        
    }

    /**
     * Method to display the menu
     * 
     * @param  None
     * @return  None      
     */
    public static void displayMenu()
    {
        // Printing the individual choices to the screen
        System.out.println("GAME OF LIFE");
        System.out.println("");
        System.out.println("Main Menu: ");
        System.out.println("1 - Load a Generation");
        System.out.println("2 - Save game");
        System.out.println("3 - Load saved game");
        System.out.println("0 - Exit");  
        
        // Class the menu method as it defines what happens for each number chosen by the user
        Menu();
    }
    
    /**
     * Method to define the menu choices
     * 
     * @param  None
     * @return  None  
     */
    public static void Menu()
    {
        // Creating a local variable
        int choice;
        
        // Printing a message to the user
        System.out.println("");
        System.out.print("Please enter either number 1, 2, 3 or 4 for a particular option: ");
        
        // Using Genio method to get choice
        choice = Genio.getInteger();
        
        // Code for defining the choices available
        do if (choice == 1)
        {
            Cell Menu = new Cell();
            Menu.generation1();
            
            System.out.println("");
            System.out.print("Please enter 1 for next generation: ");
            int choice1;
            choice = Genio.getInteger();
            if (choice == 1)
            {
               Menu.print(); 
            }
        }
        else if (choice == 2)
        {
            
        }
        else if (choice == 3)
        {
            
        }
        else if (choice == 4)
        {   
            
        }
        else if (choice == 0)
        {
            System.out.println("Goodbye, program is closing");
        }
        else
        {   
            System.out.println("Command not recognised");
        }
        while (false);
    }
}


Was This Post Helpful? 0
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Printing not as expected

Posted 01 December 2011 - 11:20 AM

So it is working? If not what it prints now?
Was This Post Helpful? 0
  • +
  • -

#12 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 12-October 11

Re: Printing not as expected

Posted 01 December 2011 - 11:25 AM

It is printing the same as before, I think it something to with my Cell class. I'm not doing something right with the rules method like you were saying before about passing it to another method. Just fiddling around with it now.
Was This Post Helpful? 0
  • +
  • -

#13 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Printing not as expected

Posted 01 December 2011 - 11:42 AM

Ya you didnt call the rule() method amywhere, so nothing will take place there.
Was This Post Helpful? 0
  • +
  • -

#14 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 12-October 11

Re: Printing not as expected

Posted 01 December 2011 - 01:06 PM

I don't really know what to do. Should I pass the rules through the generation1 method. Can you suggest anything?
Was This Post Helpful? 0
  • +
  • -

#15 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 12-October 11

Re: Printing not as expected

Posted 02 December 2011 - 06:57 AM

Right I'm working on this game called the Game of Life (Conway's Game of Life). Basically I have my class Cell and it prints the random grid out first and then it should call the runNextGeneration method which should print out the next generation. It prints it out vertically instead of like it should do in an array. I been messing around with it but can't see where I'm going wrong, any ideas?

import java.util.Random;
/**
 * 
 * 
 * @author 
 * @version 21/11/11
 */
public class Cell
{
    // instance variables - replace the example below with your own
    private static final int rowWidth = 10;
    private static final int colWidth = 10;
    
    /**
     * Constructor for objects of class Cell
     */
    public Cell()
    {
        // initialise instance variables
        
    }

    /**
     * Sets a 10x10 grid using the random package so that 1 and 0 are placed randomly on the grid
     * 
     * @param    
     * @return     
     */
    public void setGrid()
    {
        Random rand = new Random();
        int [][] currentGeneration = new int [rowWidth][colWidth];
        int [][] nextGeneration = new int [rowWidth][colWidth];
        //grid is filled randomly with 1's and 0's
        for (int row = 0; row < currentGeneration.length; row++)
        {
            for (int col = 0; col < currentGeneration[row].length; col++) 
            {
                 currentGeneration[row][col] = rand.nextInt(2);
            }
        }
        //the array is then displayed
        for(int i = 0; i <  currentGeneration.length; i++) 
        {
            for(int j = 0; j <  currentGeneration[i].length; j++)
            {
                   System.out.print(currentGeneration[i][j] + " ");
                   //System.out.println();
            }
        System.out.println("");
        }
        System.out.println("");
        runNextGeneration(currentGeneration, nextGeneration);
        
    }
    
    /**
     * The neighbours are counted and returned
     * 
     * @param  None
     * @return  None  
     */
    public int countNeighbour(int row, int col, int[][] currentGeneration) 
    {
        int count = 0;
        for(row = 1; row <  currentGeneration.length - 1; row++) 
        {
            for(col = 1; col <  currentGeneration[row].length - 1; col++)
            {
                if (currentGeneration[row-1][col] == 1)
                {
                    count++;
                }
                if (currentGeneration[row+1][col] == 1)
                {
                    count++;
                }
                if (currentGeneration[row][col-1] == 1)
                {
                    count++;
                }
                if (currentGeneration[row][col+1] == 1)
                {
                    count++;
                }
                if (currentGeneration[row-1][col-1] == 1)
                {
                    count++;
                }
                if (currentGeneration[row-1][col+1] == 1)
                {
                    count++;
                }
                if (currentGeneration[row+1][col-1] == 1)
                {
                    count++;
                }
                if (currentGeneration[row+1][col-1] == 1)
                {
                    count++;
                }
                if (currentGeneration[row+1][col+1] == 1)
                {
                    count++;
                }
            }
        }
        return count;
    }
    
    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    public void rules(int[][] currentGeneration, int[][] nextGeneration) 
    {
        for (int row = 1; row < currentGeneration.length; row++)
        {
            for ( int col = 1; col < currentGeneration.length; col++) 
            {
                // Any live cell with fewer than two live neighbours dies
                if(countNeighbour(row, col, currentGeneration) < 2) 
                {
                    nextGeneration[row][col] = 0;
                }
                // Any live cell with more than three live neighbours dies
                if(countNeighbour(row, col, currentGeneration) > 3)  
                {
                    nextGeneration[row][col] = 0;
                }
                // Any dead cell with exactly three live neighbours becomes a live cell
                if(countNeighbour(row, col, currentGeneration)  == 3)  
                {
                    nextGeneration[row][col] = 1;
                }
                // Any live cell with two neighbours live on to the next generation
                if(countNeighbour(row, col, currentGeneration)  == 2)  
                {
                    nextGeneration[row][col] = 1;
                }
            }
        }
    }
     
    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    public void copyGeneration(int[][] currentGeneration, int[][] nextGeneration) 
    {
        // copies all the nextBoard values to the currBoard values
        for (int row = 0; row < currentGeneration.length; row++) 
        {
            for (int col = 0; col < currentGeneration.length; col++) 
            {
                nextGeneration[row][col] = currentGeneration[row][col];
            }
        }
        
    } 
    
    /**
     * Game of Life Rules
     * 
     * @param  None
     * @return  None  
     */
    public void print(int[][] nextGeneration)
    {
        for (int row = 0; row < nextGeneration.length; row++)
        {
            for (int col = 0; col < nextGeneration[row].length; col++)
            {
                System.out.println(nextGeneration[row][col] + "");
            }
        }
        System.out.print("");
    }  
    
    public void runNextGeneration (int[][] currentGeneration, int[][] nextGeneration) 
    {
        // executes the continuation of the program
        print(currentGeneration);
        copyGeneration(currentGeneration, nextGeneration);
        rules(currentGeneration, nextGeneration);
        print(nextGeneration);
        copyGeneration(nextGeneration, currentGeneration);
        rules(nextGeneration, currentGeneration);
        
    }
}



Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2