在文字冒险游戏中移动

问题描述 投票:0回答:1

我正在做一个简单的文字冒险项目。当我运行代码时,当我尝试两次向右/向左/向前/向后输入时,它将不起作用。它重置为游戏的开始。

此外,当我进行前进后左转时,程序进入我的故障保险柜。不仅如此,故障保护也不起作用。我输入了允许的方向,但是故障保护功能会无限期继续。

Scanner key = new Scanner(System.in);
int[] humanCode = {0,0,0,0};
int x, y;
x = 0;
y = 0;
boolean safeOpen = false;
boolean doorKey = false;
String gameStart = "c";
String direction = "c";

System.out.print("Welcome to my text adventure game!\nIf you want to play, type in 'Start'");
gameStart = key.next();

while(!"Start".equals(gameStart))
{
  System.out.print("Incorrect input.  Type in 'Start' to begin:  ");
  gameStart = key.next();
}

while(gameStart.equals("Start"))
{
  direction = "";
  x = 0;
  y = 0;
  System.out.println("");
  System.out.print("You wake up in an extremly small room.  You estimate it to be a perfectly sized 3x3 room.  Based on your estimation, your in the center of the room.  Dispite that there is a bed directly behind you, you wake up on a circular rug.  Weird, but, the door's right in front of you.  Time to escape!\nYou can move forward, backwards, left and right.");
  direction = key.next();

  if(direction.equals("right") || direction.equals("Right"))
  {
    x++;
    System.out.print("You walk to the right.");
    direction = key.next();
  }

  else if (direction.equals("left") || direction.equals("Left"))
  {
    x--;
    System.out.print("You walk to the left.");
    direction = key.next();
  }

  else if (direction.equals("forward") || direction.equals("Forward"))
  {
    y++;
    System.out.print("You walk Forward.");
    direction = key.next();
  }

  else if (direction.equals("backwards") || direction.equals("Backwards"))
  {
    y--;
    System.out.print("You walk backwards.");
    direction = key.next();
  }
  else 
  {
    do
    {
    System.out.print("Sorry, you can't do that.  Try again:");
    direction = key.next();
    }
    while(!direction.equals("right")|| !direction.equals("Right") || !direction.equals("left") || !direction.equals("Left") || !direction.equals("forward") || !direction.equals("Forward") || !direction.equals("backwards") || !direction.equals("Backwards"));
  }

while(gameStart.equals("Start"));
  {
    if (x<-2 || x>2 || y<-2 || y>2)
    {
      System.out.println("ERROR:  OUT OF BOUNDS");
      System.exit(0);
    }
    if (x==-2 || x==2)
    {
      System.out.println("You try to walk further, however, you run right into a wall.");
      x--;
    }
    if (y==-3 || y==3)
    {
      System.out.println("You try to walk further, however, you run right into a wall.");
      y--;
    }
  }
}
java java.util.scanner
1个回答
0
投票

您的while循环后有一个分号,它立即结束。

while(gameStart.equals("Start"));

此外,此代码中还有很多不良做法...

© www.soinside.com 2019 - 2024. All rights reserved.