/// <summary> /// First the pig looks to see if there is any PigFood in its current Cell. /// If there is, the pig picks it up, eats it, and does nothing more. I.e. the pig stays where it is. /// Otherwise, the pig looks for the nearest PigFood, using the FindNearest method. /// If there isn't any food anywhere in the pigWorld, the pig does nothing. /// If there is food, then the pig (tries to) move one square in the direction /// of that food. If the move fails (e.g. because there's a wall in the way, /// or any other reason), then the pig simply remains where it is. /// </summary> public void LookForFood() { // Do not modify, delete, or move the debugAnimalAction line below, // or the Debug Info will not show correctly in the GUI, and that could be confusing. debugAnimalAction = "LookForFood"; bool pigFoodInCurrentCell = Cell.Exists(typeof(PigFood)); if (pigFoodInCurrentCell) { PigFood food = (PigFood)Cell.PickUp(typeof(PigFood)); Eat(food); } else { Echo nearestPigFood = FindNearest(typeof(PigFood)); if (nearestPigFood != null) { Cell targetCell = Cell.GetAdjacentCell(nearestPigFood.direction); if (targetCell != null) { LifeForm adjacentLifeForm = targetCell.LifeFormOccupant; if (adjacentLifeForm == null) { Move(targetCell); } } } } }
/// <summary> /// Set-up the second demo, by adding various objects (and walls) to PigWorld. /// </summary> public void SetupDemo2() { BoyPig boyPig = new BoyPig(PigWorld, new Position(0, 0)); GirlPig girlPig = new GirlPig(PigWorld, new Position(2, 0)); Tree tree1 = new Tree(PigWorld, new Position(0, 4)); Tree tree2 = new Tree(PigWorld, new Position(7, 5)); Wolf wolf = new Wolf(PigWorld, new Position(8, 8)); PigFood pigFood1 = new PigFood(PigWorld, new Position(0, 5)); PigFood pigFood2 = new PigFood(PigWorld, new Position(3, 3)); PigFood pigFood3 = new PigFood(PigWorld, new Position(6, 0)); CreateDemoWalls(); }
/// <summary> /// Set-up the first demo, by adding various objects (and walls) to PigWorld. /// </summary> public void SetupDemo1() { BoyPig boyPig = new BoyPig(PigWorld, new Position(5, 2)); GirlPig girlPig = new GirlPig(PigWorld, new Position(7, 7)); PigFood pigFood1 = new PigFood(PigWorld, new Position(1, 1)); PigFood pigFood2 = new PigFood(PigWorld, new Position(1, 5)); PigFood pigFood3 = new PigFood(PigWorld, new Position(2, 2)); PigFood pigFood4 = new PigFood(PigWorld, new Position(2, 5)); PigFood pigFood5 = new PigFood(PigWorld, new Position(3, 3)); PigFood pigFood6 = new PigFood(PigWorld, new Position(3, 4)); PigFood pigFood7 = new PigFood(PigWorld, new Position(3, 5)); PigFood pigFood8 = new PigFood(PigWorld, new Position(4, 4)); PigFood pigFood9 = new PigFood(PigWorld, new Position(4, 5)); PigFood pigFood10 = new PigFood(PigWorld, new Position(5, 4)); PigFood pigFood11 = new PigFood(PigWorld, new Position(5, 5)); CreateDemoWalls(); }
/// <summary> /// Rope-based look-for-food method /// </summary> private void LookForFoodUsingRope() { // Do not modify, delete, or move the debugAnimalAction line below, // or the Debug Info will not show correctly in the GUI, and that could be confusing. debugAnimalAction = "LookForFood"; bool pigFoodInCurrentCell = Cell.Exists(typeof(PigFood)); //If there is food in the current cell, eat it! if (pigFoodInCurrentCell) { PigFood food = (PigFood)Cell.PickUp(typeof(PigFood)); Eat(food); ClearRope(); } else { Echo nearestPigFood = FindNearest(typeof(PigFood)); if (nearestPigFood == null) { ClearRope(); WanderAround(); } else if (nearestPigFood != null) { Cell targetCell = Cell.GetAdjacentCell(nearestPigFood.direction); if (CanMove(nearestPigFood.direction) && targetCell.Exists(typeof(PigFood))) { DropRope(); Move(targetCell); } else if (CanMove(nearestPigFood.direction) && GetMyRopePiece(targetCell) == null) { DropRope(); Move(targetCell); } else if (!CanMove(nearestPigFood.direction) || GetMyRopePiece(targetCell) != null) { ExamineSurroundingCells(); } } } }
/// <summary> /// Set-up the third demo, by adding various objects (and walls) to PigWorld. /// </summary> public void SetupDemo3() { BoyPig boyPig = new BoyPig(PigWorld, new Position(4, 3)); GirlPig girlPig = new GirlPig(PigWorld, new Position(4, 5)); Tree tree1 = new Tree(PigWorld, new Position(0, 4)); Tree tree2 = new Tree(PigWorld, new Position(4, 4)); Tree tree3 = new Tree(PigWorld, new Position(8, 4)); Wolf wolf = new Wolf(PigWorld, new Position(0, 0)); PigFood pigFood1 = new PigFood(PigWorld, new Position(8, 0)); PigFood pigFood2 = new PigFood(PigWorld, new Position(8, 1)); PigFood pigFood3 = new PigFood(PigWorld, new Position(7, 0)); PigFood pigFood4 = new PigFood(PigWorld, new Position(1, 8)); PigFood pigFood5 = new PigFood(PigWorld, new Position(0, 8)); PigFood pigFood6 = new PigFood(PigWorld, new Position(0, 7)); PigWorld.FillVerticalGap(2, 5); PigWorld.FillVerticalGap(3, 1); PigWorld.FillVerticalGap(3, 6); PigWorld.FillVerticalGap(4, 1); PigWorld.FillVerticalGap(4, 6); PigWorld.FillVerticalGap(5, 1); PigWorld.FillVerticalGap(5, 6); PigWorld.FillVerticalGap(6, 2); PigWorld.FillHorizontalGap(1, 3); PigWorld.FillHorizontalGap(1, 4); PigWorld.FillHorizontalGap(1, 5); PigWorld.FillHorizontalGap(3, 1); PigWorld.FillHorizontalGap(3, 7); PigWorld.FillHorizontalGap(4, 0); PigWorld.FillHorizontalGap(4, 8); PigWorld.FillHorizontalGap(6, 3); PigWorld.FillHorizontalGap(6, 4); PigWorld.FillHorizontalGap(6, 5); }
private PigFood pigFood; // A reference to the pig food being viewed. /// <summary> /// Constructs a PigFoodView. /// /// The constructor must include a PigWorldView parameter so that all views /// created by the CreateView method(s) have similar signatures. /// </summary> /// <param name="pigFood"> the pig food to view. </param> public PigFoodView(PigWorldView pigWorldView, PigFood pigFood) : base(pigFood) { this.pigFood = pigFood; }