首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为游戏板随机化障碍物

为游戏板随机化障碍物
EN

Stack Overflow用户
提问于 2012-02-08 11:11:20
回答 3查看 1.9K关注 0票数 0

我想使用随机数生成器在游戏板上放置障碍物。5%的棋盘将有一个被定义为"*“的坑,但星号不会显示,除非玩家落地在那个点上;10%的棋盘将是被阻挡的点,表示为"X";其余的85%将是显示为”“的空地。游戏棋盘是一个10x10的数组,左上角的字母"P“作为玩家的起点,右下角的字母"T”表示结束(宝藏)。到目前为止,我已经得到了这一点,我一直在看视频教程和阅读,试图将所有这些放在一起,但我仍然坚持:

代码语言:javascript
复制
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)“之后去哪里;

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-02-08 12:41:06

至于实际的交互性,这里有一个概要:

代码语言:javascript
复制
x = 0; //coordinates of player, location of P
y = 0;

要在打印前隐藏凹坑,请使用和if语句:

代码语言:javascript
复制
if(grid[i][j]=='*') {
    System.out.println("."); //looks like ordinary land
} else {
    System.out.println(grid[i][j]);
}

现在让它在接收到输入(伪)时运行它

代码语言:javascript
复制
//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
}
票数 0
EN

Stack Overflow用户

发布于 2012-02-08 11:24:05

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

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

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

票数 1
EN

Stack Overflow用户

发布于 2012-02-08 11:27:45

你需要固定数量的障碍吗?你最好把代码放在你的循环中,它定义了你的板子:

代码语言:javascript
复制
    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';

此外,您应该将cod放在定义P和T之后的循环之外,因为它似乎只需要做一次。

编辑:此方法将为您提供游戏板的表示形式,为了覆盖*,您可以更改print方法以将它们打印为.,或者维护一个显示网格以及实际网格(例如创建2个网格)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9187352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档