Conways Game of Life

Lucaaa

Bekanntes Mitglied
Hallo!
Ich bin gerade dabei, das Conways Game of Life in Java zu programmieren.
Mein Problem ist nun, das wenn ich das Programm starte, keine Bewegung passiert. Die Zellen werden zwar gezeichnet, updaten aber nicht.
Hier die Klassen:

Main.java
Java:
package com.ludevstudio.conwaysgameoflife;
public class Main {
 static int width;
 static int height;
 
 
 
 public static void main(String[] args) {
  Frame frame = new Frame();
  frame.setDefaultCloseOperation(3);
  frame.setResizable(false);
  frame.setUndecorated(true);
  frame.setVisible(true);
 
 width = frame.getWidth();
 height = frame.getHeight();
 
 frame.createScreen();
 
 
 long lastFrame = System.currentTimeMillis();
 while(true) {
  long thisFrame = System.currentTimeMillis();
  float tslf = (float) ((thisFrame-lastFrame) / 1000.0);
  lastFrame = thisFrame;
  
  frame.update(tslf);
  frame.repaint();
  
  
  try {
   Thread.sleep(10);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 
 }
 }
 
 }

Frame.java
Java:
package com.ludevstudio.conwaysgameoflife;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.font.GraphicAttribute;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Frame extends JFrame {
 private Screen screen;
 
 Simulation sim;
 
 
 public float UPDATE_TIME = 0.1f;
 public float tslu = 0;
 
 
 public Frame() {
  setExtendedState(MAXIMIZED_BOTH);
  
 }
 
 
 public void createScreen() {
  sim = new Simulation();
  screen = new Screen();
 screen.setBounds(0,0, Main.width, Main.height);
 add(screen);
 
 }
 
 public void repaint() {
  screen.repaint();
 }
 
 
 
 
 private class Screen extends JLabel {
  
  @Override
  public void paintComponent(Graphics g) {
   super.paintComponent(g);
   
   sim.draw(g);
   
  }
  
  
  
  
  
 }
 
 
 public void update(float tslf) {
  tslu += tslf;
  if(tslu > UPDATE_TIME) {
  sim.update();
  tslu = 0;
  }
 }
 
 
 
 }

Simulation.java
Java:
package com.ludevstudio.conwaysgameoflife;
import java.awt.Graphics;
import java.util.Random;
public class Simulation {
 
 private Cell[][] cells;
 private int witdh = Main.width / Cell.size;
 private int height = Main.height / Cell.size;
 
 private Random random;
 
 public Simulation() {
  cells = new Cell[witdh] [height];
  
  random = new Random();
  
  
  for(int x = 0; x<witdh; x++) {
   for(int y = 0; y<height; y++) {
    cells[x][y] = new Cell(x, y);
    cells[x][y].setAlive(random.nextBoolean());
   }
  }
 }
 
 public void draw(Graphics g) {
  for(int x = 0; x<witdh; x++) {
   for(int y = 0; y<height; y++) {
    cells[x][y].draw(g);
   }
  }
  
 }
 
 
 void update() {
  for(int x = 0; x<witdh; x++) {
   for(int y = 0; y<height; y++) {
    int mx = x-1;
    if(mx<0) mx = witdh-1;
    int my = y-1;
    if(mx<0) mx = witdh-1;
    int gx = (x+1)%witdh;
    int gy = (y+1)%height;
    
    int aliveCounter = 0;
    
    if(cells[mx][my].isAlive()) aliveCounter++;
    if(cells[mx][y].isAlive()) aliveCounter++;
    if(cells[mx][gy].isAlive()) aliveCounter++;
    if(cells[x][my].isAlive()) aliveCounter++;
    if(cells[x][y].isAlive()) aliveCounter++;
    if(cells[gx][my].isAlive()) aliveCounter++;
    if(cells[gx][y].isAlive()) aliveCounter++;
    if(cells[gx][gy].isAlive()) aliveCounter++;
   
    if(aliveCounter <2 || aliveCounter > 3 ) {
     cells[x][y].nextRoundAlive = false;
   }  else if(aliveCounter == 3) {
    cells[x][y].nextRoundAlive = true;
   }
   
  }
   
  }
  
  
  
  
  for(int x = 0; x<witdh; x++) {
   for(int y = 0; y<height; y++) {
    cells[x][y].nextRoundAlive();
   }
  }
  
 }
 
 
 
 
}

Cell.java
Java:
package com.ludevstudio.conwaysgameoflife;
import java.awt.Color;
import java.awt.Graphics;
public class Cell {
 private int x;
 private int y; 
 
 static int size = 10;
 
 public boolean alive;
 public boolean nextRoundAlive;
 

 public Cell(int x, int y) {
  this.x = x;
  this.y = y;
  
 }
 
 
 public void draw(Graphics g) {
  g.setColor(Color.BLACK);
  g.drawRect(x*size, y*size, size, size);
  
  if(alive) g.setColor(Color.black);
  else g.setColor(Color.white);
  
  g.fillRect(x*size+1, y*size+1, size-1, size-1);
  
 }
 
 
 public boolean isAlive() {
  return alive;
 }

 public void setAlive(boolean alive) {
  this.alive = alive;
 }
 
 public void nextRoundAlive() {
  alive = nextRoundAlive;
 }
 
 public boolean isNextRoundAlive() {
  return nextRoundAlive;
 }
 
 
 
}


Danke schon mal für eure Hilfe!
 
Ahh es lag daran, dass ich die If abfrage ob mx <0 ist 2 mal gemacht habe, anstatt beim zweiten mal my abzufragen.
Danke!
 

Zurück
Oben