首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >移动随机矩阵8*8

移动随机矩阵8*8
EN

Stack Overflow用户
提问于 2011-12-03 21:26:05
回答 1查看 864关注 0票数 1

我尝试实现吸尘器随机移动,移动的方向应该在数组列表中,所以每次机器都应该从列表中随机选择3个方向作为方向示例( N,N,S)。机器会不断地按方向移动,直到到达它应该再次选择的边缘。

当机器到达边缘时不改变方向的问题。

我写了这个方法,负责决定机器是否到达边缘:

代码语言:javascript
复制
public boolean Wall(int x, int y) {

    if (choices.contains("N") && y == 7) {
        return false;
    } else if (choices.contains("S") && y == 0) {
        return false;
    } else if (choices.contains("W") && x == 0) {
        return false;
    } else if (choices.contains("E") && x == 7) {
        return false;
    } else {
        return true;
    }
}

我调用wall(),里面是move()。

这是我所有的代码:

代码语言:javascript
复制
import java.util.*;

public class test {

private static String[][] board;
private static final int ROWS = 8;
private static final int COLUMNS = 8;
int moves = 0;
List<String> orientation = Arrays.asList(new String[]{"N", "E", "W", "S"});
List<String> choices = new ArrayList<String>(3);


public test() {
    //String state = "d";
    board = new String[ROWS][COLUMNS];
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLUMNS; j++) {
            board[i][j] = " ";

        }
    }
    board[4][4] = "d";
    board[0][2] = "d";
    board[4][7] = "d";
    board[1][5] = "d";
    board[6][6] = "d";
    board[4][0] = "d";
}

public String toString() {
    String r = "";
    for (int i = 0; i < ROWS; i++) {

        for (int j = 0; j < COLUMNS; j++) {
            r += "[" + board[i][j] + "]";
        }
        r += "\n";
    }
    return r;
}

public int[] gostright(int x, int y) {
    if (choices.contains("N")) {
        x--;
        if (x == -1) {
            x = 0;
        }

    } else if (choices.contains("W")) {
        y--;
        if (y == -1) {
            y = 0;
        }

    } else if (choices.contains("S")) {
        x++;
        if (x == 8) {
            x = 7;
        }
    } else if (choices.contains("E")) {
        y++;
        if (y == 8) {
            y = 7;
        }
    }
    System.out.println("choise taste equal" + x + ":" + y);
    return new int[]{x, y};
}

public boolean Wall(int x, int y) {

    if (choices.contains("N") && y == 7) {
        return false;
    } else if (choices.contains("S") && y == 0) {
        return false;
    } else if (choices.contains("W") && x == 0) {
        return false;
    } else if (choices.contains("E") && x == 7) {
        return false;
    } else {
        return true;
    }
}

public int CountDirt() {
    int count = 0;
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLUMNS; j++) {
            if (board[i][j] == "d") {
                count++;
            }
        }
    }
    // System.out.println("the found dirt " + count+" dirts");
    return count;
}

public void Move() {
    Collections.shuffle(orientation);
    int nowX = 4, nowY = 4;
    int counter = 0;
    int strightCounter = 1;
    int wallx = 0;
    int wally = 0;
    while (CountDirt() > 0) {
        for (int i = 0; i < 3; i++) {
            choices.add(orientation.get(i));
            for (int x = 0; x < strightCounter; x++) {
                for (int y = 0; y < strightCounter; y++) {
                   if(Wall(wallx,wally))  {
                       break;
                   }              

                    System.out.println("Wall" + x + ":" + y);
                    board[nowX][nowY] = "1";
                    int[] pos = gostright(nowX, nowY);
                    nowX = pos[0];
                    nowY = pos[1];
                    System.out.println("" + nowX + ":" + nowY);
                    System.out.println("nowX and nowY" + board[nowX][nowY]);
                    board[nowX][nowY] = "#";
                    moves++;
                    System.out.println(toString());
                    System.out.println(orientation.get(i));
                    System.out.println("Choices " + choices);
                    System.out.println("# move" + moves);
                }
            }
            counter++;
            System.out.println("CountDirt()==" + CountDirt());


        }
        choices.clear();

    }

}

我认为问题出在move()中,但不确定它具体在哪里。

EN

回答 1

Stack Overflow用户

发布于 2011-12-03 22:12:05

我只看到wallx在这段代码中出现了两次。int wallx = 0;if(Wall(wallx,wally))。这意味着您总是将0和0传递给Wall()。

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

https://stackoverflow.com/questions/8367954

复制
相关文章

相似问题

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