Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 跳过二维漫游栅格阵列中的空间_Java_Arrays_Multidimensional Array - Fatal编程技术网

Java 跳过二维漫游栅格阵列中的空间

Java 跳过二维漫游栅格阵列中的空间,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,该程序的目的是生成由周期组成的二维网格阵列。用户将在网格上为要放置的“步行者”指定坐标,标记为“a”。步行者将选择随机的基本方向进入,每次选择一个新的方向,并用字母表中的下一个字母标记它,直到它到达“Z”,或直到它超出边界。如果它到达一个已经存在的空间,它会检查其他三个方向,并移动到它找到的下一个空空间 该程序在大部分情况下运行良好,但通常在运行的中点附近,它会向前跳过几个空格,或者留下一个空格,或者看起来像是在对角移动(它不应该这样做) import java.lang.Math; 导入jav

该程序的目的是生成由周期组成的二维网格阵列。用户将在网格上为要放置的“步行者”指定坐标,标记为“a”。步行者将选择随机的基本方向进入,每次选择一个新的方向,并用字母表中的下一个字母标记它,直到它到达“Z”,或直到它超出边界。如果它到达一个已经存在的空间,它会检查其他三个方向,并移动到它找到的下一个空空间

该程序在大部分情况下运行良好,但通常在运行的中点附近,它会向前跳过几个空格,或者留下一个空格,或者看起来像是在对角移动(它不应该这样做)

import java.lang.Math;
导入java.util.Random;
导入java.util.Scanner;
班级步行者{
私有字符[][]walkgrid=新字符[10][10];
私有静态int randNSEW;
私有静态int randNSEWS;
私有int-randomnum;
斯达特罗私人酒店;
私人int startcol;
私有字符alpha='A';
下一步的私人int;
私人公司;
公共步行者(内部r、内部c){
startrow=r;
startcol=c;
nextrow=startrow;
nextcol=startcol;
对于(int i=0;i<10;i++){
//用句点填充walkgrid。
对于(int j=0;j<10;j++)
walkgrid[i][j]=';
}
walkgrid[r][c]=alpha++;
}
公共静态void getRand(){
//生成0-3之间的数字
int x100=0;
双随机数=0.0;
randomNum=Math.random();
x100=(int)(随机数*100);
randNSEW=x100%4;
}
public static void getRandSecundus(){
//生成0-2之间的数字以进行更正
int x100=0;
双随机数=0.0;
randomNum=Math.random();
x100=(int)(随机数*100);
randNSEWS=x100%3;
}
public int getNextRow(){
下一步返回;
}
public int getNextCol(){
返回nextcol;
}
枚举模式{行走,纠正};
Mode=Mode.WALKING;
公共布尔处理(){
对于(inti=1;i<26;i++){//直到它到达Z为止
if(mode==mode.WALKING){
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]!='.){
//如果沃克向西走,降落在某个地方,它已经在那里了
i--;//将计数器向后设置,使其不会超过Z。
nextcol++;//移回原位。
mode=mode.CORRECTING;
getRandSecundus();//调用辅助随机数。
如果(randNSEWS==0){
nextrow++;//检查南部。
}
如果(randNSEWS==1){
nextcol++;//检查东部。
}
如果(randNSEWS==2){
下一步--;//检查北面。
}
继续;
}
if(randNSEW==1&&walkgrid[nextrow][nextcol]!='.){
//如果沃克往南走,降落在某个地方,它已经在那里了
i--;//将计数器向后设置,使其不会超过Z。
下一步--;//移回原位。
mode=mode.CORRECTING;
getRandSecundus();//调用辅助随机数。
如果(randNSEWS==0){
nextcol--;//检查西部
}
如果(randNSEWS==1){
nextcol++;//检查东部
}
如果(randNSEWS==2){
下一步--;//检查北
}
继续;
}
如果(randNSEW==2&&walkgrid[nextrow][nextcol]!='.){
//如果沃克向东,降落在某个地方,它已经在那里了
i--;//将计数器向后设置,使其不会超过Z。
nextcol--;//移回原位。
mode=mode.CORRECTING;
getRandSecundus();//调用辅助随机数。
如果(randNSEWS==0){
nextcol--;//检查西部
}
如果(randNSEWS==1){
nextrow++;//检查南部
}
如果(randNSEWS==2){
下一步--;//检查北
}
继续;
}
如果(randNSEW==3&&walkgrid[nextrow][nextcol]!='.){
//如果沃克向北走并降落在某个地方,它已经在那里了
i--;//将计数器向后设置,使其不会超过Z。
nextrow++;//移回原位。
mode=mode.CORRECTING;
getRandSecundus();
如果(randNSEWS==0){
nextcol--;//检查西部
}
如果(randNSEWS==1){
nextrow++;//检查南部
}
如果(randNSEWS==2){
nextcol++;//检查东部
}
继续;
}
mode=mode.WALKING;
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=
import java.lang.Math;
import java.util.Random;
import java.util.Scanner;

class DrunkWalker {
    private char[][] walkgrid = new char[10][10];
    private static int randNSEW;
    private static int randNSEWS;
    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 ++) { 
//Fills walkgrid with periods.
            for (int j = 0; j < 10; j++)
                walkgrid[i][j] = '.';
        }
        walkgrid[r][c] = alpha++;
    }

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

    public static void getRandSecundus(){ 
//Generates number between 0-2 for corrections
        int x100 = 0;
        double randomNum = 0.0;
        randomNum = Math.random();
        x100 = (int) (randomNum * 100);
        randNSEWS = x100 % 3;
    }

    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 ++){ //Goes until it hits Z

        if (mode == Mode.WALKING) {
            getRand(); //Retrieves random direction
            if(randNSEW == 0){
                nextcol--; //west
            }
            if(randNSEW == 1){
                nextrow++; //south
            }
            if(randNSEW == 2){
                nextcol++; //east
            }
            if(randNSEW == 3){
                nextrow--; //north
            }
        }

//if walker goes out of bounds they are arrested.
        if(nextrow < 0 || nextrow >= 10 || nextcol < 0 || nextcol >= 10) {
            return false;
        }
        if(randNSEW == 0 && walkgrid[nextrow][nextcol] != '.'){
            //If walker headed west and lands somewhere it has already been
            i--; //Sets the counter back so it won't go further than Z.
            nextcol++; //Moves back in to place.
            mode = Mode.CORRECTING;
            getRandSecundus(); //Calls for secondary random number.
            if(randNSEWS == 0){
                nextrow++; //Check South.
            }
            if(randNSEWS == 1){
                nextcol++; //Check East.
            }
            if(randNSEWS == 2){
                nextrow--; //Check North.
            }
            continue;
        }
        if(randNSEW == 1 && walkgrid[nextrow][nextcol] != '.'){
            //If walker headed south and lands somewhere it has already been
            i--; //Sets the counter back so it won't go further than Z.
            nextrow--; //Moves back in to place.
            mode = Mode.CORRECTING;
            getRandSecundus(); //Calls for secondary random number.
            if(randNSEWS == 0){
                nextcol--; //Check West
            }
            if(randNSEWS == 1){
                nextcol++; //Check East
            }
            if(randNSEWS == 2){
                nextrow--; //Check North
            }
            continue;
        }
        if(randNSEW == 2 && walkgrid[nextrow][nextcol] != '.'){
            //If walker headed east and lands somewhere it has already been
            i--; //Sets the counter back so it won't go further than Z.
            nextcol--; //Moves back in to place.
            mode = Mode.CORRECTING;
            getRandSecundus(); //Calls for secondary random number.
            if(randNSEWS == 0){
                nextcol--; //Check West
            }
            if(randNSEWS == 1){
                nextrow++; //Check South
            }
            if(randNSEWS == 2){
                nextrow--; //Check North
            }
            continue;
        }
        if(randNSEW == 3 && walkgrid[nextrow][nextcol] != '.'){
            //If walker headed north and lands somewhere it has already been
            i--; //Sets the counter back so it won't go further than Z.
            nextrow++; //Moves back in to place.
            mode = Mode.CORRECTING;
            getRandSecundus();
            if(randNSEWS == 0){
                nextcol--; //Check West
            }
            if(randNSEWS == 1){
                nextrow++; //Check South
            }
            if(randNSEWS == 2){
                nextcol++; //Check East
            }
            continue;
        }

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




    public char[][] DisplayGrid() {
    for(int y = 0; y < 10; y++) {
        for(int x = 0; x < 10; x++) {
            System.out.print(walkgrid[x][y] + " ");
        }
        System.out.println();
    }
    return walkgrid;
}
}

public class WalkTester {

    public static void main(String[] args) {
        Scanner inpr = new Scanner(System.in);
        Scanner inpc = new Scanner(System.in);
        Scanner inpchoice = new Scanner(System.in);

        int r = 0;
        int c = 0;
        char choice = 'y';

        while(choice == 'y' || choice == 'Y') {
            System.out.println("Please enter x coordinate between 1 and 10.");
            r = inpr.nextInt();
            r = r - 1;

            System.out.println("Please enter y coordinate between 1 and 10");
            c = inpr.nextInt();
            c = c - 1;

            if(r < 0 || r > 9 || c < 0 || c > 9){
                System.out.println("Invalid Entry. Restart? y/n");
                choice = inpchoice.next().charAt(0);
                if(choice == 'y' || choice == 'Y'){
                    continue;
                }
                else if(choice == 'n' || choice == 'N'){
                    return;
                }
                else{
                    System.out.println("Invalid Entry. Restart? y/n");
                    choice = inpchoice.next().charAt(0);
                }
            }
            DrunkWalker drunkwalker = new DrunkWalker(r, c);
            boolean walkerSucceeded = drunkwalker.processing();
            drunkwalker.DisplayGrid();
            if(walkerSucceeded) {
            System.out.println("You made it home");
            } else {
            System.out.println("You were arrested");
            }

            System.out.println("Restart? y/n");
            choice = inpchoice.next().charAt(0);
            if(choice == 'y' || choice == 'Y'){
                continue;
            }
            else if(choice == 'n' || choice == 'N'){
                return;
            }
            else{
                System.out.println("Invalid Entry. Restart? y/n");
                choice = inpchoice.next().charAt(0);
            }
        }
    }
}
if(randNSEW == 0 && walkgrid[nextrow][nextcol] != '.'){
    //If walker headed west and lands somewhere it has already been
    i--; //Sets the counter back so it won't go further than Z.
    nextcol++; //Moves back in to place.
    mode = Mode.CORRECTING;
    getRandSecundus(); //Calls for secondary random number.
    if(randNSEWS == 0){
        nextrow++; //Check South.
        randNSEW = 1; /////////////////////////// THIS LINE ADDED
    }
    if(randNSEWS == 1){
        nextcol++; //Check East.
        randNSEW = 2; /////////////////////////// THIS LINE ADDED
    }
    if(randNSEWS == 2){
        nextrow--; //Check North.
        randNSEW = 3; /////////////////////////// THIS LINE ADDED
    }
    continue;
}
if(randNSEW == 1 && walkgrid[nextrow][nextcol] != '.'){
    //If walker headed south and lands somewhere it has already been
    i--; //Sets the counter back so it won't go further than Z.
    nextrow--; //Moves back in to place.
    mode = Mode.CORRECTING;
    getRandSecundus(); //Calls for secondary random number.
    if(randNSEWS == 0){
        nextcol--; //Check West
        randNSEW = 0; /////////////////////////// THIS LINE ADDED
    }
    if(randNSEWS == 1){
        nextcol++; //Check East
        randNSEW = 2; /////////////////////////// THIS LINE ADDED
    }
    if(randNSEWS == 2){
        nextrow--; //Check North
        randNSEW = 3; /////////////////////////// THIS LINE ADDED
    }
    continue;
}
if(randNSEW == 2 && walkgrid[nextrow][nextcol] != '.'){
    //If walker headed east and lands somewhere it has already been
    i--; //Sets the counter back so it won't go further than Z.
    nextcol--; //Moves back in to place.
    mode = Mode.CORRECTING;
    getRandSecundus(); //Calls for secondary random number.
    if(randNSEWS == 0){
        nextcol--; //Check West
        randNSEW = 0; /////////////////////////// THIS LINE ADDED
    }
    if(randNSEWS == 1){
        nextrow++; //Check South
        randNSEW = 1; /////////////////////////// THIS LINE ADDED
    }
    if(randNSEWS == 2){
        nextrow--; //Check North
        randNSEW = 3; /////////////////////////// THIS LINE ADDED
    }
    continue;
}
if(randNSEW == 3 && walkgrid[nextrow][nextcol] != '.'){
    //If walker headed north and lands somewhere it has already been
    i--; //Sets the counter back so it won't go further than Z.
    nextrow++; //Moves back in to place.
    mode = Mode.CORRECTING;
    getRandSecundus();
    if(randNSEWS == 0){
        nextcol--; //Check West
        randNSEW = 0; /////////////////////////// THIS LINE ADDED
    }
    if(randNSEWS == 1){
        nextrow++; //Check South
        randNSEW = 1; /////////////////////////// THIS LINE ADDED
    }
    if(randNSEWS == 2){
        nextcol++; //Check East
        randNSEW = 2; /////////////////////////// THIS LINE ADDED
    }
    continue;
}