📄 pathfinderalgorythms.cs
字号:
&& bottomDecision.IsValid == true )
/// && bottomDecision.Name != strDirection )
decision = bottomDecision;
}break;
case "LEFT":
{
if( leftSquare != null
&& leftDecision.IsValid == true )
/// && leftDecision.Name != strDirection )
decision = leftDecision;
}break;
}
}
else
{
if( topSquare != null
&& topDecision.IsValid == true )
decision = topDecision;
else if( rightSquare != null
&& rightDecision.IsValid == true )
decision = rightDecision;
else if( bottomSquare != null
&& bottomDecision.IsValid == true )
decision = bottomDecision;
else if( leftSquare != null
&& leftDecision.IsValid == true )
decision = leftDecision;
else
{
/// pick a valid decision
if( topSquare != null
&& topDecision.IsValid == true )
decision = topDecision;
if( rightSquare != null
&& rightDecision.IsValid == true )
decision = rightDecision;
if( bottomSquare != null
&& bottomDecision.IsValid == true )
decision = bottomDecision;
if( leftSquare != null
&& leftDecision.IsValid == true )
decision = leftDecision;
}
}
bool bRepeatedOnce = false;
/// see if we are repeating the same moves over and over
if( currentAnt.IsRepeatingOnce == true
&& currentSquare.Name != "NEST" )
{
/// see if there is another way to move
switch( currentAnt.GetDirectionTravelledAt( 3 ) )
{
case ( int )DirectionLastTravelledSet.NONE: strDirection = "NONE"; break;
case ( int )DirectionLastTravelledSet.TOP: strDirection = "TOP"; break;
case ( int )DirectionLastTravelledSet.RIGHT: strDirection = "RIGHT"; break;
case ( int )DirectionLastTravelledSet.BOTTOM: strDirection = "BOTTOM"; break;
case ( int )DirectionLastTravelledSet.LEFT: strDirection = "LEFT"; break;
default: strDirection = "NONE"; break;
}
switch( currentAnt.GetDirectionTravelledAt( 2 ) )
{
case ( int )DirectionLastTravelledSet.NONE: strPreviousDirection = "NONE"; break;
case ( int )DirectionLastTravelledSet.TOP: strPreviousDirection = "TOP"; break;
case ( int )DirectionLastTravelledSet.RIGHT: strPreviousDirection = "RIGHT"; break;
case ( int )DirectionLastTravelledSet.BOTTOM: strPreviousDirection = "BOTTOM"; break;
case ( int )DirectionLastTravelledSet.LEFT: strPreviousDirection = "LEFT"; break;
default: strPreviousDirection = "NONE"; break;
}
if( strDirection == decision.Name )
{
/// if going in a straight line allow it to continue
if( topSquare != null
&& topDecision.IsValid == true
&& strDirection == "TOP" )
decision = topDecision;
if( rightSquare != null
&& rightDecision.IsValid == true
&& strDirection == "RIGHT" )
decision = rightDecision;
if( bottomSquare != null
&& bottomDecision.IsValid == true
&& strDirection == "BOTTOM" )
decision = bottomDecision;
if( leftSquare != null
&& leftDecision.IsValid == true
&& strDirection == "LEFT" )
decision = leftDecision;
bRepeatedOnce = true;
}
}
if( currentAnt.IsRepeating == true )
{
switch( currentAnt.GetDirectionTravelledAt( 3 ) )
{
case ( int )DirectionLastTravelledSet.NONE: strDirection = "NONE"; break;
case ( int )DirectionLastTravelledSet.TOP: strDirection = "TOP"; break;
case ( int )DirectionLastTravelledSet.RIGHT: strDirection = "RIGHT"; break;
case ( int )DirectionLastTravelledSet.BOTTOM: strDirection = "BOTTOM"; break;
case ( int )DirectionLastTravelledSet.LEFT: strDirection = "LEFT"; break;
default: strDirection = "NONE"; break;
}
switch( currentAnt.GetDirectionTravelledAt( 2 ) )
{
case ( int )DirectionLastTravelledSet.NONE: strPreviousDirection = "NONE"; break;
case ( int )DirectionLastTravelledSet.TOP: strPreviousDirection = "TOP"; break;
case ( int )DirectionLastTravelledSet.RIGHT: strPreviousDirection = "RIGHT"; break;
case ( int )DirectionLastTravelledSet.BOTTOM: strPreviousDirection = "BOTTOM"; break;
case ( int )DirectionLastTravelledSet.LEFT: strPreviousDirection = "LEFT"; break;
default: strPreviousDirection = "NONE"; break;
}
if( decision.Name == strDirection || decision.Name == strPreviousDirection
&& bRepeatedOnce == false )
{
switch( decision.Name )
{
case "TOP":
{
if( strPreviousDirection != "RIGHT"
&& rightSquare != null
&& rightDecision.IsValid == true )
decision = rightDecision;
else if( strPreviousDirection != "BOTTOM"
&& bottomSquare != null
&& bottomDecision.IsValid == true )
decision = bottomDecision;
else if( strPreviousDirection != "LEFT"
&& leftSquare != null
&& leftDecision.IsValid == true )
decision = leftDecision;
else
{
/// nuts
decision = topDecision;
}
}break;
case "RIGHT":
{
if( strPreviousDirection != "TOP"
&& topSquare != null
&& topDecision.IsValid == true )
decision = topDecision;
else if( strPreviousDirection == "BOTTOM"
&& bottomSquare != null
&& bottomDecision.IsValid == true )
decision = bottomDecision;
else if( strPreviousDirection == "LEFT"
&& leftSquare != null
&& leftDecision.IsValid == true )
decision = leftDecision;
else
{
/// grrr
decision = rightDecision;
}
}break;
case "BOTTOM":
{
if( strPreviousDirection != "TOP"
&& topSquare != null
&& topDecision.IsValid == true )
decision = topDecision;
else if( strPreviousDirection != "RIGHT"
&& rightSquare != null
&& rightDecision.IsValid == true )
decision = rightDecision;
else if( strPreviousDirection != "LEFT"
&& leftSquare != null
&& leftDecision.IsValid == true )
decision = leftDecision;
else
{
/// awww
decision = bottomDecision;
}
}break;
case "LEFT":
{
if( strPreviousDirection != "TOP"
&& topSquare != null
&& topDecision.IsValid == true )
decision = topDecision;
else if( strPreviousDirection != "RIGHT"
&& rightSquare != null
&& rightDecision.IsValid == true )
decision = rightDecision;
else if( strPreviousDirection != "BOTTOM"
&& bottomSquare != null
&& bottomDecision.IsValid == true )
decision = bottomDecision;
else
{
/// insert expletive here
decision = leftDecision;
}
}break;
}
}
}
/// move the ant away from the food once it has found it
if( currentAnt.IsReturningHome == true )
{
if( topSquare != null
&& topSquare.SquareType == "FOOD"
&& bottomSquare != null )
{
decision = bottomDecision;
}
if( rightSquare != null
&& rightSquare.SquareType == "FOOD"
&& leftSquare != null )
{
decision = leftDecision;
}
if( bottomSquare != null
&& bottomSquare.SquareType == "FOOD"
&& topSquare != null )
{
decision = topDecision;
}
if( leftSquare != null
&& leftSquare.SquareType == "FOOD"
&& rightSquare != null )
{
decision = rightDecision;
}
}
/// check for pheromone following
if( currentAnt.IsReturningHome == false
&& topSquare != null
&& topSquare.SquareType != "NEST"
&& topSquare.PathFoodPheromone > 0
&& topDecision.IsValid == true )
{
decision = topDecision;
}
else if( currentAnt.IsReturningHome == false
&& rightSquare != null
&& rightSquare.SquareType != "NEST"
&& rightSquare.PathFoodPheromone > 0
&& rightDecision.IsValid == true )
{
decision = rightDecision;
}
else if( currentAnt.IsReturningHome == false
&& bottomSquare != null
&& bottomSquare.SquareType != "NEST"
&& bottomSquare.PathFoodPheromone > 0
&& bottomDecision.IsValid == true )
{
decision = bottomDecision;
}
else
{
if( currentAnt.IsReturningHome == false
&& leftSquare != null
&& leftSquare.SquareType != "NEST"
&& leftSquare.PathFoodPheromone > 0
&& leftDecision.IsValid == true )
decision = leftDecision;
}
/// if an ant is returning home and the squar infront has no
/// pheromone and is not the home square move to it
if( currentAnt.IsReturningHome == true
&& topSquare != null
&& topSquare.SquareType != "NEST"
&& topSquare.PathFoodPheromone == 0
&& topDecision.IsValid == true )
{
decision = topDecision;
}
else if( currentAnt.IsReturningHome == true
&& rightSquare != null
&& rightSquare.SquareType != "NEST"
&& rightSquare.PathFoodPheromone == 0
&& rightDecision.IsValid == true )
{
decision = rightDecision;
}
else if( currentAnt.IsReturningHome == true
&& bottomSquare != null
&& bottomSquare.SquareType != "NEST"
&& bottomSquare.PathFoodPheromone == 0
&& bottomDecision.IsValid == true )
{
decision = bottomDecision;
}
else if( currentAnt.IsReturningHome == true
&& leftSquare != null
&& leftSquare.SquareType != "NEST"
&& leftSquare.PathFoodPheromone == 0
&& leftDecision.IsValid == true )
{
decision = leftDecision;
}
else
{
/// continue
}
/// if the ant is returning home and the next square is the home square move to it
if( currentAnt.IsReturningHome == true
&& topSquare != null
&& topSquare.SquareType == "NEST" )
{
decision = topDecision;
}
else if( currentAnt.IsReturningHome == true
&& rightSquare != null
&& rightSquare.SquareType == "NEST" )
{
decision = rightDecision;
}
else if( currentAnt.IsReturningHome == true
&& bottomSquare != null
&& bottomSquare.SquareType == "NEST" )
{
decision = bottomDecision;
}
else if( currentAnt.IsReturningHome == true
&& leftSquare != null
&& leftSquare.SquareType == "NEST" )
{
decision = leftDecision;
}
else
{
}
/// update the current square before decisions are changed
if( currentAnt.HasFoundFood == true
&& currentAnt.IsReturningHome == true )
{
currentSquare.IncrementPathFoodPheromone();
currentSquare.IncrementPathFoodPheromone();
currentSquare.IncrementPathFoodPheromone();
}
DecideSquareForAnt( ref availableSquares, ref currentAnt, decision );
}
}
public void BasicAlgorythm2( ref ArrayList availableSquares, ref Critter critter, DirectionLastTravelledSet directionLastTravelled, SquareDirectionsSet squareDirections )
{
/// 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 ];
bool bIsTopAvailable = false;
bool bIsRightAvailable = false;
bool bIsBottomAvailable = false;
bool bIsLeftAvailable = false;
/// is the top available
if( squareDirections == SquareDirectionsSet.TOPBOTTOM || squareDirections == SquareDirectionsSet.TOPLEFT
|| squareDirections == SquareDirectionsSet.TOPRIGHT || squareDirections == SquareDirectionsSet.ALL
&& topSquare != null && topSquare.SquareType == "BOTTOMLEFT" || topSquare.SquareType == "BOTTOMRIGHT"
|| topSquare.SquareType == "FOURWAYS" || topSquare.SquareType == "TOPBOTTOM" || topSquare.SquareType == "FOOD"
|| topSquare.SquareType == "NEST" )
{
bIsTopAvailable = true;
}
/// is the right available
if( squareDirections == SquareDirectionsSet.BOTTOMRIGHT || squareDirections == SquareDirectionsSet.LEFTRIGHT
|| squareDirections == SquareDirectionsSet.TOPRIGHT || squareDirections == SquareDirectionsSet.ALL
&& rightSquare != null && rightSquare.SquareType == "LEFTRIGHT" || rightSquare.SquareType == "FOURWAYS"
|| rightSquare.SquareType == "BOTTOMLEFT" || rightSquare.SquareType == "TOPLEFT" || rightSquare.SquareType == "FOOD"
|| rightSquare.SquareType == "NEST" )
{
bIsRightAvailable = true;
}
/// is the bottom square available
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -