📄 evolve.java
字号:
createNewBug ( SPACE_WIDTH / 2, SPACE_HEIGHT / 2, i );
}
setAllFloraPresent ( true );
edenCheckBox.setSelected ( true );
growthRateSpinnerNumberModel.setValue (
new Integer ( INIT_GROWTH_RATE ) );
}
private void createNewBug ( int x, int y )
//////////////////////////////////////////////////////////////////////
{
int i = indexOfFirstDeadBug ( );
if ( i > -1 )
{
createNewBug ( x, y, i );
}
}
private void createNewBug ( int x, int y, int index )
//////////////////////////////////////////////////////////////////////
{
Bug bug = bugs [ index ];
if ( bug == null )
{
bug = new Bug ( );
bug.genesX = new boolean [ GENES_MAX ];
bug.genesY = new boolean [ GENES_MAX ];
bugs [ index ] = bug;
}
bug.x = x;
bug.y = y;
bug.energy = BABY_ENERGY;
for ( int j = 0; j < GENES_MAX; j++ )
{
bug.genesX [ j ] = random.nextBoolean ( );
bug.genesY [ j ] = random.nextBoolean ( );
}
setBugColor ( bug );
}
private void setBugColor ( Bug bug )
//////////////////////////////////////////////////////////////////////
{
int xcount = 0;
int ycount = 0;
for ( int i = 0; i < GENES_MAX; i++ )
{
if ( bug.genesX [ i ] )
{
xcount++;
}
if ( bug.genesY [ i ] )
{
ycount++;
}
}
Color color = NORMAL_COLOR;
if ( ( xcount == GENES_MAX / 2 )
&& ( ycount == GENES_MAX / 2 ) )
{
color = TWIRLER_COLOR;
}
else if (
( xcount == 0 )
|| ( xcount == GENES_MAX )
|| ( ycount == 0 )
|| ( ycount == GENES_MAX ) )
{
color = CRUISER_COLOR;
}
bug.color = color;
}
private static String createStatusString (
int bugs_alive,
int time,
String genesAverageString )
//////////////////////////////////////////////////////////////////////
{
return "Alive: " + bugs_alive
+ " Time: " + time + " "
+ "Average Movement Genes " + genesAverageString;
}
private void giveBirth ( Bug parentBug )
//////////////////////////////////////////////////////////////////////
{
int index = indexOfFirstDeadBug ( );
if ( index < 0 )
{
return;
}
parentBug.energy -= BIRTH_ENERGY_COST;
Bug babyBug = bugs [ index ];
babyBug.energy = BABY_ENERGY;
babyBug.x = parentBug.x;
babyBug.y = parentBug.y;
for ( int i = 0; i < GENES_MAX; i++ )
{
babyBug.genesX [ i ] = parentBug.genesX [ i ];
babyBug.genesY [ i ] = parentBug.genesY [ i ];
}
int mutantGene = random.nextInt ( GENES_MAX );
if ( random.nextBoolean ( ) )
{
babyBug.genesX [ mutantGene ] = !parentBug.genesX [ mutantGene ];
}
else
{
babyBug.genesY [ mutantGene ] = !parentBug.genesY [ mutantGene ];
}
setBugColor ( babyBug );
}
private int indexOfFirstDeadBug ( )
//////////////////////////////////////////////////////////////////////
{
for ( int i = 0; i < bugs.length; i++ )
{
if ( bugs [ i ].energy <= 0 )
{
return i;
}
}
return -1;
}
private void moveBugs ( )
//////////////////////////////////////////////////////////////////////
{
time++;
if ( time >= GENES_MAX )
{
time = 0;
}
for ( int i = 0; i < bugs.length; i++ )
{
Bug bug = bugs [ i ];
int x = bug.x;
int y = bug.y;
if ( bug.energy > 0 )
{
if ( flora_present [ x ] [ y ] )
{
flora_present [ x ] [ y ] = false;
bug.energy += FLORA_ENERGY;
if ( bug.energy > MAX_ENERGY )
{
bug.energy = MAX_ENERGY;
}
}
if ( bug.energy >= BIRTH_ENERGY )
{
giveBirth ( bug );
}
if ( random.nextBoolean ( ) )
{
if ( bug.genesX [ time ] )
{
x++;
}
else
{
x--;
}
if ( x < 0 )
{
x = SPACE_WIDTH - 1;
}
else if ( x >= SPACE_WIDTH )
{
x = 0;
}
bug.x = x;
}
if ( random.nextBoolean ( ) )
{
if ( bug.genesY [ time ] )
{
y++;
}
else
{
y--;
}
if ( y < 0 )
{
y = SPACE_HEIGHT - 1;
}
else if ( y >= SPACE_HEIGHT )
{
y = 0;
}
bug.y = y;
}
bug.energy -= MOVE_COST;
}
}
}
private void growFlora ( )
//////////////////////////////////////////////////////////////////////
{
for ( int i = 0; i < flora_growth_rate; i++ )
{
// randomly position food flora
int x = random.nextInt ( SPACE_WIDTH );
int y = random.nextInt ( SPACE_HEIGHT );
flora_present [ x ] [ y ] = true;
}
// Replenishing the Garden of Eden.
if ( edenCheckBox.isSelected ( ) )
{
for ( int x = EDEN_X0; x <= EDEN_X1; x++ )
{
for ( int y = EDEN_Y0; y <= EDEN_Y1; y++ )
{
flora_present [ x ] [ y ] = true;
}
}
}
}
private void setAllFloraPresent ( boolean floraPresent )
//////////////////////////////////////////////////////////////////////
{
for ( int x = 0; x < SPACE_WIDTH; x++ )
{
for ( int y = 0; y < SPACE_HEIGHT; y++ )
{
flora_present [ x ] [ y ] = floraPresent;
}
}
}
private String genesAverageString ( )
//////////////////////////////////////////////////////////////////////
{
long x_sum, y_sum;
String gene_x_String = "X: ";
String gene_y_String = "Y: ";
bugs_alive = 0;
for ( int i = 0; i < bugs.length; i++ )
{
if ( bugs [ i ].energy > 0 )
{
bugs_alive++;
}
}
for ( int i = 0; i < GENES_MAX; i++ )
{
x_sum = 0;
y_sum = 0;
for ( int j = 0; j < bugs.length; j++ )
{
Bug bug = bugs [ j ];
if ( bug.energy > 0 )
{
if ( bug.genesX [ i ] )
{
x_sum++;
}
if ( bug.genesY [ i ] )
{
y_sum++;
}
}
}
if ( ( double ) x_sum / ( double ) bugs_alive >= 0.5 )
{
gene_x_String += "1";
}
else
{
gene_x_String += "0";
}
if ( ( double ) y_sum / ( double ) bugs_alive >= 0.5 )
{
gene_y_String += "1";
}
else
{
gene_y_String += "0";
}
}
return gene_x_String + " " + gene_y_String;
}
private void plotBugs ( Graphics g )
//////////////////////////////////////////////////////////////////////
{
for ( int i = 0; i < bugs.length; i++ )
{
Bug bug = bugs [ i ];
if ( bug.energy > 0 )
{
PlotLib.xy (
bug.color,
bug.x + 0.5,
bug.y + 0.5,
bounds, g,
0, SPACE_WIDTH, 0, SPACE_HEIGHT,
1, true );
}
}
}
private void plotFlora ( Graphics g )
//////////////////////////////////////////////////////////////////////
{
for ( int x = 0; x < SPACE_WIDTH; x++ )
{
for ( int y = 0; y < SPACE_HEIGHT; y++ )
{
if ( flora_present [ x ] [ y ] )
{
PlotLib.xy (
Color.GREEN,
x + 0.5,
y + 0.5,
bounds, g,
0, SPACE_WIDTH, 0, SPACE_HEIGHT,
1, true );
}
}
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -