(Java)文本冒险:如果输入输入,则关闭游戏

时间:2015-05-07 17:44:25

标签: java

我正在尝试修改我朋友在Java中的简单文本冒险。我试图在菜单中添加一个'quit'选项,并修改main()方法中的循环,以便在用户输入此命令时退出。

但无论输入是什么,此代码都会从一开始就停止游戏:

while (playerLocation==10);
            System.out.println("You won the game");
            break;

这会产生错误:

while(scanner.equals("9")) {
            System.out.println("You have quit the game.");
            break;
        }

以下完整代码:

package practice;

import java.util.Scanner;

public class practice 
{

private final int NO_EXIT   =   99999; // indicates that there is no exit in that direction

private int map[][]         =   {{NO_EXIT,1,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT},       //  1
                                {2,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT},        //  2
                                {NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,3,NO_EXIT,NO_EXIT},        //  3
                                {NO_EXIT,NO_EXIT,4,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT},                //  4
                                {NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,5},        //  5
                                {NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,6,NO_EXIT,NO_EXIT},                //  6
                                {NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,7},        //  7
                                {NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,8,NO_EXIT},        //  8
                                {9,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT},        //  9
                                {NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT}}; //  10


private String description[] = {"(Stage 1) stranded in dead-looking forest. You are trying to find your way out of here. You then see a pathway heading east.",
                                "(Stage 2) now in a large abandoned military base. There is no other way around and you see another pathway heading north.",
                                "(Stage 3) now brought by the path you took here in an abandoned farmhouse. You now see a cave up ahead on the northwest",
                                "(Stage 4) now inside a cave. It has an entrance to the west.",
                                "(Stage 5) currently in the left wing of the cave. On the southwest there is an entrance leading somwhere.",
                                "(Stage 6) halfway through to your escape. You are in the bottom of the cave. \nThere is a small hole ahead on your northeast, but you can fit right through",
                                "(Stage 7) inside a section where there is a body of water. On your southwest direction, you see a ladder leading somewhere.",
                                "(Stage 8) inside a slightly dark hallway. You see a light ahead in the southeast direction.",
                                "(Stage 9) now outside the cave. You see a path heading north connecting towards an establishment.",
                                "(Stage 10) finally on your destination!"};


private String objectName[]     = {"An adult magazine", "A fully loaded .45 calibre gun", "A motherboard from the 90s", "A syringe", 
                                    "A skull", "A worn out pair of shoes", "A Matchbox", "A Wooden Plank", "Illegal Drugs", "A bottle of whiskey" };
private int objectLocation[]    = {0, 1, 3, 1, 2, 0, 7, 6, 2, 1};
private int playerLocation      =  0;



// Prints out a description of the location the player is currently in, including a list of any objects at that location
private void describeLocation()
{
    System.out.println();
    System.out.println("You are " + description[playerLocation]);
    System.out.println("\n\t\tIn this area, you found: ");
    int numObjects = 0;
    for (int i=0; i<objectLocation.length; i++)
    {
        if (objectLocation[i]==playerLocation)
        {
            System.out.println("\n\t\t" + objectName[i]);
            numObjects++;
        }
    }
    if (numObjects==0)
    {
        System.out.println("\n\t\tNo Item(s)");
    }
    System.out.println();
}

// implements a simple text-driven menu
private int getMenuSelection(Scanner s)
{


    // display menu
    System.out.println("What do you want to do?");
    System.out.println("1. Go North");
    System.out.println("2. Go East");
    System.out.println("3. Go West");
    System.out.println("4. Go South");
    System.out.println("5. Go North East");
    System.out.println("6. Go North West");
    System.out.println("7. Go South East");
    System.out.println("8. Go South West");
    System.out.println("9. Quit Game");
    System.out.print("Enter command (1-9): ");
    // get and return the user's selection
    return s.nextInt();
}






// try to move in the specified direction
private void move(int direction)
{
    int nextLocation = map[playerLocation][direction];

    if (nextLocation==NO_EXIT)
    {
        System.out.println("\nThere is no way there. Try again!");
    }

    else
    {
        playerLocation = nextLocation;
    }
}

public void startGame()
{
    Scanner scanner = new Scanner(System.in);
    do
    {
        describeLocation();
        int selection = getMenuSelection(scanner);
        move(selection-1);



        while (playerLocation==10);
            System.out.println("You won the game");
            break;


    } while (true);     
}

public static void main(String[] args)
{
    practice adv = new practice();
    adv.startGame();


    }
}

所以基本上我需要知道的是如何在纠正这些事情后放置库存。

谢谢。

2 个答案:

答案 0 :(得分:0)

您遇到的许多问题之一是此代码段:

while (playerLocation==10); // < Semi colon here means loop forever if playerLocation==10
System.out.println("You won the game");
break; // This will always happen.

你可能想要这个:

playerLocation = scanner.nextInt();
if (playerLocation==10){
    System.out.println("You won the game");
    break; // this will break the outer loop
}

答案 1 :(得分:0)

首先,playerLocation永远不会是10,你的数组大小只有9.你必须从0开始计数而不是1.数组中的第一个值是0.

while (playerLocation == 9) { System.out.println("You won the game"); break; }

下一个是,你向着方向移动,而不是到新的位置。如果玩家想要向东移动,则将位置更改为阵列中的位置2。

所以考虑一下并使用开关盒。它可能是这样的。

// try to move in the specified direction
private void move(int direction) {

    int x = 0;//this should be the actual position of the player
    int y = 0;//this should be the actual position of the player

    switch (direction) {
    case 0:
        System.out.println("up");
        y++;
        break;
    case 1:
        System.out.println("down");
        y--;
        break;
    case 2:
        System.out.println("left");
        x--;
        break;
    case 3:
        System.out.println("right");
        x++;
        break;
    default:
        break;
    }
}