通过阵列进行房间导航的机制 [英] Mechanics of Room Navigation through Arrays

查看:65
本文介绍了通过阵列进行房间导航的机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#进行编程的知识,并且同样在尝试开发一个简单的基于文本的游戏,该游戏使用户可以使用方向按钮在一系列文本描述的房间中导航.首先,我有三个类-设置/控制类(主要形式),播放器类和房间类.

对于我的玩家班级,我想提供一个玩家所包围房间的列表,包括该玩家当前所在的房间.这会将玩家的视野"限制在他周围的基本方向上/她.

I am experimenting with my knowledge of programming in C#, and I am likewise trying to develop a simple, text-base game that lets a user navigate through a series of text-described rooms with directional buttons. First, I have three Classes - a setup/control class (the main form), a Player class and a Room Class.

For my player class, I thought I''d like to have a list of rooms the player is surrounded by, including the room the player is currently in. This would limit the "view" of the player to just the cardinal directions around him/her.

public class Player
    {
        private Int32 _playerCurrentRoom;
        ArrayList rooms = new ArrayList();
        public Player(Int32 playerCurrentRoom)
        {
            _playerCurrentRoom = playerCurrentRoom;
        }
        public Int32 MovePlayer(String direction, Room currentRoom)
        {
            Int32 heading;
            switch (direction)
            {
                case "North":
                    heading = currentRoom.ConnectNorth;
                case "East":
                    heading = currentRoom.ConnectEast;
                    break;
                case "South":
                    heading = currentRoom.ConnectSouth;
                    break;
                case "West":
                    heading = currentRoom.ConnectWest;
                    break;
                default:
                    heading = currentRoom.RoomNumber;
                    break;
            }
            return heading;
        }
    }



对于我的房间类别,我指出房间的编号,并用整数表示房间周围的房间.房间的"_roomNumber"字段和玩家的"_playerCurrentRoom"字段也应该相同,以便每次玩家进入房间时,表格的文本框都会用房间的描述进行更新.另外,请注意,MovePlayer方法采用String方向,该方向从用户单击表单的按钮向下传递.



For my Room class, I indicate the room''s number and indicate the rooms around it with an integer. The Room''s field "_roomNumber" and the Player''s field "_playerCurrentRoom" should also be the same, such that the textBox of the form is updated with the description of the room each time a player moves into it. Also, note that the MovePlayer method takes a String direction which is handed down from the button that the user clicks on the form.

public class Room : IPrintable
    {
        // assignents for what rooms surround room you are currently in
        private Int32 _connectNorth;
        private Int32 _connectEast;
        private Int32 _connectSouth;
        private Int32 _connectWest;
        private Int32 _roomNumber;
        private String _description;
        private String _roomItems;
        private String _roomActors;
        ArrayList itemsInRoom = new ArrayList();
        // Initalize a Room with essential values
        public Room(Int32 connectNorth, Int32 connectEast, Int32 connectSouth, Int32 connectWest, Int32 roomNumber, String description)
        {
            _connectNorth = connectNorth;
            _connectEast = connectEast;
            _connectSouth = connectSouth;
            _connectWest = connectWest;
            _roomNumber = roomNumber;
            _description = description;
        }
        // properties to be used for editor
        public Int32 ConnectNorth   { get; set; }
        public Int32 ConnectEast    { get; set; }
        public Int32 ConnectSouth   { get; set; }
        public Int32 ConnectWest    { get; set; }
        public Int32 RoomNumber     { get; set; }
        public String Description   { get; set; }
        public ArrayList ItemsInRoom { get; set; }


}

我的问题所在是如何将两者链接在一起,以便我可以用Room对象填充Player的ArrayList(或通用List),并遍历该列表以操纵播放器.基本上,我不知道该如何实现.所以我正在寻找一些输入.可能是我的班级也建立错了.无论如何,请让我知道您的想法.

与往常一样,我感谢您的帮助.


}

Where my problem lies is how to link the two together such that I can populate the Player''s ArrayList (or generic List) with Room objects and iterate over the list to maneuver the player. Basically, I don''t know how to achieve this. So I am looking for some input. And it may be that my classes are built wrong as well. Whatever the case, let me know what your thoughts are.

As always, I appreciate the help.

推荐答案

这比您制作起来容易得多.请注意,我在此代码块中直接输入了此代码,因此请注意不要使用愚蠢的错别字.

首先,您不是在使用类型,而是在使用类.
This is much simpler than you''re making it. Note I typed this code right in this code block so beware of stupid typos.

First off, you''re not using types, but instead the classes.
Int32

int

类型的类引用.

String

string

相同.


如果您的房间等级简化为:

.


If your room class is simplified down to:

public class Room : IPrintable
{
    public int RoomID { get; set; }
    public string RoomDescription {get;set;}
    public List<room> ConnectedRooms {get;set;}

    //This should be used to get the description for a specific room.
    private string GetDescriptionById(int roomID)
    {
         return "A big scary room!";
    }
    //Used to get the connected Roomclasses (and their subsequent connected rooms)
    private List<room> GetConnectedRooms(int roomID)
    {
        return someDatabaseObject.GetRoomsList();
    }

    public Room()
    {
        //Empty constructor logic.  We need to initialize the List no matter what.
        //to avoid object not set to instance of object Exceptions.
        ConnectedRooms = new List<room>();
    }
    public Room(int _id)
    {
        //Let''s construct a room by ID.
        ID = _id;
        Description = GetDescriptionById(_id);
        ConnectedRooms = GetConnectedRoomsById(_id);
    }
}
</room></room></room>



现在,因为我们有一个空的构造函数,所以在获取RoomList时,您可以做一些具有EMPTY连接的房间列表的漂亮魔术.这样,您就不必存储庞大的LinkedLIst类型构造.



还请注意我如何(使用匿名getter和setters)不需要显式的私有变量.方便.

通过将ConnectedRooms直接附加到您的房间类别,可以直接从任何Room实例中指定Connected Rooms列表.



Now, because we have that empty constructor, when getting the RoomList you can do some nice magic that has the EMPTY connected room list. That way you don''t have to store a huge LinkedLIst type construction.



Also notice how (with the anonymous getter and setters) I don''t need the private variables explicitly. That''s handy.

By attaching the ConnectedRooms to your room class directly, you can specify the List of Connected Rooms directly from any instance of Room.

var r = new Room();
r.ConnectedRooms(); //Will be empty.  This is an orphan room only GMs can get into.


您在此处遇到设计问题.除非有人知道您正在创建的游戏,否则无法真正提供良好的解决方案.请更新您的问题,告诉我们什么是播放器和房间.

现在,让我们看看您已经拥有的代码:

1.不需要MoverPlayer方法.当用户单击方向按钮时,您可以直接设置标题的值,而不用发送字符串然后再进行大小写切换.
2.您正在使用ArrayList.请停下.我们已经不在那个时代了.使用集合或列表(通用).
3.在房间"类中,私人成员与公共财产之间没有任何联系.您不需要私人成员,因为我们现在拥有自动属性.
4.您真的需要它们是整数吗?枚举有帮助吗?
5.再次,ArrayList!
6.为什么使用IPrintable?顺便问一下是什么? devexpress是其中之一吗?为什么需要它?

您应该暂时停止编写代码,然后重新考虑设计.如果继续这样,您可能最终将一无所获.花一些时间,可能会远离此代码/应用程序并重新开始.有时会有所帮助. :)
You are struggling with design issues here. Unless someone knows the game you are creating, cannot really provide a good solution. Please update your question to tell us what exactly are player and rooms.

Now, let''s see the code you already have:

1. There is no need of MoverPlayer method. When the user clicks on the direction buttons, you can directly set the value for heading rather than sending a string and then doing switch case.
2. You are using ArrayList. Please stop. We are not in that era anymore. Use Collection or List (generic).
3. In the Room class, there is no connection between the private members and there public properties. You do not need private members since we have automatic properties now.
4. Do you really need them to be integers? Could an enumeration help?
5. Again, ArrayList!
6. Why IPrintable? And what is it by the way? Is the devexpress one? Why do you need it?

You should really stop writing code for now and rethink about the design. If you continue like this, you may end up nowhere. Spend some time, may be away from this code/application and start fresh. It helps sometimes. :)


好的,您完全忽略了我给您的过去问题的解决方案.请参见.您没有问我任何后续问题.您重新发布了问题,而不是100%相同,但这实质上是重新发布.

您仍在基于立即字符串常量(硬编码)使用此switch语句.您重复代码.原则上,这不是编程的工作方式.这是不可修复的.

您只有两种选择:改变主意并开始学习或现在离开该行业.


我希望这个答案能获得通过.这是OP的投票(不是吗?如果您不想要,您可能不会回答),这只会使我对我的评估更加确定.我能闻到这种情况.

同时,我会很高兴知道自己做错了.但是,只有当OP改变这种态度并开始建设性地回应批评并真正开始学习一些东西时,我才能看到这一点.事实并非如此,离开该行业将是唯一可行的解​​决方案.无论哪种方式,都没有更多选择.

抱歉,
—SA
OK, you completely ignored my solution I''ve given you to your past question. Please see. You did not ask me any follow-up questions. You re-posted the question, not 100% the same, but this is essentially a re-post.

You still using this switch statement based on immediate string constants (hard-coded). You repeat code. This is not how programming works in principle. This is not fixable.

You have only two choices: change your mind and start learning or leave the industry now.


I expected the down-votes on this answer. It this is a vote by OP (isn''t it? you may not answer if you don''t want), it would only makes me even more sure about my assessment. I can smell such cases.

At the same time I would be more happy to know that I was wrong. But I can only see this if OP changes this attitude and starts respond to criticism constructively and actually start learning something. It it is not happening, leaving the industry would be the only viable solution. Either one way or another, no more options.

Sorry,
—SA


这篇关于通过阵列进行房间导航的机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆