在Java冒险游戏中跟踪房间?

在Java冒险游戏中跟踪房间?,java,adventure,Java,Adventure,我正在尝试创建一个Java冒险游戏,你可以在其中移动北、南、东、西。但是,根据每个房间的方向布尔值是true还是false,您无法移动这些方向(即,如果该房间的北向为true,您可以向北移动)。我的问题是,我不知道如何跟踪玩家当前所在的房间/移动到新房间。什么是跟踪玩家当前所在房间并移动到新房间的理想方式 代码: import java.util.Hashtable; import java.util.Scanner; public class Adventure { // Creat

我正在尝试创建一个Java冒险游戏,你可以在其中移动北、南、东、西。但是,根据每个房间的方向布尔值是
true
还是
false
,您无法移动这些方向(即,如果该房间的北向为
true
,您可以向北移动)。我的问题是,我不知道如何跟踪玩家当前所在的房间/移动到新房间。什么是跟踪玩家当前所在房间并移动到新房间的理想方式

代码:

import java.util.Hashtable;
import java.util.Scanner;

public class Adventure {
    // Create each room
    Room1 Room1 = new Room1();
    Room2 Room2 = new Room2();
    Room3 Room3 = new Room3();
    Room4 Room4 = new Room4();
    Room5 Room5 = new Room5();
    Room6 Room6 = new Room6();
    Room7 Room7 = new Room7();
    Room8 Room8 = new Room8();
    Room9 Room9 = new Room9();
    Room10 Room10 = new Room10();
    boolean allIngredients = false;

    public void RoomSetup() {
        // Create HashTable and add rooms to the HashTable
        Hashtable<Integer, Room> RoomSet = new Hashtable<Integer, Room>();
        RoomSet.put(1, Room1);
        RoomSet.put(2, Room2);
        RoomSet.put(3, Room3);
        RoomSet.put(4, Room4);
        RoomSet.put(5, Room5);
        RoomSet.put(6, Room6);
        RoomSet.put(7, Room7);
        RoomSet.put(8, Room8);
        RoomSet.put(9, Room9);
        RoomSet.put(10, Room10);

        // Set Room 1 Properties
        Room1.setIntro(
                "You are in a dark cave. In the middle, there is a cauldron boiling. With a clasp of thunder, three witches suddenly appear before you.");
        Room1.setFirstVisit(
                "The witches speak in unison: \"Mortal, we have summoned thee, make haste! And go forth into the farrow\'d waste.Find eye of newt, and toe of frog,And deliver thus to this Scottish bog. Lizard\'s leg, and owlet\'s wing, And hair of cat that used to sing. These things we need t\' brew our charm; Bring them forth -and suffer no \'arm. Leave us and go!  \'Tis no more to be said,Save if you fail, then thou be stricken, dead.\"");
        Room1.setPreAction("The witches stand before you, glaring; they seem to be expecting something from you.");
        Room1.setPostAction(
                "The witches look at your items with suspicion, but decide to go through with the incantation of the spell: \"Take lizard\'s leg and owlet\'s wing, And hair of cat that used to sing. In the cauldron they all shall go; Stirring briskly, to and fro. When the color is of a hog, Add eye of newt and toe of frog. Bubble all i\' the charmed pot; Bubble all \'til good and hot. Pour the broth into a cup of stone, And stir it well with a mummy's bone.\" You take the resulting broth offered to you and drink... As the fog clears, you find yourself at a computer terminal; your adventure is at an end.");

        // Set Room 2 Properties
        Room2.setFirstVisit(
                "You're transported back in time ... you find yourself in Georgia during the midst of a congressional campaign.");
        Room2.setPreAction(
                "There is a campaign poster of Newt Gingrich, the Speaker of the House of Representatives, on the wall, with his large eyes looking right at you.");
        Room2.setPostAction("There is a defaced poster of Newt Gingrich on the wall.");

        // Set Room 3 Properties
        Room3.setFirstVisit(
                "You open the door and step in. It looks exactly like your own bedroom from back at home for some reason...");

        // Set Room 4 Properties
        Room4.setFirstVisit("You get transported to a Nickleback concert. Better get out quick!");

        // Set Room 5 Properties
        Room5.setFirstVisit(
                "You get teleported onto the death star. You can hear darth vader breathing heavily somewhere...");

        // Set Room 6 Properties
        Room6.setFirstVisit("You find yourself in a frozen tundra. you must be in Antartica or something...");

        // Set Room 7 Properties
        Room7.setFirstVisit(
                "You find yourself walking into a scene where the cast of Monty Python's Flying Circus is performing the \"Crunchy Frog\" sketch. You see the confectioner as he replies, \"If we took the bones out it wouldn't be crunchy now, would it?");
        Room7.setPreAction(
                "You see a box of \"Crunchy Frog\" chocolates, the contents of which contains a dozen nicely cleaned whole frogs that have been carefully hand-dipped in the finest chocolate.");
        Room7.setPostAction(
                "There's an onpen box of \"Crunchy Frog\" Chocolates with one of the frogs missing its leg");

        // Set Room 8 Properties
        Room8.setFirstVisit("You open the door into a room that's pure white. The door you came through dissapears.");

        // Set Room 9 Properties
        Room9.setFirstVisit(
                "As you step through the time portal, your head begins to spin you're disoriented and then awaken. You find yourself at the outside door of a dormitory kitchen. Listening, you hear the Chef yelling, \"Stop! Stop!\" while several cats inside are singing a serenade of the \"Meow Mix\" commercial theme. Suddenly, the repeated thump of a cleaver puts an abrupt end to the music.");

        // Set Room 10 Properties
        Room10.setFirstVisit(
                "You are in the kitchen. Looking out into the cafeteria, you see students reaching for Pepto-Bismol while trying to stomach the latest version of the Chef's Surprise. You see the Chef as he finishes dumping fresh meat into his 50-quart stewing pot.");
        Room10.setPreAction(
                "There are clumps of cat hair on the butcher's block. You hear the Chef muttering to himself, \"Prepared properly, cat tastes much like chicken...\"");
        Room10.setPostAction("There's strands of cat hair all over the floor, and the kitchen smells foul!");
    }

    public void Play() {
        Scanner sc = new Scanner(System.in);
        System.out.println(Room1.getIntro());
        while(allIngredients == false){
            System.out.println("What would you like to do?");
            String Question = sc.nextLine();
            if(Question.equalsIgnoreCase("quit")){
            System.exit(0);
        }
            if(Question.toLowerCase().contains("move")||Question.toLowerCase().contains("go")){
                Question = (Question.replaceAll(" ",""));
                move(Question);
            }
            }

    }

    private void move(String question) {
        if()

    }

}

我想到的想法是在房间之间创建一个节点结构,并添加一个指向player类的指针

例如:

public class Player {
    Room r;

    public Player() {
        r = new Room(/*Implementation of the room*/);
    }
}

public class Room {
    Room left;
    Room right;
    Room up;
    Room down;
    boolean Locked;

    public Room(Room up, Room down, Room left, Room right, boolean status) {
        this.up = up;
        this.down = down;
        this.left = left;
        this.right = right;
        Locked = status;
    }

    public Room getLeft() {
        return left;  //Return the left node of this current node
    }

    public boolean roomIsLocked() {
        return Locked;
    }

    //Or to see if you can move
    public boolean canMoveLeft() {
        if (getLeft() != null)
            return getLeft().isThisRoomOpen();
        return false;
    }

    public boolean canMoveDown(){
        if (getDown() != null)
            return getDown().isThisRoomOpen();
        return false;
    }
}
使用这种类型的结构,您可以使用players“getRoom()”方法获取播放器所在的房间,然后还可以创建“getLeft”等。对于
房间
课程,当你想移动你的播放器时,你可以说一些话:

player.setRoom(player.getRoom().getLeft());

创建一个玩家对象,该对象包含对玩家所在房间的引用

public class Player {
  Room currentLocation;
  ...
}
正如其他人所指出的,房间必须以某种方式相互连接。同样,您可以添加对“Room”对象的引用,如Room_north、Room_south等


您的问题很难更准确地回答,因为您的代码示例离一个正常工作的游戏太远了。我希望我的尝试能对您有所帮助。

也许某种链表或树结构会有所帮助,因此,给定任何一个房间,您都可以看到哪些房间导致了它。看起来您正在使用面向对象语言(Java)以命令式风格编程。也许最好的办法是定义Room类和Player类。玩家应该有一个指向房间的位置。你的房间应该布置在某种树上,并指向可以进入的房间。你知道,如果你想用面向对象的方式来做这件事。@NathanielFord这里的基本问题不是强制性与面向对象-问题是没有房间的模型/表示。首先,你需要房间之间的关系。。要知道播放机在哪里,只需使用与播放机关联的
Room currentRoom
变量即可。一旦建立了关系(并编写了适当的代码),就可以执行类似于
currentRoom.getAdjoiningRooms()
(或
world.getAdjoiningRooms(currentRoom)
等)的操作来找出可用的移动。我可能会重做大部分代码。很明显,我走错了方向。这个代码示例有点难闻。叫它
节点
而不是
房间
有什么意义?命名类
player
而不是
player
的意义在哪里?为什么示例方法应该返回左
,即使没有这样的字段?为什么想要使用
movable
方法的开发人员应该关心索引?这是一个肮脏的实现细节,不应该向调用方法公开。提供像
canGoLeft
canGoUp
这样的方法要干净得多。值得抱怨。至于移动方法,有一种方法使用指数来关联它想要检查的方向,比有4种不同的方法更干净。不,这不是更干净,只是更短。但是,如果您认为最好强制开发人员重新检查每个索引的含义,并确保传递的数组与传递的文件室匹配,而不是删除该数组并使用显式方法进行“canGo…”检查,那么这取决于您。那么我可以阻止你了:啊,可能是我的编程竞赛速度的坏习惯干扰了我。我做了你建议的改变。为ie:getRoom()添加其他访问方法是否直观?我喜欢为房间添加“锁定”功能的想法,但我不会称之为
canIMoveHere
/
can
,因为不清楚这意味着什么(为什么不能进入房间)。名称
locked
和方法
isLocked
将更清楚为什么无法进入房间。但是,
isThisRoomOpen
也可以工作。那
getRoom
应该在哪里?在
Player
类中?然后
getCurrentRoom
会更好一些:P.顺便说一句,您可以直接使用“canMove..”方法中的
left
down
字段。
public class Player {
  Room currentLocation;
  ...
}