Conways Spiel des Lebens nachprogrammiert

janco2000

Mitglied
Hallo,
ich habe angefangen, Conways Spiel des Lebens nachzuprogrammieren. Allerdings flackert das Bild. Ich glaube das hängt mit dem Thread zusammen, finde aber den Fehler nicht. Vielen Dank für jede Hilfe! Anbei die Hauptklasse GameOfLive und die Klasse Cell für die Zellen.

Hauptklasse:
Java:
package gameoflive;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

public class GameOfLive extends JFrame implements Runnable, MouseListener, KeyListener{
	
	Cell[][] cell = new Cell[20][20];
	public boolean start = false;
	
	public GameOfLive(String title){
		super(title);
		//Init Cells
		initCells();
		this.addMouseListener(this);
		this.setFocusable(true);
		this.addKeyListener(this);
	}
	
	public static void main(String[] args){
		GameOfLive game = new  GameOfLive("Das Spiel des Lebens");
		game.setSize(800, 800);
		new Thread(game).start();
		game.setLocation(150, 30);
		game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		game.setUndecorated(true);
		game.setVisible(true);
	}
	
	public void getNeighbours(){
		for(int x=0; x<20; x++){
			for(int y=0; y<20; y++){
			//Prüfen
			int count = 0;
			
			//Lebende Zelle
			if(cell[x][y].getAlive()){
				if(cell[x-1][y].getAlive()){
					count++;
				}
				if(cell[x+1][y].getAlive()){
					count++;
				}
				if(cell[x][y-1].getAlive()){
					count++;
				}
				if(cell[x][y+1].getAlive()){
					count++;
				}
				if(cell[x-1][y-1].getAlive()){
					count++;
				}
				if(cell[x+1][y-1].getAlive()){
					count++;
				}
				if(cell[x-1][y+1].getAlive()){
					count++;
				}
				if(cell[x+1][y+1].getAlive()){
					count++;
				}
				//Regeln
				if(count<2){
					cell[x][y].setNextState(false);
				}
				if(count==2 || count==3){
					cell[x][y].setNextState(true);
				}
				if(count>3){
					cell[x][y].setNextState(false);
				}
			}
			
			//Tote Zelle
			int count2 = 0;
			if(x>=1 && y>=1 && x<=18 && y<=18){
			if(!cell[x][y].getAlive()){
				if(cell[x-1][y].getAlive()){
					count2++;
				}
				if(cell[x+1][y].getAlive()){
					count2++;
				}
				if(cell[x][y-1].getAlive()){
					count2++;
				}
				if(cell[x][y+1].getAlive()){
					count2++;
				}
				if(cell[x-1][y-1].getAlive()){
					count++;
				}
				if(cell[x+1][y-1].getAlive()){
					count2++;
				}
				if(cell[x-1][y+1].getAlive()){
					count2++;
				}
				if(cell[x+1][y+1].getAlive()){
					count2++;
				}
				//Regeln
				if(count2==3){
					cell[x][y].setNextState(true);
				}
			}
			}
		}
	}
	}
	
	public void initCells(){
		for(int x=0; x<20; x++){
			for(int y=0; y<20; y++){
				cell[x][y] = new Cell(false, x*40, y*40);
			}
		}
	}
	
	public void paint(Graphics gr){
		super.paintComponents(gr);
		
		//Hintergrund Weiß
		gr.setColor(Color.WHITE);
		gr.fillRect(0, 0, 800, 800);
		
		//Cell
		for(int x=0; x<20; x++){
			for(int y=0; y<20; y++){
				cell[x][y].paintCell(gr);
			}
		}
		
		//Raster
		gr.setColor(Color.BLACK);
		for(int x=0; x<40; x++){
			gr.drawLine(40*x, 0, 40*x, 800);
		}
		for(int y=0; y<40; y++){
			gr.drawLine(0, 40*y, 800, 40*y);
		}
	}

	public void run() {		
		while(true){
			if(start){
				getNeighbours();
				for(int x=0; x<20; x++){
					for(int y=0; y<20; y++){
						cell[x][y].setAlive(cell[x][y].nextState());
						System.out.println("Funktioniert!");
					}
				}
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			repaint();
			
		}
		
	}

	public void mouseClicked(MouseEvent e) {
		
	}

	public void mouseEntered(MouseEvent e) {
	
	}

	public void mouseExited(MouseEvent e) {
		
	}

	public void mousePressed(MouseEvent e) {
		int x = e.getX()/40;
		int y = e.getY()/40;
		cell[x][y].setAlive(true);
		System.out.println("X: "+x+" Y: "+y);
	}

	public void mouseReleased(MouseEvent e) {
		
	}

	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode()==32){
			start = true;
		}
	}

	public void keyReleased(KeyEvent e) {
		
	}

	public void keyTyped(KeyEvent e) {
		if(e.getKeyCode()==32){
			start = true;
		}
	}

}

Klasse Cell:
Java:
package gameoflive;

import java.awt.Color;
import java.awt.Graphics;

public class Cell {
	
	private boolean alive, nextState;
	private int x, y;
	
	public Cell(boolean alive, int x, int y){
		this.alive = alive;
		this.x = x;
		this.y = y;
	}
	
	public boolean getAlive(){
		return alive;
	}
	
	public boolean nextState(){
		return nextState;
	}
	
	public void setAlive(boolean alive){
		this.alive = alive;
	}
	
	public void setNextState(boolean nextState){
		this.nextState = nextState;
	}
	
	public void paintCell(Graphics gr){
		if(alive){
			gr.setColor(Color.GREEN);
		}else if(!alive){
			gr.setColor(Color.WHITE);
		}	
		gr.fillRect(x, y, 40, 40);
	}

	public boolean getNextState() {
		// TODO Auto-generated method stub
		return false;
	}	
	
}
 
Ich hab's mal überarbeitet ... kannst ja ein bisschen durchschauen:

Java:
package gameoflive;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GameOfLive extends JPanel implements Runnable {

	private static final long serialVersionUID = 1L;

	private Cell[][] cell = new Cell[20][20];
	private boolean start = false;

	public GameOfLive() {
		initCells();
		this.addMouseListener(new MouseController(this));
		this.setFocusable(true);
		this.addKeyListener(new KeyController(this));
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame("Das Spiel des Lebens");
		frame.setSize(800, 800);
		frame.setLocation(150, 30);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GameOfLive game = new GameOfLive();
		frame.setContentPane(game);
		frame.setVisible(true);
		new Thread(game).start();
	}

	public void calculateNextStep() {
		for (int x = 0; x < 20; x++) {
			for (int y = 0; y < 20; y++) {
				int count = 0;
				for (int dx = -1; dx <= 1; dx++) {
					for (int dy = -1; dy <= 1; dy++) {
						int xx = x + dx;
						int yy = y + dy;
						if (!(dx == 0 && dy == 0) && isInside(xx, yy)) {
							if (cell[xx][yy].getAlive()) {
								count++;
							}
						}
					}
				}
				if (cell[x][y].getAlive()) {
					boolean alive = (count == 2 || count == 3);
					cell[x][y].setNextState(alive);
				} else {
					cell[x][y].setNextState(count == 3);
				}
			}
		}
	}

	private boolean isInside(int xx, int yy) {
		return xx >= 0 && yy >= 0 && xx < 20 && yy < 20;
	}

	public void initCells() {
		for (int x = 0; x < 20; x++) {
			for (int y = 0; y < 20; y++) {
				cell[x][y] = new Cell(false, x * 40, y * 40);
			}
		}
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		for (int x = 0; x < 20; x++) {
			for (int y = 0; y < 20; y++) {
				cell[x][y].paintCell(g);
			}
		}
		g.setColor(Color.BLACK);
		for (int i = 0; i < 40; i++) {
			g.drawLine(40 * i, 0, 40 * i, 800);
			g.drawLine(0, 40 * i, 800, 40 * i);
		}
	}

	public void doStep(){
		calculateNextStep();
		for (int x = 0; x < 20; x++) {
			for (int y = 0; y < 20; y++) {
				cell[x][y].step();
			}
		}
	}
	
	public void run() {
		while (true) {
			if (start) {
				doStep();
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			repaint();
		}

	}

	public void setStart(boolean b) {
		start = b;
	}

	public void setCellAlive(int x, int y, boolean b) {
		cell[x][y].setAlive(true);
	}
}

class KeyController extends KeyAdapter {
	private GameOfLive gol;

	public KeyController(GameOfLive gol) {
		this.gol = gol;
	}

	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_SPACE) {
			gol.setStart(true);
		}
	}

	public void keyReleased(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_SPACE) {
			gol.setStart(false);
		}
	}
}

class MouseController extends MouseAdapter {
	private GameOfLive gol;

	public MouseController(GameOfLive gol) {
		this.gol = gol;
	}

	public void mousePressed(MouseEvent e) {
		int x = e.getX() / 40;
		int y = e.getY() / 40;
		gol.setCellAlive(x, y, true);
		System.out.println("X: " + x + " Y: " + y);
	}
}
Java:
package gameoflive;
 
import java.awt.Color;
import java.awt.Graphics;
 
public class Cell {
    
    private boolean alive, nextState;
    private int x, y;
    
    public Cell(boolean alive, int x, int y){
        this.alive = alive;
        this.x = x;
        this.y = y;
    }
    
    public boolean getAlive(){
        return alive;
    }
    
    public boolean nextState(){
        return nextState;
    }
    
    public void setAlive(boolean alive){
        this.alive = alive;
    }
    
    public void setNextState(boolean nextState){
        this.nextState = nextState;
    }
    
    public void paintCell(Graphics gr){
        if(alive){
            gr.setColor(Color.GREEN);
        }else{
            gr.setColor(Color.WHITE);
        }   
        gr.fillRect(x, y, 40, 40);
    }
     

	public void step() {
		alive = nextState;
		
	}   
    
}
 

Zurück
Oben