public bool Command(Command command) { if (command.Order != HuntTheWumpusDotNet.Command.Commands.Invalid) { if (command.Order == HuntTheWumpusDotNet.Command.Commands.Shoot) { TryToShootArrow(command.Direction); } else if (command.Order == HuntTheWumpusDotNet.Command.Commands.Go) { return MovePlayer(command.Direction); } else if (command.Order == HuntTheWumpusDotNet.Command.Commands.Rest) { Rest(); } if (gameRunning && map.WumpusCavern != null) map.PlaceItem(Mover.Move(map), MapItems.Wumpus); return true; } return false; }
public void InvalidDirection(Command.Directions command) { var message = String.Format("You can't go {0} from here.", ConvertDirectionToString(command)); console.WriteMessage(message); }
private static Command ConvertStringToCommand(String direction) { var command = new Command(); var convertedString = direction.Trim().ToUpperInvariant(); if (convertedString.StartsWith("GO ")) { command.Order = Command.Commands.Go; var directionString = convertedString.Substring(3); var convertedChar = directionString[0]; command.Direction = ConvertCharToDirection(convertedChar); return command; } if (convertedString.StartsWith("SHOOT ")) { command.Order = Command.Commands.Shoot; var directionString = convertedString.Substring(6); var convertedChar = directionString[0]; command.Direction = ConvertCharToDirection(convertedChar); return command; } if (convertedString.StartsWith("S ") && convertedString.Substring(2).Length > 0) { command.Order = Command.Commands.Shoot; var directionString = convertedString.Substring(2); var convertedChar = directionString[0]; command.Direction = ConvertCharToDirection(convertedChar); return command; } if (convertedString.StartsWith("S") && convertedString.Length > 1 && convertedString[1] != 'O') { command.Order = Command.Commands.Shoot; var convertedChar = convertedString[1]; command.Direction = ConvertCharToDirection(convertedChar); return command; } if ( convertedString.StartsWith("S") || convertedString.StartsWith("W") || convertedString.StartsWith("N") || convertedString.StartsWith("E")) { command.Order = Command.Commands.Go; command.Direction = ConvertCharToDirection(convertedString[0]); } if (convertedString.StartsWith("R")) { command.Order = Command.Commands.Rest; } return command; }
private static String ConvertDirectionToString(Command.Directions direction) { switch (direction) { case Command.Directions.East: return "east"; case Command.Directions.West: return "west"; case Command.Directions.North: return "north"; case Command.Directions.South: return "south"; default: throw new ArgumentException("Invalid Direction"); } }
protected void TryToShootArrow(Command.Directions direction) { if (player.OutOfArrows()) { presenter.OutOfArrows(); return; } if (PlayerIsPlaced()) { player.Shoot(); var arrowsCavern = FlyArrowToCavern(direction); if (arrowsCavern == PlayersCurrentCavern) { presenter.Suicide(); GameOver(); } else if (map.HasWumpusIn(arrowsCavern)) { presenter.WumpusHasBeenShot(); GameOver(); } else presenter.ArrowWasFired(); } }
private int FlyArrowToCavern(Command.Directions direction) { var arrowsCavern = (int) PlayersCurrentCavern; while (map.AvailableMoves(arrowsCavern).Contains(direction) && !map.HasWumpusIn(arrowsCavern)) { arrowsCavern = map.GetCavernForMove(arrowsCavern, direction); } map.PlaceItem(arrowsCavern, MapItems.Arrow); return arrowsCavern; }
protected bool MovePlayer(Command.Directions command) { if (PlayersCurrentCavern != null) { if (map.CanMove((int) PlayersCurrentCavern, command)) { map.MovePlayer(command); } else { presenter.InvalidDirection(command); return false; } } DisplayAvailableDirections(); if (CanSmellTheWumpus()) { presenter.WumpusCanSeeYou(); } PickUpAnyArrows(); DisplayArrowStatus(); return true; }
protected bool MoveRunsIntoWumpus(Command.Directions command) { var potentialCavern = map.GetCavernForMove((int) PlayersCurrentCavern, command); return map.HasWumpusIn(potentialCavern); }
public int GetCavernForMove(int cavern, Command.Directions direction) { return CavePathsFor(cavern)[direction]; }
public bool CanMove(int cavern, Command.Directions direction) { return CavePathsFor(cavern).ContainsKey(direction); }
public void AddPath(int start, int end, Command.Directions direction) { CavePathsFor(start)[direction] = end; CavePathsFor(end)[OppositeDirectionOf(direction)] = start; }
protected Command.Directions OppositeDirectionOf(Command.Directions direction) { switch (direction) { case Command.Directions.West: return Command.Directions.East; case Command.Directions.East: return Command.Directions.West; case Command.Directions.South: return Command.Directions.North; case Command.Directions.North: return Command.Directions.South; default: return Command.Directions.East; } }
public void MovePlayer(Command.Directions cavern) { if (HasPlayer()) { var originalCavern = (int) PlayersCurrentCavern(); if (cave[originalCavern].Paths.ContainsKey(cavern)) { var newCavern = cave[originalCavern].Paths[cavern]; cave[originalCavern].MapItems.Remove(MapItems.Player); cave[newCavern].MapItems.Add(MapItems.Player); } } }