📄 gameoflife.java
字号:
else if(gun.equals(arg))
{
GoLconst.flagSugarscape = false;
int shape[] = { 0,2, 0,3, 1,2, 1,3, 8,3, 8,4, 9,2, 9,4, 10,2, 10,3, 16,4, 16,5, 16,6, 17,4, 18,5, 22,1, 22,2, 23,0, 23,2, 24,0, 24,1, 24,12, 24,13, 25,12, 25,14, 26,12, 34,0, 34,1, 35,0, 35,1, 35,7, 35,8, 35,9, 36,7, 37,8 };
drawShape( 38, 15, shape );
return true;
}
else if(nextLabel.equals(arg)) // next
{
int FEEDBACK_DETAILED = 0; //provide feedback detail for each citizen
if(GoLconst.flagSugarscape)
writeTextArea( cellSpace.nextScape(FEEDBACK_DETAILED) );
else
cellSpace.next();
cellSpace.repaint();
showGenerations();
return true;
}
else if(startLabel.equals(arg)) // start
{
start2();
startstopButton.setLabel( stopLabel );
return true;
}
else if(stopLabel.equals(arg)) // stop
{
stop();
startstopButton.setLabel( startLabel );
return true;
}
else if(slow.equals(arg)) // slow
{
genTime = GoLconst.GRID_REFRESH_SLOW;
return true;
}
else if(fast.equals(arg)) // fast
{
genTime = GoLconst.GRID_REFRESH_FAST;
return true;
}
else if(hyper.equals(arg)) // hyperspeed
{
genTime = GoLconst.GRID_REFRESH_HYPER;
return true;
}
return false;
}
public String getAppletInfo()
{
return "Game Of Life v. 1.3\nCopyright 1996-2001 Edwin Martin"
+ "\nAdditional variations: abraham kannankeril 2003"
+ "\n\tver. 2.0 implements 'The Sugarscape', based on the book"
+ "\n\t'Growing Artificial Societies by Joshua M. Epstein & Robert Axtell";
}
// show number of generations
public void showGenerations() {
if( GoLconst.flagSugarscape )
genLabel.setText( "Gen'rtn: " + cellSpace.generations
+ " Pop'ltn: " + cellSpace.population );
else
genLabel.setText( "Gen'rtn: " + cellSpace.generations );
}
// draws the shape to canvas
public void drawShape( int shapeWidth, int shapeHeight, int shape[] )
{ if ( !cellSpace.drawShape( shapeWidth, shapeHeight, shape ) )
showStatus( "Shape is too big to fit." );
else
{ showStatus( "" );
cellSpace.repaint();
showGenerations();
}
}
// draws the shape to canvas
public void drawRandomShape( )
{ cellSpace.drawRandomShape( );
showStatus( "Random grid generated!" );
cellSpace.repaint();
showGenerations();
}
public synchronized void drawSugarscape( )
{ writeTextArea( cellSpace.drawSugarscape() );
//writeTextArea("Initial Sugarscape population " + cellSpace.population + "\n");
showStatus("Initial Sugarscape population " + cellSpace.population);
showGenerations();
cellSpace.repaint();
}
public void writeTextArea(String text)
{ textArea.append(text); }
/***This Method implements command-line control for the program***/
public void actionPerformed(ActionEvent evt)
{ String text = textField.getText();
text = text.toUpperCase();
//**Clear Grid or the Display area**
if( text.startsWith("CLEAR") )
{
if( text.equals("CLEAR GRID") )
{
c.select( clear );
cellSpace.clear();
cellSpace.repaint();
showGenerations();
GoLconst.flagSugarscape = false;
textArea.append("Sugarscape cleared\n");
}
else if( text.equals("CLEAR TEXT") )
{ textArea.setText(""); }
else { textArea.append("Clear what?\n"); }
}
//**Control Program Pace
else if( text.equals("HYPER") || text.equals("H") )
{ genTime = GoLconst.GRID_REFRESH_HYPER;
speed.select( hyper );
textArea.append("Switching to Hyper mode!\n");
}
else if( text.equals("FAST") || text.equals("F") )
{ genTime = GoLconst.GRID_REFRESH_FAST;
speed.select( fast );
textArea.append("Switching to Fast mode!\n");
}
else if( text.equals("SLOW") || text.equals("S") )
{ genTime = GoLconst.GRID_REFRESH_SLOW;
speed.select( slow );
textArea.append("Switching to Slow mode!\n");
}
else if( text.equals("STOP") || text.equals("X") )
{ gameThread.stop();
gameThread = null;
startstopButton.setLabel( startLabel );
textArea.append("Execution paused, type 'START' to continue.\n");
}
else if( text.equals("START") || text.equals("GO") ) // start
{
start2();
startstopButton.setLabel( stopLabel );
textArea.append("Execution started/resumed, type '?' for help.\n");
}
else if( text.equals("NEXT") || text.equals("+") ) // start
{
int FEEDBACK_DETAILED = 0; //provide feedback detail for each citizen
if(GoLconst.flagSugarscape)
writeTextArea( cellSpace.nextScape(FEEDBACK_DETAILED) );
else
cellSpace.next();
//cellSpace.repaint();
//showGenerations();
writeTextArea("Ok\n");
}
//**Provide General Help with Interactive Commands**
else if( text.equals("HELP") || text.equals("?") )
{ textArea.append(
"\n\nSugarscape Command-line Options\n" +
"=============================\n" +
"START / GO \t Starts the program run\n" +
"STOP / X \t Pause the program run\n" +
"CLEAR \t Stops the run & clears the grid\n" +
"H / F / S / X\t Controls program pace: Hyper / Fast / Slow / Pause\n" +
"? / HELP \t Display this screen\n" +
"VERSION / INFO\t Display Program Information\n" +
"\nDebugging Program Execution\n" +
"--------------------------------------\n" +
"DEBUG CRITICAL ERROR\tTracks program inconsistencies\n" +
"DEBUG\t\t\t\tTracks all events in a program\n" +
"DEBUG PROGRAM FLOW\t\tTraces program process flow\n" +
"DEBUG CREATE CITIZEN\t\tTraces 'Birth' processes\n" +
"DEBUG CITIZEN DEATH\t\tTraces 'Death' processes\n" +
"DEBUG SEARCH SUGAR\t\tTraces each citizen's search for sugar\n" +
"DEBUG CELLSCAPE SUGAR\tDisplay sugar distribution on Cellscape\n" +
"DEBUG CITIZEN DETAIL\t\tDisplay Population/Citizen details\n"
);
textArea.append("-----------------------------------------------" +
"-----------------------------------------------\n" +
"Note: Commands are case-insensitive.\nDebugging options will slow down " +
"program, especially the 'DEBUG' option\n" );
}
//**Provide Version Info**
else if( text.equals("VERSION") || text.equals("INFO") )
{ textArea.append( "Sugarscape Program Info\n"
+ "-----------------------------------"
+ "\nVersion\t\t: " + GoLconst.PROGRAM_VERSION
+ "\nRelease Date\t: " + GoLconst.PROGRAM_RELEASE_DATE + "\n\n"
);
}
//**Display Feedback For Specific Processes**
else if( text.startsWith("DEBUG") )
{ if( text.equals("DEBUG CRITICAL ERROR") )
{
GoLconst.DEBUG_CRITICAL_ERROR = !GoLconst.DEBUG_CRITICAL_ERROR;
textArea.append("DEBUG_CRITICAL_ERROR = "
+ GoLconst.DEBUG_CRITICAL_ERROR + "\n");
}
else if( text.equals("DEBUG PROGRAM FLOW") )
{ GoLconst.DEBUG_PROGRAM_FLOW = !GoLconst.DEBUG_PROGRAM_FLOW;
textArea.append("DEBUG_PROGRAM_FLOW = "
+ GoLconst.DEBUG_PROGRAM_FLOW + "\n");
}
else if( text.equals("DEBUG CREATE CITIZEN") )
{ GoLconst.DEBUG_CREATE_CITIZEN = !GoLconst.DEBUG_CREATE_CITIZEN;
textArea.append("DEBUG_CREATE_CITIZEN = "
+ GoLconst.DEBUG_CREATE_CITIZEN + "\n");
}
else if( text.equals("DEBUG CITIZEN DEATH") )
{ GoLconst.DEBUG_CITIZEN_DEATH = !GoLconst.DEBUG_CITIZEN_DEATH;
textArea.append("DEBUG_CITIZEN_DEATH = "
+ GoLconst.DEBUG_CITIZEN_DEATH + "\n");
}
else if( text.equals("DEBUG SEARCH SUGAR") )
{ GoLconst.DEBUG_SEARCH_SUGAR = !GoLconst.DEBUG_SEARCH_SUGAR;
textArea.append("DEBUG_SEARCH_SUGAR = "
+ GoLconst.DEBUG_SEARCH_SUGAR + "\n");
}
else if( text.equals("DEBUG CELLSCAPE SUGAR") )
{ GoLconst.DEBUG_CELLSCAPE_SUGAR = !GoLconst.DEBUG_CELLSCAPE_SUGAR;
textArea.append("DEBUG_CELLSCAPE_SUGAR = "
+ GoLconst.DEBUG_CELLSCAPE_SUGAR + "\n");
}
else if( text.equals("DEBUG CITIZEN DETAIL") )
{ GoLconst.DEBUG_CITIZEN_DETAIL = !GoLconst.DEBUG_CITIZEN_DETAIL;
textArea.append("DEBUG_CITIZEN_DETAIL = "
+ GoLconst.DEBUG_CITIZEN_DETAIL + "\n");
}
else if( text.equals("DEBUG") )
{ GoLconst.DEBUG = !GoLconst.DEBUG;
textArea.append("DEBUG = " + GoLconst.DEBUG + "\n");
}
else
{ textArea.append("debug what?\n"); }
}
else if( text.startsWith("TEXTAREA")) // adjust display area
{ String operand="", numStr="";
int num=0, opIndex;
if( text.length() > 8 )
{
opIndex = text.indexOf("+");
opIndex = (opIndex < 0) ? text.indexOf("-") : opIndex;
operand = text.substring( opIndex,opIndex+1 );
//textArea.append("Operand: " + operand +"("+operand.length()+")"+ "\n");
if( operand.equals("+") || operand.equals("-") )
{
numStr = text.substring(opIndex+1).trim();
numStr = numStr.equals("") ? numStr = "1" : numStr;
//textArea.append("Num String: " + numStr
// +"("+numStr.length()+")"+ "\n");
num = Integer.valueOf(numStr).intValue();
} else
{ operand = "+";
num = 0;
textArea.append("Invalid operand!\n");
}
}
if( operand.equals("+") )
{ GoLconst.TEXTAREA_HEIGHT += num; }
else if( operand.equals("-") )
{ GoLconst.TEXTAREA_HEIGHT -= num; }
textArea.setRows(GoLconst.TEXTAREA_HEIGHT);
//textArea.setColumns(cellCols*2+1);
textArea.append("TEXTAREA DIMENSIONS = "
+ textArea.getRows() + " rows by "
+ textArea.getColumns() + " cols\n");
resize( preferredSize() );
}
else if( text.equals("SUGARSCAPE") || text.equals("SS") )
{ // Note: there are 2 drawSugarscape functions
// GameOfLife.drawSugarscape() & Cellspace.drawSugarscape()
c.select( sugarscape );
GoLconst.flagSugarscape = true;
drawSugarscape();
}
else
{ textArea.append(text + "?\n"); }
//textField.selectAll();
textField.setText("");
cellSpace.repaint();
showGenerations();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -