3 Replies - 347 Views - Last Post: 16 February 2016 - 01:18 PM Rate Topic: -----

#1 sharma111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-February 16

Javafx My code is not working for conways game of life

Posted 16 February 2016 - 12:51 PM

  package application;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Ellipse;

public class GUIDemo extends Application implements EventHandler<MouseEvent>
{
	public static void main(String[] args) 
	{
	    launch(args);
	 }
	
	
  // Indicate which player has a turn, initially it is the X player
  private char whoseTurn = 'X';
  final int EMPTY = 0;  // same as Dead
  final int ALIVE = 1;
  final int BECOMING_ALIVE = 2;  //  when changing from EMPTY to ALIVE
  int CELL_SIZE;
  public int SquareSize = 16;
  public int oldgrid [][];
  public int newgrid [][];
  public int ngens=5;
  public int width = 10;
  public int height = 10;
  public int currentgen = 0;
  public Group root;
  

  // Create and initialize cell
  private Cell[][] cell =  new Cell[75][75];

  // Create and initialize a status label
  //private Label lblStatus = new Label("X's turn to play");

  @Override // Override the start method in the Application class
  public void start(Stage primaryStage) 
  {
    // Pane to hold cell
    GridPane pane = new GridPane(); 
    for (int i = 0; i < 75; i++)
      for (int j = 0; j < 75; j++)
        pane.add(cell[i][j] = new Cell(), j, i);
    
    Button btn1 = new Button("Start");
    BorderPane borderPane = new BorderPane();
    borderPane.setCenter(pane);
    borderPane.setBottom(btn1);
    
    // Create a scene and place it in the stage
    Scene scene = new Scene(borderPane, 1450, 7170);
    primaryStage.setTitle("Game Of Life"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage 
    
  }

  /** Determine if the cell are all occupied */
  public boolean isFull() {
    for (int i = 0; i < 75; i++)
      for (int j = 0; j < 75; j++)
        if (cell[i][j].getToken() == ' ')
          return false;

    return true;
  }

  
  //-------------------------------RULES--RULES--RULES----------------------------------------------------
  /*Any live cell with fewer than two live neighbors dies, as if caused by under-population.
Any live cell with two or three live neighbors lives on to the next generation.
Any live cell with more than three live neighbors dies, as if by over-population.
Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
*/

  
  public int getcurrentgen() 
  {
      return currentgen;
  }

  public int getX() {
      return oldgrid[0].length;
  }

  public int getY() {
      return oldgrid.length;
  }

  int Neighbours(int x, int y) //calculating neighbors
  {
      int neighbours = 0;

      if (x > 0 && oldgrid[y][x - 1] == 1) {
          neighbours++;
      }
      if (x < getX() - 1 && oldgrid[y][x + 1] == 1) {
          neighbours++;
      }
      if (y < getY() - 1 && x > 0 && oldgrid[y + 1][x - 1] == 1) {
          neighbours++;
      }
      if (y < getY() - 1 && x < getX() - 1 && oldgrid[y + 1][x + 1] == 1) {
          neighbours++;
      }
      if (y < getY() - 1 && oldgrid[y + 1][x] == 1) {
          neighbours++;
      }
      if (y > 0 && x > 0 && oldgrid[y - 1][x - 1] == 1) {
          neighbours++;
      }
      if (y > 0 && x < getX() - 1 && oldgrid[y - 1][x + 1] == 1) {
          neighbours++;
      }
      if (y > 0 && oldgrid[y - 1][x] == 1) {
          neighbours++;
      }

      return neighbours;
  }

  public void setup() // applying the rules
  {
      for (int y = 0; y < getY(); y++) {
          for (int x = 0; x < getX(); x++) {

              if (oldgrid[y][x] == 1) {
                  if (Neighbours(x, y) < 2) {
                      newgrid[y][x] = 0;
                  }
                  if (Neighbours(x, y) > 3) {
                      newgrid[y][x] = 0;
                  }
                  if (Neighbours(x, y) == 3 || Neighbours(x, y) == 2) {
                      newgrid[y][x] = 1;
                  }
              }
              if (oldgrid[y][x] == 0) {
                  if (Neighbours(x, y) == 3) {
                      newgrid[y][x] = 1;
                  }

              }

          }

      }
  }

  public void draw(GraphicsContext gc)
  {
      for(int y =0 ; y < getY() ; y++ )
      {
          for (int x = 0; x < getX() ; x++)
          {
              if(oldgrid[y][x] == 0)
              {
                  gc.setFill(Color.WHITE);
                  gc.fillRect(x*SquareSize, y*SquareSize, SquareSize, SquareSize);
              }
              else if(oldgrid[y][x] == 1)
              {
                  gc.setFill(Color.BLACK);
                  gc.fillRect(x*SquareSize, y*SquareSize, SquareSize, SquareSize);
              }
          }
      }

  }
  
  public void run(GraphicsContext gc)
  {
      draw(gc);
      while(currentgen < ngens)
      {
          try {
              Thread.sleep(1000);
          } catch(InterruptedException ex) {
              Thread.currentThread().interrupt();
          }
          setup();
          for (int y = 0; y < getY(); y++) {
              for (int x = 0; x < getX(); x++) {
                  oldgrid[y][x] = newgrid[y][x];
              }
          }
          currentgen++;
          draw(gc);
      }
  }
  
  
  public void randomize() {

      // randomizing dead(0) and live(1) cells
      for (int y = 0; y < getY(); y++) {
          for (int x = 0; x < getX(); x++) {
              double tmp = Math.random();
              if (tmp < 0.5) {
                  oldgrid[y][x] = 0;
              } else {
                  oldgrid[y][x] = 1;
              }

          }
      }

  }

  

  
//-------------------------------RULES--RULES--RULES----------------------------------------------------
  
  
  
  // An inner class for a cell
  public class Cell extends Pane
  {
    // Token used for this cell
    private int token = ' ';

    public Cell() 
    {
      setStyle("-fx-border-color: black"); 
      this.setPrefSize(2000, 2000);
      this.setOnMouseClicked(e -> handleMouseClick());
    }

    /** Return token */
    public int getToken() 
    {
      return token;
    }

    /** Set a new token */
    public void setToken(char c) 
    {
      token = c;
      
  //-------------------------------LINES--LINES--LINES----------------------------------------------------      
      
      
      if (token == 'X') 
      {
        Line line1 = new Line(10, 10, 
          this.getWidth() - 10, this.getHeight() - 10);
        line1.endXProperty().bind(this.widthProperty().subtract(10));
        line1.endYProperty().bind(this.heightProperty().subtract(10));
        Line line2 = new Line(10, this.getHeight() - 10, 
          this.getWidth() - 10, 10);
        line2.startYProperty().bind(
          this.heightProperty().subtract(10));
        line2.endXProperty().bind(this.widthProperty().subtract(10));
 
        
  
        // Add the lines to the pane
        this.getChildren().addAll(line1, line2); 
      }
      else if (token == 'O') 
      {
        Ellipse ellipse = new Ellipse(this.getWidth() / 2, 
          this.getHeight() / 2, this.getWidth() / 2 - 10, 
          this.getHeight() / 2 - 10);
        ellipse.centerXProperty().bind(
          this.widthProperty().divide(2));
        ellipse.centerYProperty().bind(
            this.heightProperty().divide(2));
        ellipse.radiusXProperty().bind(
            this.widthProperty().divide(2).subtract(10));        
        ellipse.radiusYProperty().bind(
            this.heightProperty().divide(2).subtract(10));   
        ellipse.setStroke(Color.BLACK);
        ellipse.setFill(Color.WHITE);
        
        getChildren().add(ellipse); // Add the ellipse to the pane
      }
    }
    
 //-------------------------------LINES--LINES--LINES----------------------------------------------------      
    

    
   //................Mouse Handler Events...............................\\
    //Handle a mouse click event 
    private void handleMouseClick()
    {
      // If cell is empty and game is not over
      if (token == ' ' && whoseTurn != ' ') 
      {
        setToken(whoseTurn); // Set token in the cell

        // Check game status
        if (ALIVE(whoseTurn))
        {
          
          whoseTurn = ' '; // Game is over
        }
        else if (isFull()) 
        {
         
          whoseTurn = ' '; // Game is over
        }
        else 
        {
          // Change the turn
          whoseTurn = (whoseTurn == 'X') ? 'O' : 'X';
         
        }
      }
    }

	private boolean ALIVE(int whoseTurn) 
	{
		// TODO Auto-generated method stub
		return false;
	}
  }


@Override
public void handle(MouseEvent e) 
{
	
	
}
  
  
 
  
} 


I have created the grid and the mouse event. The program created the grid but when i click into the grid no cell gets created and no animation occurs. What did i do wrong and what should i do to make this better? Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Javafx My code is not working for conways game of life

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,470
  • Joined: 12-December 12

Re: Javafx My code is not working for conways game of life

Posted 16 February 2016 - 12:57 PM

Whereabouts in your code should someone be looking?
Are you saying that you wrote around 350 lines of code before running it? You need to run and test your code as often as possible.
Was This Post Helpful? 0
  • +
  • -

#3 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,267
  • Joined: 25-December 13

Re: Javafx My code is not working for conways game of life

Posted 16 February 2016 - 01:11 PM

The first thing I suggest for debugging the code is to make a smaller grid, say 6x6. Define an int variable with the size and use that variable throughout the code instead of the hardcoded 75. Later when the code is working change the value of the variable to 75.
   final int SIZE = 6;  // Define size of grid
  //  ...

  // Create and initialize cell
  private Cell[][] cell =  new Cell[SIZE][SIZE];


This post has been edited by NormR: 16 February 2016 - 01:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13953
  • View blog
  • Posts: 55,693
  • Joined: 12-June 08

Re: Javafx My code is not working for conways game of life

Posted 16 February 2016 - 01:18 PM

Better yet - how about you produce your own work versus stealing code from elsewhere. I am closing the topic as you have put no effort into this outside of asking for people to do your work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1