2016-02-25 58 views
0

该程序的目的是生成由句点(。)组成的二维网格数组。用户指定以'A'标记的“步行者”的起始点,然后步行者将生成0-3的数字以表示四个基本方向。它会随着这些随机的方向移动,同时每增加一个字母,它就会离开它,直到它跑到墙上并被“逮住”或达到“使它回家”的“Z”。如果它跑到一个已经去过的地方,它必须沿相同的方向向前跳,直到它到达一个空的空间或撞到墙上。2D网格移动检查

我现在的问题是,我已经在柜台上确认它没有运行过'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

下面的代码解决您的问题。你基本上没有“国家”区分有效的举措,而是在过去的访问点上“跳跃”。我尽量保持代码尽可能接近您现有的代码。

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

继续看看这篇文章对你有用;你能否认为有用,或者如果你需要进一步的帮助,请告诉我。谢谢。 –