📄 form1.cs
字号:
/// see if the number of critters has been updated
if( nCount != antsGeneralPage.NumberOfAnts )
{
if( nCount < antsGeneralPage.NumberOfAnts )
{
int nDifference = antsGeneralPage.NumberOfAnts - nCount;
/// add the new ants to the array
for( int i=0; i<nDifference; i++ )
{
crittersArray.Add( new Ant() );
}
/// set up the defaults for the new ants
for( int i=nCount; i<antsGeneralPage.NumberOfAnts; i++ )
{
( ( Critter )crittersArray[ i ] ).Left = nest.Left;
( ( Critter )crittersArray[ i ] ).Right = nest.Right;
( ( Critter )crittersArray[ i ] ).Vertical = nest.Location.Y;
( ( Critter )crittersArray[ i ] ).Algorythm = algorythm;
/// draw the ant on the square
nest.DrawAnt = true;
nest.IsCritterOnSquare = true;
nest.CritterIsAnt = true;
/// didn't move here from anywhere
( ( Critter )crittersArray[ i ] ).LastDirectionTravelled = ( int )DirectionLastTravelledSet.NONE;
( ( Ant )crittersArray[ i ] ).TryToAvoidOccupiedSquares = true;
}
}
else // reduced the number of ants
{
int nDifference = nCount - antsGeneralPage.NumberOfAnts;
for( int i=0; i<nDifference; i++ )
{
( ( Critter )crittersArray[ i ] ).Remove = true;
}
}
}
/// check that the affected ants is still correct
int nBehaviourChanged = antsGeneralPage.NumberOfAffectedAnts;
if( nBehaviourChanged > nCount )
{
nBehaviourChanged = nCount;
antsGeneralPage.NumberOfAffectedAnts = nCount;
}
}
/// ameobas don't move
if( critter == CrittersSet.AMEOBA )
{
updateTimer.Start();
for( int i=0; i<squaresArray.Count; i++ )
{
if( squaresArray[ i ] != null )
{
if( ( ( StandardSquare )squaresArray[ i ] ).CritterIsAmeoba == true )
{
( ( StandardSquare )squaresArray[ i ] ).DrawAmeoba = true;
( ( StandardSquare )squaresArray[ i ] ).Invalidate();
return;
}
}
}
}
for( int i=0; i<crittersArray.Count; i++ )
{
/// remove all items from the available squares list
for( int j=squaresAvailableArray.Count; j > 0; j-- )
{
squaresAvailableArray.RemoveAt( j-1 );
}
/// get the squares around the current square
Critter tempCritter = ( Critter )crittersArray[ i ];
StandardSquare current = null;
for( int n=0; n<squaresArray.Count; n++ )
{
if( squaresArray[ n ] != null )
{
if( ( ( StandardSquare )squaresArray[ n ] ).Left == tempCritter.Left
&& ( ( StandardSquare )squaresArray[ n ] ).Right == tempCritter.Right
&& ( ( StandardSquare )squaresArray[ n ] ).Location.Y == tempCritter.Vertical )
{
current = ( StandardSquare )squaresArray[ n ];
}
}
}
if( current == null )
{
MessageBox.Show( "Error Square at left :- " + tempCritter.Left + ", right :- " + tempCritter.Right + " and vertical :- " + tempCritter.Vertical + " is not in list" );
updateTimer.Start();
return;
}
squaresAvailableArray.Add( current );
/// got the current square now get the surrounding squares
/// need to get top right bottom and left
StandardSquare top = null;
for( int n=0; n<squaresArray.Count; n++ )
{
if( squaresArray[ n ] != null )
{
if( ( ( StandardSquare )squaresArray[ n ] ).Location.X == current.Location.X
&& ( ( StandardSquare )squaresArray[ n ] ).Location.Y == ( current.Location.Y - current.Size.Height ) )
{
if( ( ( StandardSquare )squaresArray[ n ] ).SquareType == "STANDARD" )
top = null;
else
top = ( StandardSquare )squaresArray[ n ];
}
}
}
if( top == null )
{
squaresAvailableArray.Add( new StandardSquare() );
}
else
squaresAvailableArray.Add( top );
/// now get the square to the right
StandardSquare right = null;
for( int n=0; n<squaresArray.Count; n++ )
{
if( squaresArray[ n ] != null )
{
if( ( ( StandardSquare )squaresArray[ n ] ).Location.Y == current.Location.Y
&& ( ( StandardSquare )squaresArray[ n ] ).Location.X == ( current.Location.X + current.Size.Width ) )
{
if( ( ( StandardSquare )squaresArray[ n ] ).SquareType == "STANDARD" )
right = null;
else
right = ( StandardSquare )squaresArray[ n ];
}
}
}
if( right == null )
{
squaresAvailableArray.Add( new StandardSquare() );
}
else
squaresAvailableArray.Add( right );
/// now get the bottom square
StandardSquare bottom = null;
for( int n=0; n<squaresArray.Count; n++ )
{
if( squaresArray[ n ] != null )
{
if( ( ( StandardSquare )squaresArray[ n ] ).Location.X == current.Location.X
&& ( ( StandardSquare )squaresArray[ n ] ).Location.Y == ( current.Location.Y + current.Size.Height ) )
{
if( ( ( StandardSquare )squaresArray[ n ] ).SquareType == "STANDARD" )
bottom = null;
else
bottom = ( StandardSquare )squaresArray[ n ];
}
}
}
if( bottom == null )
{
squaresAvailableArray.Add( new StandardSquare() );
}
else
squaresAvailableArray.Add( bottom );
/// now get the square to the left
StandardSquare left = null;
for( int n=0; n<squaresArray.Count; n++ )
{
if( squaresArray[ n ] != null )
{
if( ( ( StandardSquare )squaresArray[ n ] ).Location.Y == current.Location.Y
&& ( ( StandardSquare )squaresArray[ n ] ).Location.X == ( current.Location.X - current.Size.Height ) )
{
if( ( ( StandardSquare )squaresArray[ n ] ).SquareType == "STANDARD" )
left = null;
else
left = ( StandardSquare )squaresArray[ n ];
}
}
}
if( left == null )
{
squaresAvailableArray.Add( new StandardSquare() );
}
else
squaresAvailableArray.Add( left );
if( tempCritter.Remove == true )
{
( ( StandardSquare )squaresAvailableArray[ ( int )ArrayPositionSet.CURRENT ] ).IsCritterOnSquare = false;
( ( StandardSquare )squaresAvailableArray[ ( int )ArrayPositionSet.CURRENT ] ).DrawAnt = false;
( ( StandardSquare )squaresAvailableArray[ ( int )ArrayPositionSet.CURRENT ] ).Invalidate();
crittersArray.Remove( tempCritter );
}
else
{
CalculateNextSquare( ref squaresAvailableArray, i );
}
}
DisplayLog();
updateTimer.Start();
}
/// <summary>
/// calculate where to go for the next square
/// squares are stored in clockwise manner so current square will be at position 0
/// </summary>
/// <param name="availableSquares"></param>
/// <returns></returns>
public StandardSquare CalculateNextSquare( ref ArrayList availableSquares, int nCrittersArrayLocation )
{
if( availableSquares == null || availableSquares.Count == 0 )
{
MessageBox.Show( "Error Empty array passed to Calulate next square" );
return null;
}
/// find what directions are available to move to
SquareDirectionsSet squareDirections;
switch( ( ( StandardSquare )availableSquares[ 0 ] ).Directions )
{
case "ALL": squareDirections = SquareDirectionsSet.ALL; break;
case "BOTTOMLEFT": squareDirections = SquareDirectionsSet.BOTTOMLEFT; break;
case "BOTTOMRIGHT": squareDirections = SquareDirectionsSet.BOTTOMRIGHT; break;
case "LEFTRIGHT": squareDirections = SquareDirectionsSet.LEFTRIGHT; break;
case "NONE": squareDirections = SquareDirectionsSet.NONE; break;
case "TOPBOTTOM": squareDirections = SquareDirectionsSet.TOPBOTTOM; break;
case "TOPLEFT": squareDirections = SquareDirectionsSet.TOPLEFT; break;
case "TOPRIGHT": squareDirections = SquareDirectionsSet.TOPRIGHT; break;
default:
{
MessageBox.Show( "Error direction passed is not in list" );
squareDirections = SquareDirectionsSet.NONE;
}break;
}
/// get the squares
StandardSquare topSquare = ( StandardSquare )availableSquares[ ( int )ArrayPositionSet.TOP ];
StandardSquare rightSquare = ( StandardSquare )availableSquares[ ( int )ArrayPositionSet.RIGHT ];
StandardSquare bottomSquare = ( StandardSquare )availableSquares[ ( int )ArrayPositionSet.BOTTOM ];
StandardSquare leftSquare = ( StandardSquare )availableSquares[ ( int )ArrayPositionSet.LEFT ];
StandardSquare currentSquare = ( StandardSquare )availableSquares[ ( int )ArrayPositionSet.CURRENT ];
/// find out where we came from
DirectionLastTravelledSet directionLastTravelled;
switch( ( DirectionLastTravelledSet )( ( Critter )crittersArray[ nCrittersArrayLocation ] ).LastDirectionTravelled )
{
case DirectionLastTravelledSet.BOTTOM: directionLastTravelled = DirectionLastTravelledSet.BOTTOM; break;
case DirectionLastTravelledSet.LEFT: directionLastTravelled = DirectionLastTravelledSet.LEFT; break;
case DirectionLastTravelledSet.NONE: directionLastTravelled = DirectionLastTravelledSet.NONE; break;
case DirectionLastTravelledSet.RIGHT: directionLastTravelled = DirectionLastTravelledSet.RIGHT; break;
case DirectionLastTravelledSet.TOP: directionLastTravelled = DirectionLastTravelledSet.TOP; break;
default:
{
MessageBox.Show( "Error direction last travelled is not in list" );
directionLastTravelled = DirectionLastTravelledSet.NONE;
}break;
}
if( squareDirections == SquareDirectionsSet.NONE && directionLastTravelled == DirectionLastTravelledSet.NONE )
{
MessageBox.Show( "This critter is like so totally screwed you really need a break point here" );
return null;
}
Critter tempCritter = ( Critter )crittersArray[ nCrittersArrayLocation ];
/// LogString( "Calling the Algorythms to move the critters" );
/// alright lets see where we are going then
switch( tempCritter.Algorythm )
{
case AlgorythmSet.BASIC:
{
pathFinderAlgorythms.BasicAlgorythm( ref availableSquares, ref tempCritter, directionLastTravelled, squareDirections );
}break;
case AlgorythmSet.BASIC2:
{
pathFinderAlgorythms.BasicAlgorythm2( ref availableSquares, ref tempCritter, directionLastTravelled, squareDirections );
}break;
case AlgorythmSet.PHEROMONES:
{
pathFinderAlgorythms.PheromonesAlgorythm( ref availableSquares, ref tempCritter, directionLastTravelled, squareDirections );
}break;
case AlgorythmSet.MEMORY:
{
/// tell the critter to remember where it has been
if( critter == CrittersSet.ANT )
{
for( int i=0; i<crittersArray.Count; i++ )
{
( ( Ant )crittersArray[ i ] ).UseMemory = true;
( ( Ant )crittersArray[ i ] ).RememberWayHome = true;
}
}
pathFinderAlgorythms.MemoryAlgorythm( ref availableSquares, ref tempCritter, directionLastTravelled, squareDirections );
}break;
case AlgorythmSet.HILLCLIMB:
{
pathFinderAlgorythms.HillClimbingAlgorythm( ref availableSquares, ref tempCritter, directionLastTravelled, squareDirections );
}break;
case AlgorythmSet.HILLCLIMBTWO:
{
pathFinderAlgorythms.HillClimbingAlgorythmTwo( ref availableSquares, ref tempCritter, directionLastTravelled, squareDirections );
}break;
case AlgorythmSet.HILLCLIMBTHREE:
{
/// tell the critter to remember where it has been
if( critter == CrittersSet.ANT )
{
for( int i=0; i<crittersArray.Count; i++ )
{
( ( Ant )crittersArray[ i ] ).UseMemory = true;
( ( Ant )crittersArray[ i ] ).RememberWayHome = true;
}
}
pathFinderAlgorythms.HillClimbingAlgorythmThree( ref availableSquares, ref tempCritter, directionLastTravelled, squareDirections );
}break;
default:
{
MessageBox.Show( "Error algorythm is not in the list" );
/// don't put a return here as it makes the break unreachable
}break;
}
return null;
}
/// <summary>
/// called by the pheromone decay timer decrements the pheromone levels for a square over time
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnPheromoneDecay(object sender, System.EventArgs e)
{
for( int i=0; i<squaresArray.Count; i++ )
{
if( squaresArray[ i ] != null )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -