在行动

时间:2016-02-25 02:36:20

标签: java

该程序的目的是构建一个由句点(。)组成的10x10阵列网格,允许用户指定一个起点,然后程序将随机选择分配给&#39方向的数字。 ;沃克&#39;去。每次移动时,它都会用字母表中的下一个字母标记其位置(起点标记为A)。如果助行器越过阵列的边界(AKA> 10或<0),它将说&#34;你被逮捕&#34;如果变量alpha ==&#39; Z&#39;它的方式说&#34;你把它带回家&#34;。

我唯一要做的就是让它如果步行者试图回到它已经去过的地方,它将跳到下一个空间,直到它到达一个它没有的空间。过。

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



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

1 个答案:

答案 0 :(得分:1)

通过&#34; 跳到下一个空格,直到它到达空格&#34;我认为你的意思是他们继续朝着同一个方向前进。

所以,就在你做之前:

walkgrid[nextrow][nextcol] = alpha++;

你需要检查:

if (walkgrid[nextrow][nextcol] == '.')

这些内容如下:

    ...
    do {
        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 (walkgrid[nextrow][nextcol] == '.') continue;  /* try next */
        walkgrid[nextrow][nextcol] = alpha++;
    } while (false);

或者我们可以将最后三行重写为:

    } while (walkgrid[nextrow][nextcol] == '.');
    walkgrid[nextrow][nextcol] = alpha++;