All pastes #14185 Raw Edit

Miscellany

public java v1 · immutable
#14185 ·published 2005-06-12 22:52 UTC
rendered paste body
import java.util.Random;public class Gol{    protected boolean alive;	protected boolean dead;		private static int xDim;	private static int yDim;	private static Cell[][] grid;	private static Cell[][] tempGrid;		public int type;		public Gol(int xDim, int yDim)    {        this.xDim = xDim;        this.yDim = yDim;                              initMatrix();    }        public void setLife(int k)	{		//if(grid.length != this.yDim)		//throw new IllegalArgumentException("Das Array abstraktesSpielfeld muß exakt dieselben Abmessungen haben wie das Spielfeld.");		for(int i = 0; i < xDim; i++)		{			//if(grid[i].length != this.xDim)			//throw new IllegalArgumentException("Das Array abstraktesSpielfeld muß exakt dieselben Abmessungen haben wie das Spielfeld.");						for(int j = 0; j < yDim; j++)			{				type = generateLife();												if(type >= 0 && type < k )				{					grid[i][j].setAlive(false);				}				else				{					grid[i][j].setAlive(true);				}			}		}	}		private int generateLife()	{		Random rn = new Random();		int number = rn.nextInt(10);		return number;			}			private void initMatrix()    {        this.grid = new Cell[yDim][xDim];                for(int i = 0; i < yDim; i++)        {            for(int j = 0; j < xDim; j++)            {                            	this.grid[i][j] = new Cell(j, i);                            }        }    }        public void playLifecycles(int n)    {       	System.out.println("StartGrid:");       	printMatrix();               	for(int i = 0; i < n; i++)        {            System.out.println("Generation " + (i + 1) + ":");            nextGeneration();            printMatrix();        }    }            private boolean checkRighttolive(int r, int c) 	{    	int neighbourCount = 0;		int tmp_i = 0;		int tmp_k = 0;        for(int i = -1; i <= 1; i++)         {			for(int k = -1; k <= 1; k++)             {				if(r+i > this.xDim)				{					tmp_i = 0;					}				else if(r+i < 0)				{					tmp_i = this.xDim-1;					}				else				{					tmp_i = r+i;					}							if(c+k > this.xDim-1)				{					tmp_k = 0;					}				else if(c+k < 0)				{					tmp_k = this.xDim-1;					}				else				{					tmp_k = c+k;					}								if(tmp_i == r && tmp_k == c) 				{                	continue;                    }								if(this.grid[tmp_i][tmp_k].isAlive() == true) 				{					neighbourCount++;					if(neighbourCount > 3)					{						return false;	                    }                }                               if(this.grid[r][c].isAlive() == true)               	{              		if(neighbourCount < 2 || neighbourCount == 3)                     {                    	return true;	                    }                }                else                 {                	if(neighbourCount == 3)          			{         				return true;         				}                }                                }          }return false;    }  	    public void nextGeneration()    {    	this.tempGrid = new Cell[xDim][yDim];    	    	   	        for(int i = 0; i < this.xDim; i++)        {            for(int j = 0; j < this.yDim; j++)            {            	Cell c = new Cell(i, j);            	c.setAlive(this.grid[i][j].alive);                this.tempGrid[i][j] = c;                }        }              	    	// Nächste Runde berechnen        for(int i = 0; i < this.yDim; i++)        {            for(int j = 0; j < this.xDim; j++)            {                if(checkRighttolive(i, j) == true)                   this.grid[i][j].setAlive(true);                else                   this.grid[i][j].setAlive(false);            }        }                        }			public void printMatrix()    {        System.out.println();        for(int i = 0; i < this.yDim; i++)        {            for(int j = 0; j < this.xDim; j++)            {                System.out.print(grid[i][j]);            }            System.out.println();        }        System.out.println();    }	        }