public Player(IMovePlayer start) { current = start; onCurrentChanged = () => { }; onDone = () => { }; onWait = () => { }; }
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"); } } } }
public OpenWall(IMovePlayer destination) { if (destination == null) { throw new ArgumentNullException("destination", "destination is null."); } this.destination = destination; }
/// <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)); }
void IMovable.MoveTo(IMovePlayer room) { if (room == null) { throw new ArgumentNullException("room", "room is null."); } current = room; onCurrentChanged(); }
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); }
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)); }