예제 #1
0
        protected override bool CanMove(Ocean aquarium, Point move)
        {
            var fishAtLocation = aquarium.GetFishAt(Location.X + move.X, Location.X + move.Y);

            return(fishAtLocation == this || fishAtLocation == null ||
                   fishAtLocation is SmallFish);
        }
예제 #2
0
		static void Main(string[] args)
		{
			var ocean = new Ocean();
			var random = new Random();


			var fishes = Enumerable.Range(0, 20)
				.Select(_ => new Point(random.Next(80), random.Next(25)))
				.Select(pt => random.Next(2) == 0
					? (Fish) new BigFish(pt)
					: new SmallFish(pt)).ToArray();

			foreach (var fish in fishes)
			{
				ocean.AddFish(fish);
			}

			while (true)
			{
				Console.Clear();
				ocean.Render(f =>
				{
					Console.SetCursorPosition(f.Location.X, f.Location.Y);
					Console.Write(f.ToString());
				});

				foreach (var fish in fishes)
				{
					fish.Move(ocean);
				}

				Thread.Sleep(TimeSpan.FromSeconds(1));
			}
		}
예제 #3
0
        protected override void MoveTo(Ocean ocean, Point location)
        {
            var fishAtLocation = ocean.GetFishAt(location.X, location.Y);

            if (fishAtLocation != null)
            {
                ocean.Remove(fishAtLocation);
            }

            ocean.MoveTo(this, location);
        }
예제 #4
0
        public void Move(Ocean ocean)
        {
            var potentialMoves = (from x in Enumerable.Range(-1, 3)
                                  from y in Enumerable.Range(-1, 3)
                                  let m = new Point(x, y)
                                          where ocean.InBounds(Location.X + m.X, Location.Y + m.Y)
                                          where CanMove(ocean, m)
                                          select m).ToArray();

            var move = potentialMoves[Random.Next(potentialMoves.Length)];

            var newLocation = new Point(Location.X + move.X, Location.Y + move.Y);

            MoveTo(ocean, newLocation);
            Location = newLocation;
        }
예제 #5
0
 protected override void MoveTo(Ocean ocean, Point location)
 {
     ocean.MoveTo(this, location);
 }
예제 #6
0
 protected abstract bool CanMove(Ocean aquarium, Point move);
예제 #7
0
 protected abstract void MoveTo(Ocean ocean, Point newLocation);