将分配给对象的一个​​变量更改为另一个对象?

时间:2015-12-09 01:32:27

标签: java oop adventure

我正在尝试创建一个冒险游戏,用户可以在其中输入北,南,东和西,以便移动到不同的房间。所以我有一个玩家类,它有一个Currentroom变​​量,并将一个房间分配给它作为默认房间。我想要做的是当玩家进入一个方向时,它会将玩家的Currentroom变​​量更新为另一个房间对象。我怎么做这样我可以在房间里导航?我将发布冒险课程,以及其中一个房间的示例。

冒险课程:

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();
    Player player = new Player();
    boolean allIngredients = false;

    public void RoomSetup() {

        // 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.equalsIgnoreCase("go") || Question.equalsIgnoreCase("move")) {
                Question = (Question.replaceAll(" ", ""));
                Move(Question);
            }
        }

    }

    private void Move(String question) {
        player.CurrentRoom = question;

    }
}

Room1(起居室):

public class Room1 extends Room {

        String intro;
        Room3 north;
        Room8 south;
        Room5 east;
        Room6 west;

        String[] Cauldreon = new String[5];

        public String getIntro() {
            return intro;
        }

        public void setIntro(String intro) {
            this.intro = intro;
        }

    }

玩家类:

public class Player {
    Room1 CurrentRoom = new Room1();
}

1 个答案:

答案 0 :(得分:1)

试试房间:

public class Room
{
  // define constructor and anything else you need
  // such as whether this room was visited already

  // assign different directions from the room
  Room getExit( Direction dir )
  {
    return exits.get( dir );
  }

  void addExit( Direction dir, Room room )
  {
    exits.put( dir, room );
  }

  private Map<Direction,Room> exits = new HashMap<>();
}

设置场景时,请执行以下操作:

Room room1 = new Room( ... );
Room room2 = new Room( ... );
Room room3 = new Room( ... );

然后设置每个房间的连接/出口:

room1.addExit( Direction.NORTH, room2 );
room1.addExit( Direction.WEST, room3 );

room2.addExit( Direction.SOUTH, room1 );

room3.addExit( Direction.EAST, room1 );

当你想向一个方向移动时,你会做这样的事情:

Room nextRoom = player.getCurrentRoom().getExit( dir );
if( nextRoom == null )
  {
    System.out.println( "You bump into a wall. Ouch!" );
  }
else
  {
    player.setCurrentRoom( nextRoom );
    System.out.println( "You arrive in "+nextRoom );
  }