该程序的目的是生成由句点(。)组成的二维网格数组。用户指定由'A'标记的'walker'的起始点,然后walker将生成0-3的数字以表示四个基本方向。它将沿着这些随机方向移动,同时用它留下的每个标记递增字母表,直到它跑到墙上并被“逮捕”或到达“Z”,在那里它“使它回家”。如果它跑到它已经到过的空间,它必须向同一方向前进,直到它到达一个空的空间或撞到墙壁。
我现在的问题是我把它放在柜台上以确保它不会超过'Z',并且如果达到那个点就会“让它回家”。但是,即使是为了避免覆盖已经存在的地方所采取的动作也是在柜台上登记(他们不应该这样),所以即使它尚未击中Z,它仍然会返回真实状态,并且它仍然会调用我的随机数生成器,因此当它试图纠正自己时,它不会保持相同的方向。它似乎偶尔会跳过空地。
问题在于处理()
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 = 1; 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;
}
if(randNSEW == 0 && walkgrid[nextrow][nextcol] != '.'){
nextcol--;
continue;
}
if(randNSEW == 1 && walkgrid[nextrow][nextcol] != '.'){
nextrow++;
continue;
}
if(randNSEW == 2 && walkgrid[nextrow][nextcol] != '.'){
nextcol++;
continue;
}
if(randNSEW == 3 && walkgrid[nextrow][nextcol] != '.'){
nextrow--;
continue;
}
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);
}
}
}
}
答案 0 :(得分:0)
以下代码可以解决您的问题。你基本上没有&#39;州&#39;区分有效的举动,而不是去跳跃&#39;过去的访问点。我试图让代码尽可能接近现有代码。
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;
}