Esempio n. 1
0
        private static World CreateWorld()
        {
            Random random = new Random();
              var content = new List<ActorWithLocationAndBehaviour>();

              var islandInAscii = @"
            WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
            WWWWWWWWWPPPPPPPPPPPPPPPWWWWWWWWWW
            WWWWPPPPPPPPPPPPPPPPPPPPPPPPWWWWWW
            WWWPPPPPPPPPPPPPPPPPPPPPPPPPPPPWWW
            WWWPPPPPPPPPPPPPPPPPPPPPPPPPPPPWWW
            WWWPPPPPPPPPPPPPPPPPPPPPPPPPPPPWWW
            WWWPPPPPPPPPPPPPPPPPPPPPPPPPPPPWWW
            WWWPPPPPPPPPPPPPPPPPPPPPPPPPPPPWWW
            WWWWPPPPPPPPPPPPPPPPPPPPPPPPWWWWWW
            WWWWWWWWWPPPPPPPPPPPPPPPWWWWWWWWWW
            WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW";

              var landscapeArray = islandInAscii.Split('\n').Skip(1).Select(
            row => row.Trim().ToCharArray()).ToArray();

              Landscape[,] landscape = new Landscape[landscapeArray[0].Length, landscapeArray.Length];

              for (int i = 0; i < landscapeArray.Length; i++)
              {
            for (int j = 0; j < landscapeArray[0].Length; j++)
            {
              var location = new Location(j, i);

              switch (landscapeArray[i][j])
              {
            case 'W':
              landscape[j, i] = new Water();
              break;

            case 'P':
              landscape[j, i] = new Plain();
              break;
              }
            }
              }

              var person = new Person();
              var personLocation = new Location(landscape.GetLength(0)/2, landscape.GetLength(1)/2);
              content.Add(new ActorWithLocationAndBehaviour(
            person,
            personLocation,
            new Behaviour(new PersonScript(person, personLocation).CollectWood)));

              return new World(landscape, content);
        }
Esempio n. 2
0
        public Tuple<Activity, Behaviour> CollectWood(WorldView state)
        {
            if (state.Location != home && state.CanCollect<Wood>() > 0)
              {
            lastWoodSource = state.Location;
            return Do.Collect<Wood>().Then(ReturnHome);
              }

              if (state.CanHarvest<Wood>() > 0)
              {
            return Do.Harvest<Wood>().Then(CollectWood);
              }

              if (lastWoodSource != null && lastWoodSource != state.Location)
              {
            return Move.Towards(lastWoodSource).Then(CollectWood);
              }

              lastWoodSource = null;
              return FindWood(state);
        }
Esempio n. 3
0
 public PersonScript(Person me, Location home)
 {
     this.me = me;
       this.home = home;
 }
 public void Behave(World state)
 {
     var worldView = new WorldView(state, location);
       behaviour = behaviour.Invoke(actor, worldView);
       location = worldView.Location;
 }
 public ActorWithLocationAndBehaviour(Actor actor, Location location, Behaviour behaviour)
 {
     this.actor = actor;
       this.location = location;
       this.behaviour = behaviour;
 }
Esempio n. 6
0
 public static Move Towards(Location destination)
 {
     return new Move(destination);
 }
Esempio n. 7
0
 public Move(Location destination)
 {
     this.destination = destination;
 }