对于一个学校项目,我必须使用一些提供的骨架代码来完成Conways的生活游戏。
我的问题是,当我使用可视化工具时,我的所有细胞似乎都已初始化(因此不会活跃起来) - 我不是在寻找整个项目的直接答案,只是在某个方向上我的代码坏了(为可怕的格式化道歉,这是我第一次来这里):
以下是Cell.class
及以下CellGrid
- 提供了可视化工具。
package simulation;
/**
* This class represents a single Cell in the grid.
*
* Complete this class as part of the core of the assessment.
* You must implement the constructor, isAlive, setAlive and isAliveNextStep.
*/
public class Cell
{
// true if the cell is alive and false if it is dead
private boolean alive;
/**
* Cell constructor - all cells should start out dead
*/
public Cell()
{
alive = false;
}
/**
* Accessor method for alive
*
* @return true if the cell is currently alive; false otherwise
*/
public boolean isAlive()
{
if (alive == true)
{
return true;
}
else
return false;
}
/**
* Mutator method for alive
*
* @param alive - the new state of the cell
*/
public void setAlive(boolean alive)
{
if (alive == true)
alive = false;
else alive = true;
}
/**
* Determine whether this cell should be alive in the next step,
* given the number of surrounding neighbours.
*
* See the assignment specification sheet to determine the rules
* for living and dead cells.
*
* @param numNeighbours - the number of living cells surrounding this cell
* @return true if the cell should be alive; false otherwise
*/
public boolean isAliveNextStep(int numNeighbours)
{
if (numNeighbours <= 2)
return false;
if (numNeighbours == 3)
return true;
if (numNeighbours == 4 && alive == true)
return true;
if (numNeighbours == 5)
return false;
if (numNeighbours > 5)
return true;
else return false;
}
}
CellGrid类:
package simulation;
/**
* This class represents an n x n grid of Cells.
*
* Complete this class as part of the core of the assessment.
* You must implement the constructor, simulateStep, isValidCoordinate,
* countNeighbours, getCell and setCell.
*/
public class CellGrid
{
// Store the cells of the game in this 2D array
private Cell[][] cells;
/**
* Constructor for a CellGrid. Populates the grid with cells that will be
* either living or dead. Consider using Math.random() in order to generate
* random numbers between 0.0 and 1.0, in conjunction with lifeChance.
*
* @param size - the size of the grid will be size x size
* @param lifeChance - the probability of each cell starting out
* alive (0.0 = 0%, 1.0 = 100%)
*/
public CellGrid(int size, double lifeChance)
{
cells = new Cell[size][size];
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cells[i][j] = new Cell();
if (Math.random() < lifeChance);
{
cells[i][j].setAlive(false);
}
}
}
}
/**
* Run one step in the simulation. This has 2 stages in the following order:
*
* 1. (Core) Update all cells in the grid according to the rules given in the
* assignment specification sheet.
*
* 2. (Extension) Evolve the cells by calculating their new genes - also
* see the assignment specification sheet.
*/
public void simulateStep()
{
for (int i = 0; i < cells.length; i++)
{
for (int j = 0; j < cells.length; j++)
{
int Neighbours = countNeighbours(i, j);
cells[i][j].isAliveNextStep(Neighbours);
}
}
}
/**
* Check if the given coordinates are inside the grid of cells.
*
* @param x - the x coordinate (column) of the cell
* @param y - the y coordinate (row) of the cell
* @return true if the given coordinates are inside the grid of cells; false
* otherwise.
*/
public boolean isValidCoordinate(int x, int y)
{
int validc = 0; //*variable to check for validity of coordinate by traversal *//
for (int i = 0; i < cells.length; i++)
{
for (int j = 0; j > cells.length; j++)
{
if (x == i+1 && y == j+1)
{
validc = 1;
}
}
}
if (validc == 1)
{
return true;
}
else return false;
}
/**
* Count the number of living neighbours in the 8 cells surrounding the
* given coordinates.
*
* @param x - the x coordinate (column) of the cell
* @param y - the y coordinate (row) of the cell
* @return the number of living neighbours of the cell at the given
* coordinates; or 0 if the coordinates are invalid.
*/
public int countNeighbours(int x, int y)
{
int N = 0;
for (int i = 0; i < cells.length; i++)
{
for (int j = 0; j > cells.length; j++)
{
if (i-1 >= 0 && j-1 >= 0 && cells[i-1][j-1].equals(true))
N++;
if (i-1 >= 0 && cells[i-1][j].equals(true))
N++;
if (i-1 >= 0 && j+1 <= cells.length && cells[i-1][j+1].equals(true))
N++;
if (i >= 0 && j-1 >=0 && cells[i][j-1].equals(true))
N++;
if (i >= 0 && j >= 0 && cells[i][j].equals(true))
N++;
if (i >= 0 && j+1 <= cells.length && cells[i][j+1].equals(true))
N++;
if (i+1 <= cells.length && j-1 >= 0 && cells[i+1][j-1].equals(true))
N++;
if (i+1 <= cells.length && j >= 0 && cells[i+1][j].equals(true))
N++;
if (i+1 <= cells.length && j+1 <= cells.length && cells[i+1][j+1].equals(true))
N++;
}
}
return N;
}
/**
* Get the cell at the given coordinates.
*
* @param x - the x coordinate (column) of the cell
* @param y - the y coordinate (row) of the cell
* @return the cell at the given coordinates; or null if the coordinates are
* invalid
*/
public Cell getCell(int x, int y)
{
if (x < cells.length && y < cells.length)
return cells[x][y];
else return null;
}
/**
* Set the cell at the given coordinates to the cell provided, if the
* coordinates are valid.
*
* @param x - the x coordinate (column) of the cell
* @param y - the y coordinate (row) of the cell
* @param cell - the new cell to put at the coordinates given.
*/
public void setCell(int x, int y, Cell cell)
{
cells[x][y] = getCell(x,y);
}
}
答案 0 :(得分:0)
您的setAlive
方法不正确。
public void setAlive(boolean alive){
if (alive == true)
alive = false;
else alive = true;
}
由于variable shadowing,您永远不会真正更改字段alive
。它应该是这样的:
public void setAlive(boolean alive){
this.alive = alive;
}