private EndState CheckTargetHit(Wumpus wumpus, IEnumerable <int> traversedRoom) { EndState endState = null; foreach (int r in traversedRoom) { var e = HitTarget(r, wumpus); if (e.IsGameOver) { endState = e; _map.RoomsArrowTraversed.Add(r); break; } _map.RoomsArrowTraversed.Add(r); } return(endState); }
// Traverses the given rooms or randomly selected adjacent rooms if the given rooms are not traversable. // Checks if the arrow hit the player, wumpus, or was a miss, and game state is set accordingly. private void ShootArrow(IReadOnlyCollection <int> roomsToTraverse, Wumpus wumpus, Action <EndState> gameOverHandler) { --CrookedArrowCount; var endState = CheckTargetHit(wumpus, Traverse(roomsToTraverse)); if (endState != null) { gameOverHandler(endState); } else { Log.Write(Message.Missed); Log.Write($"You now have {CrookedArrowCount} crooked arrows."); gameOverHandler(CrookedArrowCount == 0 ? new EndState(true, $"{Message.OutOfArrows}\n{Message.LoseMessage}") : new EndState()); } }
private EndState HitTarget(int currentRoom, Wumpus wumpus) { Log.Write(currentRoom.ToString()); EndState endState; if (RoomNumber == currentRoom) { endState = new EndState(true, $"{Message.ArrowGotYou}\n{Message.LoseMessage}"); } else if (wumpus.RoomNumber == currentRoom) { endState = new EndState(true, Message.WinMessage); wumpus.IsDiscovered = true; } else { endState = new EndState(); } return(endState); }
/// <summary> /// Requests the player how many and what rooms the arrow should traverse. /// The arrow traverses the traversable rooms or randomly selected adjacent ones and the /// arrows traversal path is checked for a hit to determine end game state. /// </summary> /// <param name="wumpusRoomNumber">current wumpus room number</param> /// <param name="gameOverHandler"></param> /// <returns>game end state result</returns> public void ShootArrow(Wumpus wumpus, Action <EndState> gameOverHandler) { OnRoomsToTraverseDo(rooms => ShootArrow(rooms, wumpus, gameOverHandler)); }