【问题标题】:Randomizing obstacles for a game board游戏板的随机障碍
【发布时间】:2012-02-08 03:11:19
【问题描述】:

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

【问题讨论】:

  • 如果您从 0 到 99 从左上角开始逐行对网格中的每个单元格进行编号,您将如何编写代码来做到这一点?如果您开始在纸上执行此操作,那么该算法很可能会出现在您身上。我建议你试一试!

标签: java random


【解决方案1】:

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

从 1 到 100 中选择 15 个随机数;前 5 个标识坑,后 10 个标识块。

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

【讨论】:

    【解决方案2】:

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

    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
    }
    

    【讨论】:

    • 如何将动作扫描为字符串变量?
    • 你是什么意思,“扫描动作?”
    • 对不起,我的意思是如何将每个用户输入命令(U/D/L/R)声明为字符串变量?我试过这样做: String movePlayer="U", "D", "L", "R"; 但这不允许我以这种格式初始化多个字符串变量。
    • 你会使用一个按键监听器,然后当你从它得到一个按键事件时,char keyInput = myKeyEvent.getKeyChar()
    【解决方案3】:

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

        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 的 cod 放在循环之外,因为它似乎只需要完成一次。

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

    【讨论】:

    • 通常这不会产生正确数量的坑或块,因为Math.random() 是随机的。
    • 这就是为什么我首先问他是否需要正好 5% 的坑和 10% 的墙,正如你所说,这种方式不会每次都准确地给你,但我更喜欢随机性而不是“有 5 个尖峰”避免!'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    相关资源
    最近更新 更多