到目前为止,我已经实现了:北,东,南和西的基于命令的移动,查看命令以获取房间描述,帮助命令以获取说明,还有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;
}
}
发现了问题。当检查房间时,我只检查了一次坐标。这使所有房间符合其标准即为有效房间。
看起来您处在正确的位置。您可能需要拥有一个包含游戏所有状态的类。可能是这样的(只是粗略的伪代码可以帮助您入门)。
public class Dungeon()
{
public List<Room> Rooms {get; set;}
public Player Player {get; set;}
public void AddRoom(Room room)
{
// add rooms in here - can check none have overlapping coordinates
if (noOverlap)
{
Rooms.Add(room);
}
}
public void MovePlayer(Direction direction)
{
// get the new coordinates if the player moves
// check the list of rooms to see if there is one there
if (roomExists)
{
Player.Move(direction);
}
else
{
throw new Exception("No room to move to");
}
}
}
您需要知道玩家当前位置以及可用来检查动作是否有效的房间的知识,因此需要游戏的课程。您可以在主游戏循环中处理异常并通知用户。