Java 二维网格移动检查

Java 二维网格移动检查,java,arrays,Java,Arrays,该程序的目的是生成由周期(.)构成的二维网格阵列。用户为标记为“a”的“walker”指定一个起点,然后walker将生成0-3之间的数字,以表示四个基本方向。它将在这些随机方向上移动,同时用它留下的每一个标记递增字母表,直到它跑进墙中并被“逮捕”或到达它“回家”的“Z”。如果它跑到一个它已经去过的地方,它必须朝着同一个方向向前跳,直到它到达一个空的地方或者撞到墙上 我现在的问题是,我把它放在一个计数器上,以确保它不会跑过“Z”,如果它达到那个点,我会“让它回家”。但是,即使是它为了避免覆盖已经

该程序的目的是生成由周期(.)构成的二维网格阵列。用户为标记为“a”的“walker”指定一个起点,然后walker将生成0-3之间的数字,以表示四个基本方向。它将在这些随机方向上移动,同时用它留下的每一个标记递增字母表,直到它跑进墙中并被“逮捕”或到达它“回家”的“Z”。如果它跑到一个它已经去过的地方,它必须朝着同一个方向向前跳,直到它到达一个空的地方或者撞到墙上

我现在的问题是,我把它放在一个计数器上,以确保它不会跑过“Z”,如果它达到那个点,我会“让它回家”。但是,即使是它为了避免覆盖已经覆盖的位置而采取的动作也在计数器上注册(它们不应该是这样),因此它返回true,即使它还没有到达Z,并且它仍然调用我的随机数生成器,因此当它试图纠正自身时,它不会保持相同的方向。它有时甚至会跳过空旷的空间

问题在处理中()

package步行测试仪;
导入java.lang.Math;
导入java.util.Random;
导入java.util.Scanner;
班级步行者{
私有字符[][]walkgrid=新字符[10][10];
私有静态int randNSEW;
私有int-randomnum;
斯达特罗私人酒店;
私人int startcol;
私有字符alpha='A';
下一步的私人int;
私人公司;
公共步行者(内部r、内部c){
startrow=r;
startcol=c;
nextrow=startrow;
nextcol=startcol;
对于(int i=0;i<10;i++){
对于(int j=0;j<10;j++)
walkgrid[i][j]=';
}
walkgrid[r][c]=alpha++;
}
公共静态void getRand(){
int x100=0;
双随机数=0.0;
randomNum=Math.random();
x100=(int)(随机数*100);
randNSEW=x100%4;
}
public int getNextRow(){
下一步返回;
}
public int getNextCol(){
返回nextcol;
}
公共布尔处理(){
对于(int i=1;i<26;i++){
getRand();
如果(randNSEW==0){
nextcol--;
}
如果(randNSEW==1){
nextrow++;
}
如果(randNSEW==2){
nextcol++;
}
如果(randNSEW==3){
下一步--;
}
如果(nextrow<0 | | nextrow>=10 | | nextcol<0 | | nextcol>=10){
返回false;
}
if(randNSEW==0&&walkgrid[nextrow][nextcol]!='.){
nextcol--;
继续;
}
if(randNSEW==1&&walkgrid[nextrow][nextcol]!='.){
nextrow++;
继续;
}
如果(randNSEW==2&&walkgrid[nextrow][nextcol]!='.){
nextcol++;
继续;
}
如果(randNSEW==3&&walkgrid[nextrow][nextcol]!='.){
下一步--;
继续;
}
walkgrid[nextrow][nextcol]=alpha++;
}
返回true;
}
公共字符[][]显示网格(){
对于(int y=0;y<10;y++){
对于(int x=0;x<10;x++){
系统输出打印(walkgrid[x][y]+“”);
}
System.out.println();
}
返回网格;
}
}
公共类步行测试仪{
公共静态void main(字符串[]args){
扫描仪inpr=新扫描仪(System.in);
扫描仪inpc=新扫描仪(System.in);
扫描器输入选择=新扫描器(System.in);
int r=0;
int c=0;
字符选择='y';
while(choice='y'| choice='y'){
System.out.println(“请输入1和10之间的x坐标”);
r=inpr.nextInt();
r=r-1;
System.out.println(“请输入1和10之间的y坐标”);
c=inpr.nextInt();
c=c-1;
如果(r<0 | | r>9 | | c<0 | | c>9){
System.out.println(“无效输入。重新启动?是/否”);
choice=inpchoice.next().charAt(0);
如果(选项='y'| |选项=='y'){
继续;
}
else if(choice='n'| | choice='n'){
返回;
}
否则{
System.out.println(“无效输入。重新启动?是/否”);
choice=inpchoice.next().charAt(0);
}
}
醉汉步行者=新的醉汉步行者(r,c);
布尔walkerSucceeded=walker.processing();
DisplayGrid();
如果(WalkerSucceed){
System.out.println(“你回家了”);
}否则{
System.out.println(“你被捕了”);
}
系统输出打印项次(“重启?是/否”);
choice=inpchoice.next().charAt(0);
如果(选项='y'| |选项=='y'){
继续;
}
else if(choice='n'| | choice='n'){
返回;
}
否则{
System.out.println(“无效输入。重新启动?是/否”);
choice=inpchoice.next().charAt(0);
}
}
}
}

以下代码修复了您的问题。你基本上没有“状态”来区分有效的移动,而不是在过去的访问点上移动到“跳跃”。我试图使代码尽可能接近您现有的代码

class DrunkWalker {
    private char[][] walkgrid = new char[10][10];
    private static int randNSEW;
    private int randomnum;
    private int startrow;
    private int startcol;
    private char alpha = 'A';
    private int nextrow;
    private int nextcol;

    public DrunkWalker(int r, int c) {
        startrow = r;
        startcol = c;
        nextrow = startrow;
        nextcol = startcol;

        for (int i = 0; i < 10; i ++) {
            for (int j = 0; j < 10; j++)
                walkgrid[i][j] = '.';
        }
        walkgrid[r][c] = alpha++;
    }

    public static void getRand(){
        int x100 = 0;
        double randomNum = 0.0;
        randomNum = Math.random();
        x100 = (int) (randomNum * 100);
        randNSEW = x100 % 4;
    }

    public int getNextRow(){
        return nextrow;
    }

    public int getNextCol(){
        return nextcol;
    }

    enum Mode {WALKING, CORRECTING};
    Mode mode =  Mode.WALKING;

    public boolean processing(){
    for(int i = 1; i < 26; i ++){

        if (mode == Mode.WALKING) {
            getRand();
            if(randNSEW == 0){
                nextcol--;
            }
            if(randNSEW == 1){
                nextrow++;
            }
            if(randNSEW == 2){
                nextcol++;
            }
            if(randNSEW == 3){
                nextrow--;
            }
        }

        if(nextrow < 0 || nextrow >= 10 || nextcol < 0 || nextcol >= 10) {
            return false;
        }
        if(randNSEW == 0 && walkgrid[nextrow][nextcol] != '.'){
            i--;
            nextcol--;
            mode = Mode.CORRECTING;
            continue;
        }
        if(randNSEW == 1 && walkgrid[nextrow][nextcol] != '.'){
            i--;
            nextrow++;
            mode = Mode.CORRECTING;
            continue;
        }
        if(randNSEW == 2 && walkgrid[nextrow][nextcol] != '.'){
            i--;
            nextcol++;
            mode = Mode.CORRECTING;
            continue;
        }
        if(randNSEW == 3 && walkgrid[nextrow][nextcol] != '.'){
            i--;
            nextrow--;
            mode = Mode.CORRECTING;
            continue;
        }

        mode = Mode.WALKING;
        walkgrid[nextrow][nextcol] = alpha++;
    }
    return true;
}
class-walker{
私有字符[][]walkgrid=新字符[10][10];
私有静态int randNSEW;
私有int-randomnum;
斯达特罗私人酒店;
私人int startcol;
私有字符alpha='A';
下一步的私人int;
私人公司;
公共步行者(内部r、内部c){
startrow=r;
startcol=c;
nextrow=startrow;
nextcol=startcol;
为了
class DrunkWalker {
    private char[][] walkgrid = new char[10][10];
    private static int randNSEW;
    private int randomnum;
    private int startrow;
    private int startcol;
    private char alpha = 'A';
    private int nextrow;
    private int nextcol;

    public DrunkWalker(int r, int c) {
        startrow = r;
        startcol = c;
        nextrow = startrow;
        nextcol = startcol;

        for (int i = 0; i < 10; i ++) {
            for (int j = 0; j < 10; j++)
                walkgrid[i][j] = '.';
        }
        walkgrid[r][c] = alpha++;
    }

    public static void getRand(){
        int x100 = 0;
        double randomNum = 0.0;
        randomNum = Math.random();
        x100 = (int) (randomNum * 100);
        randNSEW = x100 % 4;
    }

    public int getNextRow(){
        return nextrow;
    }

    public int getNextCol(){
        return nextcol;
    }

    enum Mode {WALKING, CORRECTING};
    Mode mode =  Mode.WALKING;

    public boolean processing(){
    for(int i = 1; i < 26; i ++){

        if (mode == Mode.WALKING) {
            getRand();
            if(randNSEW == 0){
                nextcol--;
            }
            if(randNSEW == 1){
                nextrow++;
            }
            if(randNSEW == 2){
                nextcol++;
            }
            if(randNSEW == 3){
                nextrow--;
            }
        }

        if(nextrow < 0 || nextrow >= 10 || nextcol < 0 || nextcol >= 10) {
            return false;
        }
        if(randNSEW == 0 && walkgrid[nextrow][nextcol] != '.'){
            i--;
            nextcol--;
            mode = Mode.CORRECTING;
            continue;
        }
        if(randNSEW == 1 && walkgrid[nextrow][nextcol] != '.'){
            i--;
            nextrow++;
            mode = Mode.CORRECTING;
            continue;
        }
        if(randNSEW == 2 && walkgrid[nextrow][nextcol] != '.'){
            i--;
            nextcol++;
            mode = Mode.CORRECTING;
            continue;
        }
        if(randNSEW == 3 && walkgrid[nextrow][nextcol] != '.'){
            i--;
            nextrow--;
            mode = Mode.CORRECTING;
            continue;
        }

        mode = Mode.WALKING;
        walkgrid[nextrow][nextcol] = alpha++;
    }
    return true;
}