【问题标题】:move to a new instance in an 2d array randomly once随机移动到二维数组中的新实例一次
【发布时间】:2015-11-07 11:46:38
【问题描述】:

所以我是一个二维数组上的玩家,当我做一个动作时,我希望玩家移动到他周围的 8 个可用块之一,下面的代码随机移动他,但会移动两次

移动前的地图

草草草草

草雷克草草

草草草草

草草草草

随机移动

0 0

0 0 //这不应该发生

移动后的地图

草草草草

草草草草

草草草草

GrassGrassGrass Rek

import java.util.Random;

public class command_Movment implements command_Move {


inSwamp map = new inSwamp();
inSwamp rek = new Rek();
Random random = new Random();


int row = random.nextInt(3);
int col = random.nextInt(3);

@Override
public Command move() {

    for (int i = 0; i < map.grid.length; i++) {
        for (int j = 0; j < map.grid[i].length; j++) {

            if (map.grid[i][j] == rek.getName()) {

                try {
                    map.grid[i][j] = "Grass";
                    if (row == 0) {
                        i++;
                    }
                    if (row == 1) {
                        i--;
                    }

                    if (col == 0) {
                        j++;
                    }
                    if (col == 1) {
                        j--;
                    }

                    map.grid[i][j] = rek.getName();
                    System.out.println(col + "     " + row);

                    break;

                } catch (ArrayIndexOutOfBoundsException exception) {
                    if (row == 0) {
                        i--;
                    }
                    if (row == 1) {
                        i++;
                    }

                    if (col == 0) {
                        j--;
                    }
                    if (col == 1) {
                        j++;
                    }
                    map.grid[i][j] = rek.getName();
                    System.out.println("Error");

                    break;
                }



        }
    }


}

    return null;

} }

【问题讨论】:

    标签: java for-loop multidimensional-array


    【解决方案1】:

    首先,你不应该使用 == 来比较字符串,你应该使用 equals 方法。所以用if (map.grid[i][j].equals(rek.getName()))替换if (map.grid[i][j] == rek.getName())

    编辑请不要使用标签来破坏程序的模块化! 请不要使用捕获 ArrayIndexOutofBound 异常来确定数组索引是否正确。异常应该发生。你应该先检查索引。

    我为你的随机移动更新了我的程序:基本上我认为你想要: 1) 从原始位置随机上移或下移 2) 如果上移或下移超出矩阵的边界,则不要往那个方向移动。

    以下程序应将 rek 随机移动到其 8 个邻居之一,而不会导致任何 ArrayIndexOutOfBoundException。

    public Command move() {
        // randomly determine the moving direction
        // -1 means move left, 1 means move right
        int horizontal_direction = Math.random() > 0.5 ? -1 : 1;
        // -1 means move up, 1 mean move down
        int vertical_direction = Math.random() > 0.5 ? -1 : 1;
    
        for (int i = 0; i < map.grid.length; i++) {
            for (int j = 0; j < map.grid[i].length; j++) {
    
                if (map.grid[i][j].equals(rek.getName())) {
                    map.grid[i][j] = "Grass"; // replace rek's current position with Grass\
    
                    // if the newRow exceeds the boundaries, don't move in that direction
                    int newRow = i + horizontal_direction;
                    if (newRow < 0 || newRow == map.grid.length)
                        newRow = i;
    
                    // if the newCol exceeds the boundaries, don't move in that direction
                    int newCol = j + vertical_direction;
                    if (newCol < 0 || newCol == map.grid[i].length)
                        newCol = j;
    
                    map.grid[newRow][newCol] = rek.getName(); // move rek to the new position
    
                    System.out.println(newRow + "     " + newCol);
                    break;
                }
            }
        }
        return null;
    }
    

    【讨论】:

    • 谢谢,我现在将它们更改为 .equals 并且代码不适用于网格中的任何地方,只有 rek 周围的 8 个可用块。
    • @MichaelAlexanderDelaney 更新了代码,这应该符合您的要求。
    【解决方案2】:

    在你的外循环中添加这样的标签:

    outer:
    for (int i = 0; i < map.grid.length; i++) {
        ......
    }
    

    try 块中,以这种方式中断循环:

    map.grid[i][j] = rek.getName();
    System.out.println(col + "     " + row);
    
    break outer;
    

    【讨论】:

    • 谢谢,效果很好,以前从来不知道这个功能
    • 你可以解决这个问题,你知道的。但在这种情况下,这种“黑客”似乎是有益的。灵感来自程序集的 GOTO 和 JUMP TO 指令。
    • @JDev 我从来不知道 Java 中存在这种情况。我投了反对票,因为我同意您的评论 - 这非常hacky并且破坏了程序的模块化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2015-10-24
    • 2017-10-07
    • 2015-06-29
    • 1970-01-01
    相关资源
    最近更新 更多