Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // Create a Player
            Player p = new Player();

            p.Move(Position.North);
            Console.WriteLine(p.currentPosition);
            Console.ReadKey();
            p.Move(Position.South);
            Console.WriteLine(p.currentPosition);
            Console.ReadKey();
            p.Move(Position.East);
            Console.WriteLine(p.currentPosition);
            Console.ReadKey();
            p.Move(Position.West);
            Console.WriteLine(p.currentPosition);
            Console.ReadKey();
            p.Move(Position.South);
            Console.WriteLine(p.currentPosition);
            Console.ReadKey();


            // Now, try it using the State Pattern!
            Console.WriteLine("\n\n\n\n");
            Console.WriteLine("Let's try it using a state pattern!");
            OOPlayer p2 = new OOPlayer();

            p2.SetState(new North());  // First, we set the state of the player
            p2.Move();                 // Then, we execute some instructions with the player.
            Console.ReadKey();
            p2.SetState(new South());
            p2.Move();
            Console.ReadKey();
            p2.SetState(new West());
            p2.Move();
            Console.ReadKey();
            p2.SetState(new East());
            p2.Move();
            Console.ReadKey();
            p2.SetState(new West());
            p2.Move();
            Console.ReadKey();
            p2.SetState(new West());
            p2.Move();
            Console.ReadKey();
        }
Exemplo n.º 2
0
 public void Move(OOPlayer context)
 {
     context.SetState(new South());
     Console.WriteLine("Moved South!");
 }
Exemplo n.º 3
0
 public void Move(OOPlayer context)
 {
     context.SetState(new West());
     Console.WriteLine("Moved West young man!");
 }
Exemplo n.º 4
0
 public void Move(OOPlayer context)
 {
     context.SetState(this);
     Console.WriteLine("Didn't move");
 }
Exemplo n.º 5
0
 public void Move(OOPlayer context)
 {
     context.SetState(new East());
     Console.WriteLine("Moved East!");
 }
Exemplo n.º 6
0
 public void Move(OOPlayer context)
 {
     context.SetState(this);
     Console.WriteLine("moved North");
 }