?? world.java
字號:
package gameoflife;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import com.axon7.jibu.CancelException;
import com.axon7.jibu.IForTask;
import com.axon7.jibu.Parallel;
enum CellAction { NONE, KILL, GIVE_LIFE }
enum CellState { DEAD, ALIVE }
class Cell
{
private CellAction action = CellAction.NONE;
private CellState state = CellState.DEAD;
public CellAction getCellAction()
{
return action;
}
public void setCellAction(CellAction action)
{
this.action = action;
}
public CellState getState()
{
return state;
}
public void setState(CellState state)
{
this.state = state;
}
}
public class World
{
private Cell[][] grid;
private int width, height;
private static final Color GRIDCOLOR = Color.WHITE;
private static final Color ALIVECOLOR = Color.ORANGE;
private static final Color DEADCOLOR = Color.GRAY;
public World(int width, int height)
{
this.width = width;
this.height = height;
grid = new Cell[width][height];
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
grid[x][y] = new Cell();
}
}
}
/**************************************
* Finds the state that a given cell
* will be in in the next generation.
**************************************/
private void findNextState(int x, int y)
{
int alive = 0;// (x,y)周圍8個格子存活的數目
int xBegin = (x == 0 ? x : x - 1);
int xEnd = (x == width-1 ? x : x + 1);
int yBegin = (y == 0 ? y : y - 1);
int yEnd = (y == height-1 ? y : y + 1);
for(int xLook = xBegin ; xLook <= xEnd ; xLook++)
{
for(int yLook = yBegin; yLook <= yEnd; yLook++)
{
if((x != xLook || y != yLook) && grid[xLook][yLook].getState() == CellState.ALIVE)
alive++;
}
}
Cell c = grid[x][y];
switch(c.getState())
{
case ALIVE:
if(alive == 2 || alive == 3)
c.setCellAction(CellAction.NONE);
else
c.setCellAction(CellAction.KILL);
break;
case DEAD:
if(alive == 3)
c.setCellAction(CellAction.GIVE_LIFE);
else
c.setCellAction(CellAction.NONE);
break;
}
}
/**************************************
* Changes the state for a cell.
**************************************/
private void changeState(int x, int y)
{
Cell c = grid[x][y];
switch(c.getCellAction())
{
case KILL:
c.setState(CellState.DEAD);
break;
case GIVE_LIFE:
c.setState(CellState.ALIVE);
break;
case NONE:
break;
}
}
/*********************************************
* Sets the state of a single cell to ALIVE.
*********************************************/
public void setAlive(int x, int y)
{
if((x >= 0 && x < width) && (y >= 0 && y < height))
grid[x][y].setState(CellState.ALIVE);
}
/**************************************
* Transitions to the next iteration
* of life.
* Sequential version.
**************************************/
public void sequentialTransition()
{
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
findNextState(x, y);
}
}
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
changeState(x, y);
}
}
}
/**************************************
* Transitions to the next iteration of life.
* Parallel version.
**************************************/
public void parallelTransition() throws CancelException
{
Parallel.forLoop(0, width, 10, new IForTask()
{
public void loopBody(int x)
{
for(int y = 0; y < height; y++)
{
findNextState(x, y);
}
}
});
Parallel.forLoop(0, width, 10, new IForTask()
{
public void loopBody(int x)
{
for(int y = 0; y < height; y++)
{
changeState(x, y);
}
}
});
}
/**************************************
* Draws the world to a BufferedImage instance.
* Parallel version.
**************************************/
public void parallelImageDraw(final BufferedImage img, final int cellSize) throws CancelException
{
Graphics2D g2d = img.createGraphics();
g2d.setColor(GRIDCOLOR);
g2d.fillRect(0, 0, width * cellSize, height * cellSize);
Parallel.forLoop(0, width, 10, new IForTask()
{
public void loopBody(int x)
{
Graphics2D g2d = img.createGraphics();
for(int y = 0; y < height; y++)
{
if(grid[x][y].getState() == CellState.ALIVE)
{
g2d.setColor(ALIVECOLOR);
g2d.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
else
{
g2d.setColor(DEADCOLOR);
g2d.drawRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
}
}
});
}
/************************************************
* Draws the world to a BufferedImage instance.
* Sequential version.
************************************************/
public void sequentialImageDraw(BufferedImage img, final int cellSize)
{
final Graphics2D g2d = img.createGraphics();
g2d.setColor(GRIDCOLOR);
g2d.fillRect(0, 0, width * cellSize, height * cellSize);
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
if(grid[x][y].getState() == CellState.ALIVE)
{
g2d.setColor(ALIVECOLOR);
g2d.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
else
{
g2d.setColor(DEADCOLOR);
g2d.drawRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
}
}
}
/**************************************
* Draws the world to a the console.
**************************************/
public void consoleDraw()
{
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
Cell c = grid[x][y];
if(c.getState() == CellState.ALIVE)
System.out.print(" o ");
else
System.out.print(" x ");
}
System.out.println();
}
System.out.println();
}
/*******************************************
* Adds an array of LifeForms to the world.
*******************************************/
public void addLifeForms(LifeForm[] lifeForms)
{
for(LifeForm lf : lifeForms)
{
int xOffset = lf.getOffset().x;
int yOffset = lf.getOffset().y;
for(Point p : lf.getPoints())
{
setAlive(p.x + xOffset, p.y + yOffset);
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -