我想使用随机数生成器在游戏板上放置障碍物。 5%的棋盘会有一个被定义为“ *”的底坑,但除非玩家落在该位置,否则星号不会显示;木板的10%将被阻塞的斑点表示为“ X”;剩下的85%将是显示为“”的开放空间。游戏板是一个10x10的数组,左上角的字母“ P”作为玩家的起点,右下角的字母“ T”作为结束符(宝物)。到目前为止,我已经掌握了这一点,并且我一直在观看视频教程以及阅读以尝试将所有内容放在一起,但仍然存在问题:
import java.util.Scanner;
import java.util.Random;
public class Adventure {
public static void main(String[] args) {
char grid[][]= new char[10][10];
Scanner move = new Scanner(System.in);
System.out.println("Here is the current game board:");
System.out.println("-------------------------------");
for(int i=0; i<grid.length; i++) {
for(int j=0; j<grid.length; j++) {
grid[i][j]='.';
grid[0][0]='P';
grid[9][9]='T';
System.out.print(grid[i][j]);
}
Random obstacle = new Random();
int obstacleNum;
for(int k=1; k<=100; k++) {
obstacleNum = 1+obstacle.nextInt(100);
}
System.out.println("");
}
System.out.printf("Enter your move (U/D/L/R)>");
}
}
不知道在“ obstacleNum = 1 + obstacle.nextInt(100);”之后应该去哪里?
最佳答案
至于实际的交互性,这是一个概述:
x = 0; //coordinates of player, location of P
y = 0;
要隐藏凹坑,在打印之前,请粘贴if语句:
if(grid[i][j]=='*') {
System.out.println("."); //looks like ordinary land
} else {
System.out.println(grid[i][j]);
}
现在在收到输入时运行此命令(伪)
//following if for moving left
if(grid[y][x+1] isn't out of bounds and right key is pressed and grid[y][x+1]!='X') {
grid[y][x] = '.'; //removes current position
x++; //change location
}
//following if for moving right
if(grid[y][x-1] isn't out of bounds and left key is pressed and grid[y][x-1]!='X') {
grid[y][x] = '.'; //removes current position
x--; //change location
}
//following if for moving down
if(grid[y+1][x] isn't out of bounds and down key is pressed and grid[y+1][x]!='X') {
grid[y][x] = '.'; //removes current position
y++; //change location
}
//following if for moving up
if(grid[y-1][x] isn't out of bounds and up key is pressed and grid[y-1][x]!='X') {
grid[y][x] = '.'; //removes current position
y--; //change location
}
if(grid[y][x] == '*') { //when trapped in a pit
System.out.println("You fell in a pit. Game over.");
} else {
grid[y][x] = 'P'; //update grid
}
关于java - 随机分配游戏板的障碍,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9187352/