⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 critter.cs

📁 包括Pheromones Algorythm、Memory Algorythm和Hill Climbing Algorythm I
💻 CS
字号:
using System;
using System.Collections;

namespace PathFinder
{
	/// <summary>
	/// base class for critters takes care of things like direction travelling and othe common stuff
	/// </summary>
	public class Critter
	{
		/// <summary>
		/// critter identifier
		/// </summary>
		private static int nCritterID = 0;
		private int nCritterNumber;

		/// <summary>
		///  direction the critter came from
		/// </summary>
		private int nLastDirectionTravelled;

		/// <summary>
		/// if the ant has done what it set out to do is it returning home
		/// </summary>
		private bool bIsReturningHome;

		/// <summary>
		/// identifiers for the current location of the square the critter is located at
		/// </summary>
		private int nLeft;
		private int nRight;
		private int nVertical;

		/// <summary>
		/// make an effort to find another path if critter is already on target square
		/// </summary>
		private bool bTryToAvoidOccupiedSquares;

		/// <summary>
		/// keep track of if we are repeating a set of actions
		/// </summary>
		private ArrayList arrayDirectionLastTravelled;

		/// <summary>
		///  number of moves to remember
		/// </summary>
		const int ARRAYCOUNTSIZE = 4;

		/// <summary>
		/// should this critter be removed from the array
		/// </summary>
		private bool bRemove;

		/// <summary>
		/// algorythm for this critter to use
		/// </summary>
		private AlgorythmSet algorythm;


		/// <summary>
		/// which was the last direction the critter came from
		/// See DirectionLastTravelledSet enum in Form1.cs for meanings
		/// </summary>
		public int LastDirectionTravelled
		{
			get
			{
				return nLastDirectionTravelled;
			}
			set
			{
				nLastDirectionTravelled = value;
				if( arrayDirectionLastTravelled.Count == ARRAYCOUNTSIZE )
				{
					arrayDirectionLastTravelled.RemoveAt( 0 );
				}
				
				arrayDirectionLastTravelled.Add( nLastDirectionTravelled );
			}
		}

		public int GetDirectionTravelledAt( int nLocation )
		{
			if( nLocation < arrayDirectionLastTravelled.Count )
				return ( int )arrayDirectionLastTravelled[ nLocation ];
			
			return 0;
		}


		/// <summary>
		/// if the critter has done or got what it was after return home
		/// </summary>
		public bool IsReturningHome
		{
			get
			{
				return bIsReturningHome;
			}
			set
			{
				bIsReturningHome = value;
			}
		}


		/// <summary>
		/// the left and right values of the square should be enough to
		/// identify each square
		/// </summary>
		public int Left
		{
			get
			{
				return nLeft;
			}
			set
			{
				nLeft = value;
			}
		}

		public int Right
		{
			get
			{
				return nRight;
			}
			set
			{
				nRight = value;
			}
		}

		public int Vertical
		{
			get
			{
				return nVertical;
			}
			set
			{
				nVertical = value;
			}
		}

		public bool TryToAvoidOccupiedSquares
		{
			get
			{
				return bTryToAvoidOccupiedSquares;
			}
			set
			{
				bTryToAvoidOccupiedSquares = value;
			}
		}

		public AlgorythmSet Algorythm
		{
			get
			{
				return algorythm;
			}
			set
			{
				algorythm = value;
			}
		}

		public bool IsRepeating
		{
			get
			{
				if( arrayDirectionLastTravelled.Count == ARRAYCOUNTSIZE )
				{
					if( ( int )arrayDirectionLastTravelled[ 0 ] == ( int )arrayDirectionLastTravelled[ 2 ] &&
						( int )arrayDirectionLastTravelled[ 1 ] == ( int )arrayDirectionLastTravelled[ 3 ] )
						return true;
				}
			
				return false;
			}
		}

		public bool IsRepeatingOnce
		{
			get
			{
				if( arrayDirectionLastTravelled.Count == ARRAYCOUNTSIZE )
				{
					if( ( int )arrayDirectionLastTravelled[ 1 ] == nLastDirectionTravelled )
						return true;
				}

				return false;
			}
		}

		public bool HasTravelled( int nDirection )
		{
			bool bHasTravelled = false;
			for( int i=0; i<arrayDirectionLastTravelled.Count; i++ )
			{
				if( nDirection == ( int )arrayDirectionLastTravelled[ i ] )
					bHasTravelled = true;
			}

			return bHasTravelled;
		}


		public bool Remove
		{
			get
			{
				return bRemove;
			}
			set
			{
				bRemove = value;
			}
		}

		public int CritterIdentifier
		{
			get
			{
				return nCritterNumber;
			}
		}

		public Critter()
		{
			nLastDirectionTravelled = 0;
			bTryToAvoidOccupiedSquares = false;
			nLeft = 0;
			nRight = 0;
			nVertical = 0;
			arrayDirectionLastTravelled = new ArrayList();
			bRemove = false;
			algorythm = AlgorythmSet.BASIC;
			nCritterNumber = nCritterID;
			nCritterID++;
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -