这是我的生命游戏代码。目前游戏是从一个种子文件播放,但我想添加随机化,我正在努力做。有人可以提供一些意见吗?
class LifeGrid
{
public int[][] CG; //New 2d Array called Current Generation
public int[][] NG; //New 2d Array called NextGeneration
public int x, y, Rows, Columns;
public LifeGrid(int x, int y, String filename) throws FileNotFoundException
{
File file = new File(filename); //New File that directs to Seed
Scanner Scan = new Scanner(file); //Scans the seed file
CG = new int[x][y]; //Initialize CurrentGeneration with variables x and y referring to grid size
NG = new int[x][y]; //Initialize NextGeneration with variables x and y
this.x = x; //Makes x equal to x in constructor
this.y = y; //Makes y equal to y in constructor
System.out.println("Original Seed File: "); //Prints the title for Seed File
while(Scan.hasNextLine()) //Checks if there is another line after current line
{
System.out.println(Scan.nextLine()); //Scans each next line and prints it to screen
}
Scan.close(); //Closes the scanner
Scan = new Scanner(file); //Reopens the scanner
while(Scan.hasNextLine()) //Checks if there is another line after current line
{
Rows++; //increments Rows variable by 1 until there is no line to scan
Scan.nextLine(); //Scans each next line
}
Scan.close();
Scan = new Scanner(file);
String c = Scan.next(); //Initializing a new String variable c that stores each character
Columns = c.length(); //Variable column (defined on top) equals the length of C that contains characters
System.out.println("Columns in Seed = " + Columns + "\nRows in Seed = " + Rows); //Rows,Columns
//^ Prints to screen Number of columns and rows in the seed file
int xOffset = (((x-1)/2) - (Rows/2)); int yOffset = (((y-1)/2) - (Columns/2));
//^ Places the seed into the middle of the grid
Scan.close();
Scan = new Scanner(file);
String ReadLine = Scan.nextLine(); //Initializing a string called ReadLine that stores each line in text file
for(int i=xOffset; i<x; i++) //for loop that starts at the offset
{
for(int j=yOffset, k=0;k<ReadLine.length(); j++, k++)
{
if(ReadLine.charAt(k)=='*') //Checks if current line contains the character '*'
{
CG[i][j] = 1; //For all characters equal to '*', cell at i,j, equals 1
}
}
if(Scan.hasNextLine() == false) //If there are no more lines left to scan
break; // then stop
else //Otherwise
ReadLine = Scan.nextLine(); //Continue reading the next line
}
}
生活游戏检查邻居
public int Neighbours(int xInput, int yInput) //Creates a new method called Neighbours that uses x and y inputs from 'LifeMain'
{
xInput -= 1; yInput -= 1; // x and y inputs equals x and y inputs minus 1
int neighbour = 0; int incrementer = 0; //Creates new variables neighbour and incrementer
int iMinus = -1, kMinus = -1, iPositive= 1, kPositive = 1;
if(yInput == 0) //if the y input (from grid) is zero
kMinus = 1; //Then make the K value equal to 1
if(xInput == 0) //if the xInput is equal to zero
iMinus = 1; //set iMinus to 1
if(yInput + 1 == x) //If the yInput + 1 is equal to the x value
kPositive = 0; //set kPositive equal to zero
if(xInput + 1 == y) //If the xInput + 1 is equal to the y value
iPositive = 0; //set iPositive equal to zero
for(int c = kMinus; c <= kPositive; c++) //for loop to start at -1 and increment but to remain less than or equal to 1
{
if(c == 0)
incrementer = 2;
else
incrementer = 1;
for(int row = iMinus; row <= iPositive; row+=incrementer)
{
if(CG[yInput + c][xInput + row] == 1)
neighbour++; //Increment neighbour when the cell in Current Gen equals 1
}
}
return neighbour;
}
public void show() //Method to Show Details
{
for(int i=0; i<y; i++) //Scans rows
{
for(int k=0; k<x; k++) //Scans columns
{
if(CG[i][k] == 1) //Checks which cells are equal to 1
{
System.out.print("[*]"); //Replaces all cells containing 1 to [*]
}
else
{
System.out.print("[ ]"); //Cells with zero are replaced with empty grid
}
}
System.out.println("");
}
System.out.println("");
}
生命游戏规则
public void Run() //Life Rules
{
for(int Row=1; Row<CG.length+1; Row++)
{
for(int Column=1; Column<CG[0].length+1; Column++)
{
if(CG[Row-1][Column-1] == 1 && Neighbours(Column,Row) < 2)
{
NG[Row-1][Column-1] = 0;
}
else if(CG[Row-1][Column-1] == 1 && Neighbours(Column,Row) >3)
{
NG[Row-1][Column-1]=0;
}
else if(CG[Row-1][Column-1] == 1 && (Neighbours(Column,Row) == 2 || Neighbours(Column,Row) == 3))
{
NG[Row-1][Column-1]=1;
}
else if(CG[Row-1][Column-1] == 0 && Neighbours(Column,Row) == 3)
{
NG[Row-1][Column-1] =1;
}
}
}
show();
for(int i=0; i<y; i++) //r = row. This scans the rows
{
for(int k=0; k<x; k++) //c = column. This scans the columns
{
CG[i][k] = NG[i][k]; //Makes the current generation equal to the next generation to print
}
}
}
}
答案 0 :(得分:0)
查找java.util.Random
。您可能希望针对人口密度测试nextDouble()
。
通过活细胞的密度(空白为0.0,实体为1.0)定义“随机”空间是正常的。
编写构造函数:
#import java.util.Random;
//...
public LifeGrid(int x, int y, double density, Random rand){
//Initialise - same a file reading constructor
//Fill - 2 nested loops calling rand.
}