随机化游戏板的障碍

时间:2012-02-08 03:11:20

标签: java random

我想使用随机数生成器在游戏板上放置障碍物。 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);”之后要去哪里;

3 个答案:

答案 0 :(得分:1)

如果你的游戏板有100个点,那么它将有5个坑,10个街区和85个空地。

从1到100中选择15个随机数;前5个识别凹坑,接下来的10个识别块。

创建一个列表以跟踪15个数字。每次选择随机数时,请检查该数字是否已存在于列表中。如果是,丢弃它并选择不同的随机数。否则将其添加到列表中并继续,直到您选择了所有15个数字。

答案 1 :(得分:0)

你需要一定数量的障碍吗?最好将代码放在定义电路板的循环中:

    for(int i=0; i<grid.length; i++) {          
        for(int j=0; j<grid.length; j++) {
            int random = Math.random();
            if (random <.05){
                grid[i][j]='*';
            }else if (random < .15) {
                grid[i][j]='X'
            }else {
                grid[i][j]='.';
            }
            System.out.print(grid[i][j]);
        }
    }
    grid[0][0]='P';
    grid[9][9]='T';

此外你应该把鳕鱼放在上面定义P和T之后,因为它似乎只需要做一次。

编辑:此方法将为您提供游戏板的表示,以覆盖*,您可以更改打印方法以将其打印为.或维护显示网格,以及作为实际网格(例如制作2个网格)

答案 2 :(得分:0)

至于实际的互动性,这是一个大纲:

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
}