我的程序在输入的每个命令后生成一个新的游戏板。我只希望“P”更新移动,而棋盘和障碍保持不变直到游戏终止。感谢您的帮助,这是我的代码:
import java.util.Scanner;
import java.util.Random;
public class Adventure {
public static void main(String[] args) {
// Create 2D array for game board.
char grid[][]= new char[10][10];
Scanner move = new Scanner(System.in);
System.out.println("Here is the current game board:");
System.out.println("-------------------------------");
do{
for(int i=0; i<grid.length; i++) {
for(int j=0; j<grid.length; j++) {
double Random = Math.random();
if(Random <=.05) {
grid[i][j]='*';
}
else if(Random > .06 && Random <= .15) {
grid[i][j]='X';
}
else {
grid[i][j]='.';
}
grid[0][0]='P';
grid[9][9]='T';
System.out.print(grid[i][j]);
}
System.out.println("");
}
System.out.print("Enter your move (U/D/L/R)>");
String movePlayer = move.next();
int x=0, y=0;
if(movePlayer.equals("R")) {
grid[y][x]='.';
x++;
}
else if(movePlayer.equals("L")) {
grid[y][x]='.';
x--;
}
else if(movePlayer.equals("U")) {
grid[y][x]='.';
y++;
}
else if(movePlayer.equals("D")) {
grid[y][x]='.';
y--;
}
else if(grid[y][x]=='*') {
System.out.println("You fell in a pit. Game Over.");
}
else if(grid[y][x]=='X') {
System.out.println("That spot is blocked. Please enter another move.");
}
else if(grid[y][x]=='T')
System.out.println("Congratulations! You've found the treasure!");
else {
System.out.print(grid[y][x]);
}
}while('P' != 'T');
}
}
答案 0 :(得分:0)
将生成板的代码移到do ... while()
循环之外:
// generate the board once at the beginning of the program.
do
{
// update moves repeatedly until game is over
} while (<game over perdicate>)