如何更改使用的房间

时间:2014-09-04 15:51:47

标签: java data-structures

我已经对此进行过研究,到目前为止还没有找到我在网站上寻找的内容。我或多或少地尝试了一个基于半基础文本的冒险类型游戏,玩家选择了一个选项,如(l)ook,(w)ander,(le)leep等.I'我创建了Room的类,并初始化了它,所有其余的,并设置了描述等。在其中一个选项中,你进入了一个洞穴,我没有从其他人和我的老师那里学到足够的东西来允许你所在区域的描述发生变化。到目前为止,代码行如下:

public void Gameloop(Room startRoom)
{
    String input;
    boolean notDead = false;

    Room currentRoom = startRoom;

    while(!notDead)
    {
        //Shows description of the room
    input = JOptionPane.showInputDialog(null, currentRoom.getDescription() + " What do you do? \n(l)ook around\n(g)rab a stick\n(s)leep.");

这是你在森林里游戏的开始。描述正确,通过选择的进步是正确的。问题出在这个代码区域

    if(input.equalsIgnoreCase("l"))
    {
        input = JOptionPane.showInputDialog(null,"You see trees and a cave. Do you want to go into the casve? \n(y)es \n(n)o");
        if(input.equalsIgnoreCase("y"))
        {

            input = JOptionPane.showInputDialog(null,currentRoom.getDescription() + " Do you want to set up a camp?\n(y)es\n(n)o");

问题特别在于我根本没有学会如何实现房间变化,否则,游戏基础将是合理的,可行的,选项会更好地考虑,项目系统将在以后实施。基本上,我如何更改"房间"这个人在。注意,这个游戏没有GUI,字面意思是你为每个动作键入一个字母

1 个答案:

答案 0 :(得分:0)

我会在这里使用策略模式。我给你一些伪代码让你开始:),注意这不编译。 我们的想法是你制作一个IRoom界面。这将允许您根据自己的喜好制作多种不同类型的房间。每个房间都可以定义此房间可以执行的操作,因此您可以将某些操作添加到森林房间。 它还有一个确保房间执行操作的方法。这可能会导致房间变换,因此它将返回一个新的IRoom。

      public class Game {
          private IRoom currentRoom;

          public void Gameloop(IRoom startRoom){

          while(!notDead)
          {
            //Shows description of the room
            input = JOptionPane.showInputDialog(null, currentRoom.getDescription() + " What  do you do? " + dissplayPossibleActionsToUser(startRoom.getPossibleActionsInRoom()));

            Action chosenAction = dericeActionFromImput(input);
            currentRoom = startRoom.performAction(chosenAction);


          }

        }

     }

        public interface IRoom{
           //Returns all the actions the current room provides
           List<Actions> getPossibleActionsInRoom();

           //Does the action
           IRoom performAction(Action action);
        }

        public enum Actions{
           LOOK, WANDER, SLEEP;
        }


           public class Forest implements IRoom{

                public List<Actions> getPossibleActionsInRoom(){
                   return new ArrayList(Actions.wander);
                }

               public IRoom performAction(Action action){
                    if(action == wander){
                      return new HouseRoom();
                    }
                }
            }