0 Replies - 960 Views - Last Post: 30 January 2014 - 10:27 PM Rate Topic: -----

#1 catnamedkat   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-May 13

Interactive Game of Life in Java

Posted 30 January 2014 - 10:27 PM

I'm trying to write an interactive game of life where i can manually insert gliders in the game field.
How i want it to work is i have a glider button and after i press it i can move my cursor to where i want glider to be set on the grid and after i click on the grid glider integrates in the game of life.
I'm using processing, and i'm using this sketch as a start up code. http://www.openproce...rg/sketch/95216

This is the code to create new cells on mouse press (one at the time)

    // Create  new cells manually on pause
      if (pause && !gliderSelected && mousePressed) {
        // Map and avoid out of bound errors
        int xCellOver = int(map(mouseX, 0, width, 0, width/cellSize));
        xCellOver = constrain(xCellOver, 0, width/cellSize-1);
        int yCellOver = int(map(mouseY, 0, height, 0, height/cellSize));
        yCellOver = constrain(yCellOver, 0, height/cellSize-1);
    
        // Check against cells in buffer
        if (cellsBuffer[xCellOver][yCellOver]==1) { // Cell is alive
          cells[xCellOver][yCellOver]=0; // Kill
          fill(dead); // Fill with kill color
        }
        else { // Cell is dead
          cells[xCellOver][yCellOver]=1; // Make alive
          fill(alive); // Fill alive color
        }
      } 
      else if (pause && !mousePressed) { // And then save to buffer once mouse goes up
        // Save cells to buffer (so we operate with one array keeping the other intact)
        for (int x=0; x<width/cellSize; x++) {
          for (int y=0; y<height/cellSize; y++) {
            cellsBuffer[x][y] = cells[x][y];
          }
        }
      }



Glider shape is :

OXO <br>
OOX <br>
XXX <br>

where O is a dead cell and X is an alive cell.

      //create gliders on press
      if (pause && gliderSelected && mousePressed) {
        // Map and avoid out of bound errors
        int xCellOver = int(map(mouseX, 0, width, 0, width/cellSize));
        xCellOver = constrain(xCellOver, 0, width/cellSize-1);
        int yCellOver = int(map(mouseY, 0, height, 0, height/cellSize));
        yCellOver = constrain(yCellOver, 0, height/cellSize-1);
        
        //here i thought of maybe creating an array of cells that map the glider and then running a loop to change the grid cell status according to the glider array 
        

      }



I'm not sure how to make an array that will store the glider cell locations. Each cell is a 10 pixel square, so i know how to map it if i wanted to just build it, but not sure how to stick it in the array and then integrate it in the grid.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1