/// <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); } }
/// <summary> /// Accesses a piece of rope on the specified cell owned by this pig. /// If there is more than one piece of rope on that cell owned by this pig, /// then the most recently placed will be returned. /// If no ropes owned by this pig are on that cell, null is returned. /// </summary> /// <returns> the requested rope from that Cell. </returns> public RopePiece GetMyRopePiece(Cell cell) { List <RopePiece> ropes = cell.InspectAll <RopePiece>(); RopePiece bestRopeSoFar = null; foreach (RopePiece ropePiece in ropes) { if (ropePiece.OwnerPig == this) { if (bestRopeSoFar == null) { bestRopeSoFar = ropePiece; } else { if (ropePiece.GetDistanceFromOwner() < bestRopeSoFar.GetDistanceFromOwner()) { bestRopeSoFar = ropePiece; } } } } return(bestRopeSoFar); }