随机遍历10x10网格阵列

时间:2016-02-24 21:08:51

标签: java

该程序的目的是构建一个由句点(。)组成的10x10数组网格,允许用户指定一个起点,然后程序将随机选择分配给“walker”方向的数字去。每次移动时,它都会用字母表中的下一个字母标记其位置(起点标记为A)。我还没有完成这部分,但我知道我会如何;如果助行器越过阵列的边界(AKA> 10或<0),它会说“你被逮捕了”,如果变量alpha =='Z'就说“你把它弄回家”。< / p>

据我所知,主要的问题是方法processing(),我从getRand中绘制数字(我已确认它正在工作),然后为它们分配给予数组的方向。但由于某些原因,当它打印网格时,它只显示起点,而不是步行者的移动。

另一个我不太重要的问题是,如果有一种更简洁的方式来显示我的网格而不是我曾经(在System.out.print中有一块巨大的文本块)

    package walktester;

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 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;
}

public void processing(){
    for(int i = 0; i < 26; i ++){
    getRand();
    if(randNSEW == 0){
        nextcol--;
        walkgrid[nextrow][nextcol] = alpha++;
    }
    if(randNSEW == 1){
        nextrow++;
        walkgrid[nextrow][nextcol] = alpha++;
    }
    if(randNSEW == 2){
        nextcol++;
        walkgrid[nextrow][nextcol] = alpha++;
    }
    if(randNSEW == 3){
        nextrow--;
        walkgrid[nextrow][nextcol] = alpha++;
    }
    }
}



public char[][] DisplayGrid() {
    System.out.print(
            walkgrid[0][0] + " " + walkgrid[0][1] + " " + walkgrid[0][2] + " " + walkgrid[0][3] + " " + walkgrid[0][4] + " " + walkgrid[0][5] + " " + walkgrid[0][6] + " " + walkgrid[0][7] + " " + walkgrid[0][8] + " " + walkgrid[0][9] + "\n" + 
            walkgrid[1][0] + " " + walkgrid[1][1] + " " + walkgrid[1][2] + " " + walkgrid[1][3] + " " + walkgrid[1][4] + " " + walkgrid[1][5] + " " + walkgrid[1][6] + " " + walkgrid[1][7] + " " + walkgrid[1][8] + " " + walkgrid[1][9] + "\n" + 
            walkgrid[2][0] + " " + walkgrid[2][1] + " " + walkgrid[2][2] + " " + walkgrid[2][3] + " " + walkgrid[2][4] + " " + walkgrid[2][5] + " " + walkgrid[2][6] + " " + walkgrid[2][7] + " " + walkgrid[2][8] + " " + walkgrid[2][9] + "\n" + 
            walkgrid[3][0] + " " + walkgrid[3][1] + " " + walkgrid[3][2] + " " + walkgrid[3][3] + " " + walkgrid[3][4] + " " + walkgrid[3][5] + " " + walkgrid[3][6] + " " + walkgrid[3][7] + " " + walkgrid[3][8] + " " + walkgrid[3][9] + "\n" + 
            walkgrid[4][0] + " " + walkgrid[4][1] + " " + walkgrid[4][2] + " " + walkgrid[4][3] + " " + walkgrid[4][4] + " " + walkgrid[4][5] + " " + walkgrid[4][6] + " " + walkgrid[4][7] + " " + walkgrid[4][8] + " " + walkgrid[4][9] + "\n" + 
            walkgrid[5][0] + " " + walkgrid[5][1] + " " + walkgrid[5][2] + " " + walkgrid[5][3] + " " + walkgrid[5][4] + " " + walkgrid[5][5] + " " + walkgrid[5][6] + " " + walkgrid[5][7] + " " + walkgrid[5][8] + " " + walkgrid[5][9] + "\n" + 
            walkgrid[6][0] + " " + walkgrid[6][1] + " " + walkgrid[6][2] + " " + walkgrid[6][3] + " " + walkgrid[6][4] + " " + walkgrid[6][5] + " " + walkgrid[6][6] + " " + walkgrid[6][7] + " " + walkgrid[6][8] + " " + walkgrid[6][9] + "\n" + 
            walkgrid[7][0] + " " + walkgrid[7][1] + " " + walkgrid[7][2] + " " + walkgrid[7][3] + " " + walkgrid[7][4] + " " + walkgrid[7][5] + " " + walkgrid[7][6] + " " + walkgrid[7][7] + " " + walkgrid[7][8] + " " + walkgrid[7][9] + "\n" + 
            walkgrid[8][0] + " " + walkgrid[8][1] + " " + walkgrid[8][2] + " " + walkgrid[8][3] + " " + walkgrid[8][4] + " " + walkgrid[8][5] + " " + walkgrid[8][6] + " " + walkgrid[8][7] + " " + walkgrid[8][8] + " " + walkgrid[8][9] + "\n" + 
            walkgrid[9][0] + " " + walkgrid[9][1] + " " + walkgrid[9][2] + " " + walkgrid[9][3] + " " + walkgrid[9][4] + " " + walkgrid[9][5] + " " + walkgrid[9][6] + " " + walkgrid[9][7] + " " + walkgrid[9][8] + " " + walkgrid[9][9] + "\n"
    );
    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);
        drunkwalker.DisplayGrid();

        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);
        }


       }
    }
}

1 个答案:

答案 0 :(得分:3)

移动walker的代码位于名为processing()的方法中,但您从不调用它。

您可以使DisplayGrid()方法更简单(它仍会打印相同的内容):

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;
}

您的processing()方法需要检查助行器是否离开该区域的边界。如果不这样做,您将获得ArrayIndexOutOfBoundsException

public boolean processing(){
    for(int i = 0; i < 26; i ++){
        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;
        }

        walkgrid[nextrow][nextcol] = alpha++;
    }
    return true;
}

现在你需要调用processing()并检查返回值以查看walker是否成功:

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");
}

请记住(正如您在测试时所看到的那样),助行器可以跨越自己的轨道(因此您可能会看到一些字母丢失)。