📄 gameoflife.java
字号:
// Built-on the original code from
//--------------------------------
// Game of Life v1.3
// Copyright 1996-2001 Edwin Martin <edwin@bitstorm.nl>
// version 1.0 online since July 3 1996
// Changes:
// 1.1: Double buffering to screen; faster paint
// 1.2: Arrowkeys changed; better use of `synchronized'
// 1.3: Choose speed from drop down menu and draw with mouse
//
//-------------Game of Life - Array version------------------
//-------------abraham kannankeril---------------------------
// 1.4: Added template 'Random'& 'Big Bang' to layout choices
//-------------------------xxx-------------------------------
// 2.0: Add Sugarscape & Citizen objects incorporating a new
// & more sophisticated ruleset.
// -> a single resource (sugar) is randomly dispersed on the grid (sugarscape)
// -> citizens scattered on the sugarscape must collect & consume sugar to ensure survival
// -> citizens possess attributes - vision, metabolism & sugar hoard
// System-level variables can be manually adjusted via public constants to influence
// behavior on the sugarscape.
// 2.1: Split into multiple files for better manageablity - package GameOfLife
// Added system variable DEBUG to switch debug info ON/OFF
// 2.2: Added visual representation of cell & citizen properties
// -> cells shaded to represent available sugar level (low / medium / high)
// -> citizen shaded to represent available sugar level (low / medium / high)
// -> citizen shape conveys representation of following combined properties
// - high vision, high metabolism
// - high vision, low metabolism
// - low vision, high metabolism
// - low vision, low metabolism
// 2.21 Improved sugar search routine to enable neutral selection between equally fertile cells
// and selection of random cell if no sugar available to citizen
// Added boolean variables to toggle debugging of selected program functions
// Added boolean flag to allow imposition of limits on sugar accumulation in cells and a
// corresponding method to depict absolute values of the sugar matrix
// Color cues implemented in visual display:
// -> Cell color darkens according to sugar accumulated in each cell
// -> Citizen color gradually varies from red to orange to yellow to green to represent
// the amount of sugar collected
// -> Citizen shape varies to depict one of four relative vision/metabolism combinations
// -> Grid coordinates can be printed if needed. This includes options to print all or
// some coordinates & to select a print color
// 2.22 All output from the program has now been redirected to a graphical textarea within the
// same window as the grid. The earlier dump into STDOUT has been discontinued.
// 2.23 Eliminated "Active Cell found unexpectedly" error
// Eliminated grid sugar renewal problem - now regenerated with new sugarscape
// Added debugging variable, DEBUG_PROGRAM_FLOW - enables tracing of method sequence
// Added command line interface to enable interactive control of program parameters
import GameOfLife.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
public class GameOfLife extends Applet implements Runnable,ActionListener
{
private CellSpace cellSpace;
private Thread gameThread = null;
private int genTime;
private int cellSize;
private int cellCols;
private int cellRows;
//**Initial templates for the Game of Life grid
private final String clear = "Clear";
private final String sugarscape = "Sugarscape";
private final String random = "Random";
private final String glider = "Glider";
private final String bigbang = "Big Bang";
private final String exploder1 = "Small Exploder";
private final String exploder2 = "Exploder";
private final String row10 = "10 Cell Row";
private final String fish = "Fish";
private final String pump = "Pump";
private final String gun = "Shooter";
//**Speed settings for the simulation
private final String hyper = "Hyper";
private final String fast = "Fast";
private final String slow = "Slow";
//**Labels for buttons
private final String nextLabel = "Next";
private final String startLabel = "Start";
private final String stopLabel = "Stop";
//**Program data display & interaction objects
private Button startstopButton;
private Label genLabel;
private TextField textField;
private TextArea textArea;
Choice c, speed;
public void init()
{ String param;
// set background
setBackground( new Color(140,150,140) );
// read parameters from HTML
param = getParameter("cellsize");
if ( param == null) { cellSize = GoLconst.GRID_CELLSIZE; }
else { cellSize = Integer.valueOf( param ).intValue(); }
param = getParameter("cellcols");
if ( param == null ) { cellCols = GoLconst.GRID_COLUMNS; }
else { cellCols = Integer.valueOf( param ).intValue(); }
param = getParameter("cellrows");
if ( param == null ) { cellRows = GoLconst.GRID_ROWS; }
else { cellRows = Integer.valueOf( param ).intValue(); }
param = getParameter("gentime");
if ( param == null ) { genTime = GoLconst.GRID_REFRESH_HYPER; }
else { genTime = Integer.valueOf( param ).intValue(); }
// create components and add them to container
cellSpace = new CellSpace( cellSize, cellCols, cellRows );
/*Choice*/ c = new Choice();
c.addItem( clear );
c.addItem( sugarscape );
c.addItem( random );
c.addItem( glider );
c.addItem( bigbang );
c.addItem( exploder2 );
c.addItem( exploder1 );
c.addItem( row10 );
c.addItem( fish );
c.addItem( pump );
c.addItem( gun );
/*Choice*/ speed = new Choice();
speed.addItem( hyper );
speed.addItem( fast );
speed.addItem( slow );
genLabel = new Label( "Generat's: 0 " );
startstopButton = new Button( startLabel );
if( GoLconst.TEXTAREA_HEIGHT > 15 || GoLconst.TEXTAREA_HEIGHT < 1)
{
if( GoLconst.DEBUG_CRITICAL_ERROR || GoLconst.DEBUG )
{ System.out.println("TEXTAREA_HEIGHT:Invalid value specified, assuming 3!!\n"); }
GoLconst.TEXTAREA_HEIGHT = 3;
}
//Textfield to accept commands for processing
textField = new TextField(cellCols*2+1);
textField.addActionListener(this);
//Textarea to display feedback to user
textArea = new TextArea(GoLconst.TEXTAREA_HEIGHT , cellCols*2+1);
textArea.setEditable(false);
//Chk global constantfor invalid values
writeTextArea( GoLconst.chkGoLconstants() );
Panel controls = new Panel();
controls.add( c );
controls.add( new Button( nextLabel ));
controls.add( startstopButton );
controls.add( speed );
controls.add( genLabel );
Panel area = new Panel();
area.setLayout(new BorderLayout());
//area.add("North",new Label("Sugarscape Statsheet"));
area.add("Center",textArea);
area.add("South",textField);
setLayout(new BorderLayout());
add( "Center", controls );
add( "North", cellSpace);
add( "South", area );
show();
resize( preferredSize() );
validate();
}
// no start() to prevent starting immediately
public void start2() {
if(gameThread == null) {
gameThread = new Thread(this);
gameThread.start();
}
}
public void stop() {
if(gameThread != null) {
gameThread.stop();
gameThread = null;
}
}
public void run() {
while (gameThread != null)
{ int FEEDBACK_SUMMARY = 2; //provide feedback in summary format
if( GoLconst.flagSugarscape )
{
writeTextArea( cellSpace.nextScape(FEEDBACK_SUMMARY) );
//display sugar distribution
if( GoLconst.DEBUG || GoLconst.DEBUG_CELLSCAPE_SUGAR )
{ writeTextArea( cellSpace.showSugarscapeStats(0,0,cellCols,cellRows) ); }
}
else cellSpace.next();
cellSpace.repaint();
showGenerations();
try { gameThread.sleep( genTime );
} catch (InterruptedException e){}
}
}
public boolean action(Event evt, Object arg) {
if( clear.equals( arg ) ) // clear the grid
{ cellSpace.clear();
cellSpace.repaint();
showGenerations();
GoLconst.flagSugarscape = false;
return true;
}
else if( sugarscape.equals( arg ) ) // generate Sugarscape
{ // Note: there are 2 drawSugarscape functions
// GameOfLife.drawSugarscape() & Cellspace.drawSugarscape()
GoLconst.flagSugarscape = true;
drawSugarscape(); // GameOfLife.drawSugarscape()
return true;
}
else if( random.equals( arg ) ) // generate random shape
{ GoLconst.flagSugarscape = false;
drawRandomShape( );
return true;
}
else if( glider.equals( arg ) ) // misc shapes
{
GoLconst.flagSugarscape = false;
int shape[] = { 0,1, 1,2, 2,2, 2,1, 2,0 };
drawShape( 3, 3, shape );
return true;
}
else if( bigbang.equals( arg ) ) // misc shapes
{
GoLconst.flagSugarscape = false;
int shape[] = { 0,0, 0,1, 0,2, 0,4, 0,5, 0,6,
1,0, 1,6,
2,1, 2,5,
4,1, 4,5,
5,0, 5,6,
6,0, 6,1, 6,2, 6,4, 6,5, 6,6 };
drawShape( 7, 5, shape );
return true;
}
else if( exploder1.equals( arg ) )
{
GoLconst.flagSugarscape = false;
int shape[] = { 0,1, 0,2, 1,0, 1,1, 1,3, 2,1, 2,2 };
drawShape( 3, 4, shape );
return true;
}
else if(exploder2.equals(arg))
{
GoLconst.flagSugarscape = false;
int shape[] = { 0,0, 0,1, 0,2, 0,3, 0,4, 2,0, 2,4, 4,0, 4,1, 4,2, 4,3, 4,4 };
drawShape( 5, 5, shape );
return true;
}
else if(row10.equals(arg))
{
GoLconst.flagSugarscape = false;
int shape[] = { 0,0, 1,0, 2,0, 3,0, 4,0, 5,0, 6,0, 7,0, 8,0, 9,0 };
drawShape( 10, 1, shape );
return true;
}
else if(fish.equals(arg))
{
GoLconst.flagSugarscape = false;
int shape[] = { 0,1, 0,3, 1,0, 2,0, 3,0, 3,3, 4,0, 4,1, 4,2 };
drawShape( 5, 4, shape );
return true;
}
else if(pump.equals(arg))
{
GoLconst.flagSugarscape = false;
int shape[] = { 0,3, 0,4, 0,5, 1,0, 1,1, 1,5, 2,0, 2,1, 2,2, 2,3, 2,4, 4,0, 4,1, 4,2, 4,3, 4,4, 5,0, 5,1, 5,5, 6,3, 6,4, 6,5 };
drawShape( 7, 6, shape );
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -