【问题标题】:Beginner making a Console based RPG in C#, having some problems with room navigation初学者用 C# 制作基于控制台的 RPG,房间导航有一些问题
【发布时间】: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;

    }
}

【问题讨论】:

    标签: c# console-application


    【解决方案1】:

    看起来你在正确的路线上。您可能需要拥有一个包含所有游戏状态的类。它可能是这样的(只是一个粗略的伪代码让你开始)。

    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");
         }
      }
    }
    

    您需要了解玩家当前位置的信息,以及能够检查移动是否有效的可用房间,因此需要一个游戏类。您可以在主游戏循环中处理异常并通知用户。

    【讨论】:

      【解决方案2】:

      发现问题。在检查房间时,我只检查了一次坐标。这使得它认为所有房间都是有效的房间,只要它们符合一个标准。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多