private void wanderWolf(Animal animal) { int friendx, friendy; int closex, closey; int fdist = 0; Direction fd = Direction.Down; friendx = friendy = 0; closex = 0; closey = 0; d = 0; foreach (System.Drawing.Point an in animal.SharedView) { sheepx = sheepy = wolfx = wolfy = 0; sheepx = an.X; sheepy = an.Y; //there is a sheep wolfx = animal.X; wolfy = animal.Y; d = Math.Abs(wolfx - friendx); //get shortestd distance to a friend if (fdist == 0) { fdist = d; closex = friendx; closey = friendy; } else if (d < fdist) { fdist = d; closex = friendx; closey = friendy; } } if (fdist >= 3) { if (closex - wolfx < 0 && Math.Abs(closex - wolfx) >= 3) fd = Direction.Left; else if (closex - wolfx > 0 && Math.Abs(closex - wolfx) >= 3) fd = Direction.Right; else if (closey - wolfy < 0 && Math.Abs(closey - wolfy) >= 3) fd = Direction.Down; else fd = Direction.Up; } else fd = Direction.Down; animal.waitMove(fd, 300); }
public void escape(Animal animal) { int enemyx, enemyy; int closex, closey; int sheepx, sheepy; int fdist = 0; Direction fd = Direction.Up; enemyx = enemyy = 0; closex = closey = 0; sheepx = sheepy = 0; d = 0; foreach (System.Drawing.Point an in animal.SharedView) { sheepx = sheepy = wolfx = wolfy = 0; sheepx = an.X; sheepy = an.Y; //there is a sheep wolfx = animal.X; wolfy = animal.Y; sheepx = animal.X; sheepy = animal.Y; d = Math.Abs(sheepx - enemyx);// + Math.Abs(sheepy - enemyy); //get closest wolf if (fdist == 0) { fdist = d; closex = enemyx; closey = enemyy; } else if (d < fdist) { fdist = d; closex = enemyx; closey = enemyy; } else if (sheepx == 0) animal.waitMove(Direction.Down, 300); else if (sheepy == 0) animal.waitMove(Direction.Down, 300); } if (fdist <= 5) { if (closey - sheepy <= 0) fd = Direction.Down; if (closey - sheepy > 0) fd = Direction.Up; else if (closex - sheepx <= 0) fd = Direction.Right; else if (closex - sheepx > 0) fd = Direction.Left; else if (sheepx == 0) fd = Direction.Down; else if (sheepy == 0) fd = Direction.Down; } else fd = Direction.Up; animal.waitMove(fd, 300); }
private void hunt(Animal animal) { if (animal is Wolf) { shortestd = d = 0; //look at all the sheep and find the closest one foreach (System.Drawing.Point an in animal.SharedView) { sheepx = sheepy = wolfx = wolfy = 0; sheepx = an.X; sheepy = an.Y; //there is a sheep wolfx = animal.X; wolfy = animal.Y; d = Math.Abs(wolfx - sheepx) + Math.Abs(wolfy - sheepy); if (shortestd > d && d != 0) { shortestd = d; targetx = sheepx; targety = sheepy; } } if (sheepy > wolfy) animal.waitMove(Direction.Down, 300); else if (sheepy < wolfy) animal.waitMove(Direction.Up, 300); else if (sheepx < wolfx) animal.waitMove(Direction.Left, 300); else if (sheepx > wolfx) animal.waitMove(Direction.Right, 300); } else escape(animal); }