Java 给GOL增加随机性?
这是我的人生游戏代码。目前游戏是从一个种子文件中进行的,但是我希望添加随机性,这是我很难做到的。有人能提供一些意见吗Java 给GOL增加随机性?,java,conways-game-of-life,Java,Conways Game Of Life,这是我的人生游戏代码。目前游戏是从一个种子文件中进行的,但是我希望添加随机性,这是我很难做到的。有人能提供一些意见吗 class LifeGrid { public int[][] CG; //New 2d Array called Current Generation public int[][] NG; //New 2d Array called NextGeneration public int x, y, R
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[][]CG;//称为当前生成的新2d数组
public int[][]NG;//名为NextGeneration的新2d数组
公共整数x、y、行、列;
公共LifeGrid(int x,int y,字符串文件名)引发FileNotFoundException
{
File File=新文件(filename);//指向种子的新文件
Scanner Scan=新扫描仪(文件);//扫描种子文件
CG=new int[x][y];//使用变量x和y引用网格大小初始化CurrentGeneration
NG=new int[x][y];//使用变量x和y初始化下一代
this.x=x;//使x等于构造函数中的x
this.y=y;//使y在构造函数中等于y
System.out.println(“原始种子文件:”;//打印种子文件的标题
while(Scan.hasNextLine())//检查当前行之后是否有另一行
{
System.out.println(Scan.nextLine());//扫描下一行并将其打印到屏幕上
}
Scan.close();//关闭扫描仪
Scan=新扫描仪(文件);//重新打开扫描仪
while(Scan.hasNextLine())//检查当前行之后是否有另一行
{
Rows++;//将Rows变量递增1,直到没有要扫描的行为止
Scan.nextLine();//扫描下一行
}
Scan.close();
扫描=新扫描仪(文件);
String c=Scan.next();//初始化存储每个字符的新字符串变量c
Columns=c.length();//变量列(在顶部定义)等于包含字符的c的长度
System.out.println(“Seed中的列=”+Columns+“\n Seed中的行=”+Rows);//行,列
//^打印以筛选种子文件中的列数和行数
int-xOffset=((x-1)/2)-(行/2));int-yOffset=((y-1)/2)-(列/2));
//^将种子放置在栅格的中间
Scan.close();
扫描=新扫描仪(文件);
String ReadLine=Scan.nextLine();//初始化名为ReadLine的字符串,该字符串将每一行存储在文本文件中
对于(int i=xOffset;i查找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.
}
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
}
}
}
}
#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.
}