Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Conway's game of Life please help D:

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Conway's game of Life please help D:

    I manage to get it to generate the cells, but unfortunately only one cell generates and moves left diagonally and every other generation it activates cells below and to the right of it

    package GameofLife;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.MouseEvent;//need it for the mouse when clicking on the grid
    import java.awt.event.MouseListener;
    import java.awt.event.ActionListener;//the buttons react
    import java.awt.event.ActionEvent;
     
    public class GameofLife implements MouseListener, ActionListener, Runnable{
     
      final int WIDTH = 75; //every calculation in the grid will rely on these variable names.
      final int HEIGHT = 75;
      JFrame frame = new JFrame("Give me an A, please.");//window heading
      boolean[][] grid = new boolean[WIDTH][HEIGHT];
      CustomPanel panel = new CustomPanel(grid);
     
      //buttons to be pressed to perform certain functions.
      //take the hint please.
      JButton next = new JButton("Next, give me a passing grade");
      JButton start = new JButton("Start, give 90%+");
      JButton stop = new JButton("Stop, don't give anything lower than 90%");
      Container north = new Container();//holds the buttons
      boolean running = false;//used for runnable, run
     
      public GameofLife(){
     
        frame.setSize(600,600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//has the X button
        frame.setVisible(true);//window is visible
        frame.setLayout(new BorderLayout());
        frame.add(panel,BorderLayout.CENTER);//grid
        frame.add(north,BorderLayout.NORTH);//button layout
        panel.addMouseListener(this);
     
        next.addActionListener(this);
        start.addActionListener(this);
        stop.addActionListener(this);
     
        north.setLayout(new GridLayout(1,3));//row and column
        north.add(next);//button
        north.add(start);//button
        north.add(stop);
     
      }
      public static void main(String[] args){
       new GameofLife();  
      }
     
      public void mouseClicked(MouseEvent ev){
      }
      public void mouseEntered(MouseEvent ev){
      }
      public void mouseExited(MouseEvent ev){
      }
      public void mousePressed(MouseEvent ev){
      }
      public void mouseReleased(MouseEvent ev)throws ArrayIndexOutOfBoundsException{//activates the cell(turns it black)
        System.out.println(ev.getX() + "," + ev.getY());
     
        //most aggrivating code, sorry but the only way it works if you click the cell you want to activate in a special way.
        //this section is a little off T.T(yes that is a crying pain staking emoticon).
     
        final short row = (short)(Math.min(ev.getY() /7,75));
        final short col = (short)(Math.min(ev.getX() /7,75));
        grid[col][row] = (!grid[col][row]);
        System.out.println(col+","+row+","+panel.getHeight()+","+panel.getWidth());
        frame.repaint();
     
        try{//catches out of bounds exception
          grid[col][row] = grid[col][row];
        }catch(ArrayIndexOutOfBoundsException e){
          System.out.println("Too Far");
        }
      } 
        public void actionPerformed(ActionEvent ev){
          if(ev.getSource().equals(next) == true){
          next();
          frame.repaint();
          }
          if(ev.getSource().equals(start) == true){
           Thread thread = new Thread(this);
           running = true;
           thread.start();
          }
          if(ev.getSource().equals(stop) == true){
            running = false;
          }
      }
        public void run(){
          while(running == true){
            next();
     
            try{
            Thread.sleep(1000);
            }catch(Exception ex){//catches all if any exceptions with the run time
              ex.printStackTrace();
            }
        }
        }
      public void next(){
        boolean[][] newGrid = new boolean[grid.length][grid.length];
        int neighbor = 0;
        //checks the neighbors.
        for(int i = 0; i < grid.length; i++){
          for(int j = 0; j < grid.length; j++){
            if(j > grid.length -1 && grid[i][j-1] == true){//checks bottom
              neighbor = neighbor + 1;
            }
            if(j < grid.length -1 && grid[i][j+1] == true){//checks top
              neighbor = neighbor + 1;
            }
            if(i > grid.length-1 && grid[i-1][j] == true){//checks left
              neighbor = neighbor + 1;
            }
            if(i < grid.length - 1 && grid[i +1][j] == true){//checks right
              neighbor = neighbor + 1;
            }
            if(j < grid.length-1  && i < grid.length-1  && grid[i+1][j+1] == true){//checks top right
              neighbor = neighbor + 1;
            }
            if(j > grid.length-1  && i > grid.length-1  && grid[i-1][j-1] == true){//checks bottom left
              neighbor = neighbor + 1;
            }
            if(j > grid.length-1  && i < grid.length-1   && grid[i+1][j-1] == true){//checks top left
              neighbor = neighbor + 1;
            }
            if(j < grid.length-1  && i > grid.length-1  && grid[i-1][j+1] == true){//checks bottom right
              neighbor = neighbor + 1;
            }
     
     
     
            if(grid[i][j] == true){//if alive and neighbors are two or three, alive, else false.
              if(neighbor == 2 || neighbor == 3){
                newGrid[i][j] = true;
              }
     
     
              else{//dead cell
                if(neighbor < 2 || neighbor > 3){
                newGrid[i][j] = false;
              }
            }
            }
            else if(grid[i][j] == false){//dead to alive
              if(neighbor == 3){
                newGrid[i][j] = true;
     
              }
              else{//still dead
                newGrid[i][j] = false;
              }
            }
          }
          }
     
            grid = newGrid;
            panel.setGrid(newGrid);
            frame.repaint();
     
     
        }
      }

    Here is the second class:


    package GameofLife;
     
    import javax.swing.JPanel;
    import java.awt.Graphics;
    import java.awt.Color;
     
     
    public class CustomPanel extends JPanel{
     
       boolean[][] grid;
     
       public CustomPanel(boolean[][] newGrid){
         //connects to GameofLife
         grid = newGrid;
     
       }
     
       public void setGrid(boolean[][] newGrid){//for the changes on the grid
         grid = newGrid;
       }
      public void paintComponent(Graphics g){
     
        super.paintComponent(g);
     
        //makes the math much simpler.
        double cellWidth = ((double)(532))/grid.length;
        double cellHeight = ((double)(532))/grid.length;
       //draws out the lines
        for(int i = 0; i < grid.length; i++){
          for(int j = 0; j < grid.length; j++){
             g.drawLine(0,(int)(i*cellHeight),532,(int)(i*cellHeight));
             g.drawLine((int)(j*cellWidth),0,(int)(j*cellWidth),532);
             //if clicked on, color the cell that color.
             if(grid[i][j] == true){
               g.setColor(Color.BLACK);
               g.fillRect((int)(i*cellWidth),(int)(j*cellHeight),(int)(cellWidth),(int)(cellHeight));
             }
        }
     
      }
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Conway's game of Life please help D:

    Since we can't run it, we can only suggest adding print statements in strategic places to inspect variable values while it runs to determine what is happening that you were not expecting.

  3. #3
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Conway's game of Life please help D:

    Quote Originally Posted by GregBrannon View Post
    Since we can't run it, we can only suggest adding print statements in strategic places to inspect variable values while it runs to determine what is happening that you were not expecting.
    Well the program here so far has only two classes. let me post the second class ^^;

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Conway's game of Life please help D:

    First thing I recommend is to change the code to use a few variables so it's size can be made smaller for testing.
    For example:
      final int WIDTH = 10; // 75; //every calculation in the grid will rely on these variable names.
      final int HEIGHT = 10; //75;
    The mouseListener has hardcoded 7 and 75 that causes a out of bounds error.
    The CustomPanel class has a hardcoded 532 that breaks the program with the above values.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Anyone know how to do this game of life problem
    By by513 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2013, 10:19 PM
  2. Need help with Game of Life generations
    By MarcusSt0ne in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 7th, 2012, 08:56 AM
  3. Help with my game of life program
    By thatni**a in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 11th, 2012, 09:20 AM
  4. Game of Life GUI Error
    By Lavace in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 3rd, 2011, 09:15 AM
  5. Replies: 1
    Last Post: March 28th, 2009, 07:21 AM

Tags for this Thread