每次命令输入后停止板随机化
我的程序在我输入每个命令后都会生成一个新的游戏板。我只希望“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');
}
}
My program generates a new game board after every command I input. I only want the "P" to update the move while the board and obstacles stay the same until the game is terminated. Thanks for the help in advance, here is my code:
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');
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将生成棋盘的代码移到 do ... while() 循环之外:
move the code that generates the board outside the
do ... while()
loop: