【发布时间】:2020-05-21 03:05:47
【问题描述】:
到目前为止,我已经实现了:基于命令的北、东、南和西移动,用于房间描述的 Look Command,用于说明的 Help Command,以及用于测试的 5 个加号形状的房间。 现在,我正在努力让玩家在没有空间的情况下无法朝某个方向移动。我的解决方案适用于线性移动,但如果玩家向北,然后向西,他们能够进入一个不存在的房间,并且编译器在下次尝试计算移动时抛出异常,因为当前房间为空.出于某种原因,我的房间检测功能并不完全正常。 这是我的代码:
主要:
class Program
{
static void Main(string[] args)
{
Room currentRoom;
List<Room> rooms = new List<Room>();
Player player = new Player();
//Rooms
Room startingRoom = new Room("Starting room", "This is the starting room", 0, 0);
Room northRoom = new Room("North room", "This is the north room", 0, 1);
Room northMostRoom = new Room("North room", "This is the north room", 0, 2);
Room eastRoom = new Room("East room", "This is the east room", 1, 0);
Room southRoom = new Room("South room", "This is the south room", 0, -1);
Room westRoom = new Room("West room", "This is the west room", -1, 0);
//Add and set starting room as current room
rooms.Add(startingRoom);
currentRoom = startingRoom;
//Adding rooms
rooms.Add(northRoom);
rooms.Add(northMostRoom);
rooms.Add(eastRoom);
rooms.Add(southRoom);
rooms.Add(westRoom);
Console.WriteLine("Welcome to the Dungeon. This is the starting room.");
GetHelp();
Console.WriteLine("There are four exits. Which way do you want to go?");
while (true)
{
HandleInput(player, ref currentRoom, rooms);
}
}
private static void GetHelp()
{
Console.WriteLine("You can move to locations by entering 'North', 'East', 'South', and 'West'.");
Console.WriteLine("You can also examine the room by entering 'Look'.");
Console.WriteLine("Type 'Help' at any time to view instructions.");
}
private static Room SetCurrentRoom(List<Room> rooms, Player player)
{
foreach (var room in rooms)
{
if (room.XPos == player.PlayerXPos && room.YPos == player.PlayerYPos)
{
return room;
}
}
return null;
}
private static void HandleInput(Player player, ref Room currentRoom, List<Room> rooms)
{
var input = Console.ReadLine();
if (input == "North")
{
if (CheckIfRoomToNorth(currentRoom, rooms))
{
player.Move(Direction.Directions.North);
currentRoom = SetCurrentRoom(rooms, player);
}
else
{
Console.WriteLine("Cannot move north.");
}
}
else if (input == "East")
{
if (CheckIfRoomToEast(currentRoom, rooms))
{
player.Move(Direction.Directions.East);
currentRoom = SetCurrentRoom(rooms, player);
}
else
{
Console.WriteLine("Cannot move to east.");
}
}
else if (input == "South")
{
if (CheckIfRoomToSouth(currentRoom, rooms))
{
player.Move(Direction.Directions.South);
currentRoom = SetCurrentRoom(rooms, player);
}
else
{
Console.WriteLine("Cannot move to south.");
}
}
else if (input == "West")
{
if (CheckIfRoomToWest(currentRoom, rooms))
{
player.Move(Direction.Directions.West);
currentRoom = SetCurrentRoom(rooms, player);
}
else
{
Console.WriteLine("Cannot move to west.");
}
}
else if (input == "Help")
{
GetHelp();
}
else if (input == "Look")
{
var currentRoomDescription = currentRoom.RoomDescription;
Console.WriteLine(currentRoomDescription);
}
else
{
Console.WriteLine("Invalid input.");
}
}
private static bool CheckIfRoomToNorth(Room currentRoom, List<Room> rooms)
{
foreach (var room in rooms)
{
if (currentRoom.YPos == (room.YPos - 1))
{
return true;
}
}
return false;
}
private static bool CheckIfRoomToEast(Room currentRoom, List<Room> rooms)
{
foreach (var room in rooms)
{
if (currentRoom.XPos == (room.XPos - 1))
{
return true;
}
}
return false;
}
private static bool CheckIfRoomToSouth(Room currentRoom, List<Room> rooms)
{
foreach (var room in rooms)
{
if (currentRoom.YPos == (room.YPos + 1))
{
return true;
}
}
return false;
}
private static bool CheckIfRoomToWest(Room currentRoom, List<Room> rooms)
{
foreach (var room in rooms)
{
if (currentRoom.XPos == (room.XPos + 1))
{
return true;
}
}
return false;
}
}
玩家:
public class Player
{
public int Health { get; set; }
public string Name { get; set; }
public int PlayerXPos { get; set; }
public int PlayerYPos { get; set; }
public void Move(Direction.Directions direction)
{
switch (direction)
{
case Direction.Directions.North:
PlayerYPos += 1;
Console.WriteLine("Moving north");
break;
case Direction.Directions.East:
PlayerXPos += 1;
Console.WriteLine("Moving east.");
break;
case Direction.Directions.South:
PlayerYPos -= 1;
Console.WriteLine("Moving south.");
break;
case Direction.Directions.West:
PlayerXPos -= 1;
Console.WriteLine("Moving west.");
break;
}
}
}
方向:
public class Direction
{
public enum Directions
{
North,
East,
South,
West
}
}
房间:
public class Room
{
public string RoomName { get; set; }
public string RoomDescription { get; set; }
public int XPos { get; set; }
public int YPos { get; set; }
public Room(string roomName, string roomDescription, int xPos, int yPos)
{
this.RoomName = roomName;
this.RoomDescription = roomDescription;
this.XPos = xPos;
this.YPos = yPos;
}
}
【问题讨论】: