2016-03-09 67 views
-1

該程序的目的是生成一個由週期組成的二維網格數組。用戶將在網格上指定要放置的「步行者」的座標,標記爲'A'。步行者將選擇隨機的主要方向進入,每次選擇一個新的方向,並用字母表中的下一個字母標記它,直到達到「Z」,或者直到它超出邊界。如果它到達一個已經存在的空間,它將檢查其他三個方向並移動到它找到的下一個空白區域。在二維步行網格數組中跳過空格

該程序大部分運行良好,但通常在運行的中點附近會跳過幾個空格,或者留出空隙,或者顯示爲斜向移動(不應該)。

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

你一直在遊行這個程序很長時間 - 你有沒有給過幫助你的人點? – gpasch

+0

是的,我只是沒有足夠的代表,所以它不顯示。 –

回答

0

我發現了這個問題。在尋找新的旅行方向的過程中,您從不設置randNSEW以適應變化。您需要添加一條線,將其重新分配以對應與新值randNSEWS相關的方向。以下工作代碼:

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

此外,疏忽:沒有檢查,看看是否所有四個相鄰的位置已被使用。如果DrunkWalker移動到它已經訪問過的位置所包圍的位置,程序將掛起,因爲它找不到另一個行進方向。我建議在另一個if語句中編寫代碼,該語句檢查DrunkWalker是否被困,如果是的話逮捕他。

+0

非常好,非常感謝。 –

+0

我需要弄清楚如何確定它是否超出界限,如果它遇到了它已經存在的地方,它將返回到原來的位置。我一直嘗試不同的方式,邏輯似乎不透氣,但出於某種原因,它不會起作用。我恢復了我的項目,回到之前我嘗試過的方式。 http://pastebin.com/ne1YC65n 這樣做的最好方法是什麼? –