コード例 #1
0
 public Player(IMovePlayer start)
 {
     current          = start;
     onCurrentChanged = () => { };
     onDone           = () => { };
     onWait           = () => { };
 }
コード例 #2
0
        public void StartMaze(IMovePlayer maze)
        {
            bool continuePlay = true;
            var  player       = new Player(maze);

            Action displayCurrentPosition = () => Console.WriteLine("Player is now in room {0}.", player.CurrentPosition);

            player.OnCurrentChanged = displayCurrentPosition;
            player.OnWait           = () => Console.WriteLine("Player is still in room {0}.", player.CurrentPosition);
            player.OnDone           = () =>
            {
                continuePlay = false;

                Console.WriteLine("Player has exited the maze.");
            };

            while (continuePlay)
            {
                Console.Write("Enter command (North, East, South, West, Current, Exit): ");
                string line = Console.ReadLine();

                IEnumerable <string> tokens = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                              .Where(IsValid);

                foreach (string token in tokens)
                {
                    if (northTokens.Any(Matches(token)))
                    {
                        player.MoveNorth();
                    }
                    else if (eastTokens.Any(Matches(token)))
                    {
                        player.MoveEast();
                    }
                    else if (southTokens.Any(Matches(token)))
                    {
                        player.MoveSouth();
                    }
                    else if (westTokens.Any(Matches(token)))
                    {
                        player.MoveWest();
                    }
                    else if (exitTokens.Any(Matches(token)))
                    {
                        continuePlay = false;
                    }
                    else if (currentTokens.Any(Matches(token)))
                    {
                        displayCurrentPosition();
                    }
                    else
                    {
                        throw new ArgumentException(string.Format("Unknown token {0}.", token), "token");
                    }
                }
            }
        }
コード例 #3
0
        public OpenWall(IMovePlayer destination)
        {
            if (destination == null)
            {
                throw new ArgumentNullException("destination", "destination is null.");
            }

            this.destination = destination;
        }
コード例 #4
0
        /// <summary>
        /// Prompts the user for a password if necessary and attempts
        /// to enter the portal.
        /// </summary>
        /// <returns></returns>
        public BoolMessageResult TryEnter(IMovePlayer movePlayer)
        {
            string pwdText = string.Empty;

            if (this.pwd != null)
            {
                pwdText = this.userPrompt.PromptText("Enter Portal", "What's the password? ");
            }

            return(this.TryEnter(movePlayer, pwdText));
        }
コード例 #5
0
        void IMovable.MoveTo(IMovePlayer room)
        {
            if (room == null)
            {
                throw new ArgumentNullException("room", "room is null.");
            }

            current = room;

            onCurrentChanged();
        }
コード例 #6
0
        public void Execute()
        {
            Console.Write("Enter maze type ({0}): ", string.Join(", ", validMazeTypes));
            string mazeType = Console.ReadLine();

            if (string.IsNullOrEmpty(mazeType))
            {
                mazeType = SquareSnake;
            }
            if (!validMazeTypes.Any(validMazeType => StringComparer.OrdinalIgnoreCase.Compare(validMazeType, mazeType) == 0))
            {
                throw new ArgumentException(string.Format("Unknown maze type {0}.", mazeType));
            }

            int  length  = 0;
            bool invalid = true;

            while (invalid)
            {
                Console.Write("Enter a length: ");
                string input = Console.ReadLine();
                if (string.IsNullOrEmpty(input))
                {
                    length = 4;

                    break;
                }
                if (input.StartsWith("q", StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }

                invalid = !int.TryParse(input, out length);
            }

            ICreateMazes mazeFactory = null;

            if (StringComparer.OrdinalIgnoreCase.Compare(mazeType, SquareSnake) == 0)
            {
                mazeFactory = new SnakeSquareMazeFactory(length);
            }
            else if (StringComparer.OrdinalIgnoreCase.Compare(mazeType, Simple) == 0)
            {
                mazeFactory = new SimpleMazeFactory(length);
            }

            IMovePlayer maze = mazeFactory.Create();

            mazeClient.StartMaze(maze);
        }
コード例 #7
0
        public BoolMessageResult TryEnter(IMovePlayer movePlayer, string pwd)
        {
            if (this.pwd != null)
            {
                if (this.failedAttempts >= this.maxAttempts)
                {
                    var msg = string.Format("This portal has closed because you have tried {0} incorrect passwords to access it. Tough luck!", this.failedAttempts);
                    return(new BoolMessageResult(false, msg));
                }

                if (string.IsNullOrEmpty(pwd))
                {
                    return(new BoolMessageResult(false, "This portal requires a password"));
                }
                else if (!this.pwd.IsMatch(pwd))
                {
                    this.failedAttempts++;
                    return(new BoolMessageResult(false, "That is not the correct password for this portal"));
                }
            }

            return(movePlayer.MoveTo(this.destination));
        }