Ejemplo n.º 1
0
        /// <summary>
        /// If a pig can not move directly towards it's food, this method examines all surrounding cells
        /// and determines which direction is the best to move in. In the following order:
        /// 1. Move towards an empty cell in the closest direction to the closest food.
        /// 2. If no cells are empty move towards the cell with the oldest rope on it.
        /// 3. Unable to move to any nearby cell. Do nothing.
        /// pre: Pig is unable to move towards the food, or there is rope in the adjacent cell.
        /// post: Pig will move to a valid location, or not at all. Depending on the above circumstances.
        /// </summary>
        private void ExamineSurroundingCells()
        {
            const double DEFAULT_LOWEST_DIRECTION_DIFFERENCE = 360.00;
            Echo         nearestPigFood            = FindNearest(typeof(PigFood));
            double       lowestDirectionDifference = 360.00;          //default value 360 is a placeholder
            Direction    closestDirectionToFood    = Direction.NORTH; //default value (Direction.NORTH) is a placeholder
            int          oldestNearbyRope          = -1;              //default value -1 is a placeholder
            Direction    oldestRopeDirection       = Direction.NORTH; //default value (Direction.NORTH) is a placeholder

            for (int i = 0; i < Direction.NUMBER_POSSIBLE; i++)
            {
                Direction direction    = Direction.GetAdjacentCellDirection(i);
                Cell      adjacentCell = Cell.GetAdjacentCell(direction);

                if (adjacentCell != null && CanMove(direction))
                {
                    RopePiece ropeInCell = GetMyRopePiece(adjacentCell);
                    if (ropeInCell == null)
                    {
                        double adjacentCellDirection = direction.Degrees;
                        double foodDirection         = nearestPigFood.direction.Degrees;
                        double differenceInDegrees   = Math.Abs(foodDirection - adjacentCellDirection);

                        // The two lines below solve the '359 degrees is right next to 0 degrees' issue using basic maths.
                        if (differenceInDegrees > 180)                       // 180 being 180 degrees (DO NOT change)
                        {
                            differenceInDegrees = 360 - differenceInDegrees; // 360 being 360 degrees (DO NOT change)
                        }
                        if (lowestDirectionDifference > differenceInDegrees)
                        {
                            lowestDirectionDifference = differenceInDegrees;
                            closestDirectionToFood    = direction;
                        }
                    }
                    else if (ropeInCell != null)
                    {
                        Pig ropeOwner = ropeInCell.OwnerPig;
                        int ropeAge   = ropeInCell.GetDistanceFromOwner();
                        if (oldestNearbyRope < ropeAge)
                        {
                            oldestNearbyRope    = ropeAge;
                            oldestRopeDirection = direction;
                        }
                    }
                }
            }
            if (lowestDirectionDifference != DEFAULT_LOWEST_DIRECTION_DIFFERENCE)   //If it has changed from default value, a valid direction has been found.
            {
                DropRope();
                Move(closestDirectionToFood);
            }
            else
            {
                DropRope();
                Move(oldestRopeDirection);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Tests if this and the other pig are full brothers/sisters.  That is,
 /// tests that both pigs have the same two parents.
 /// </summary>
 /// <param name="possibleSibling"> A Pig ... or perhaps a null reference </param>
 /// <returns> true if this pig and the other pig have the same two parents.  </returns>
 protected bool IsSibling(Pig pig)
 {
     if ((pig.mother == null) || (pig.father == null))
     {
         return(false);
     }
     if ((this.mother == pig.mother) && (this.father == pig.father))
     {
         return(true);
     }
     return(false);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Tests to see if this object is indeed a child of possibleParent.
 /// </summary>
 /// <param name="possibleParent"> The Pig who's past is in question, or a null
 /// reference </param>
 /// <returns> true if this pig is a child of possibleParent </returns>
 public bool IsParent(Pig possibleParent)
 {
     if (this.mother == possibleParent)
     {
         return(true);
     }
     if (this.father == possibleParent)
     {
         return(true);
     }
     return(false);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Constructs a new piece of rope.
        /// </summary>
        /// <param name="pigWorld"></param>
        /// <param name="ownerPig"></param>
        /// <param name="position"></param>
        public RopePiece(PigWorld pigWorld, Pig ownerPig, Position position)
        {
            this.ownerPig = ownerPig;

            // Allow more than one ropePiece in a cell.  (See comments at top of Cell.cs.)
            OnlyOneObjectOfThisTypePerCell = false;

            AddToWorld(pigWorld, position);
            if (this.PigWorld == null)
            {
                throw new Exception("Error: Rope could not be created!");
            }
        }