2016-09-19 155 views
-3

我們的任務是製作一個簡單的'pac-man'遊戲,只需吃一個矩形內的垃圾。垃圾是「*」。 我迄今爲止代碼:Java幫助 - 製作'清掃'遊戲

public class Sweeper 
{  
    public static void main(String[] args) throws InterruptedException 
    { 
     //************************************************************ 
     // Variable set up 
     int sy = 10, sx= 10; // Box size 
     int x= 5, y= 5; // Start point 
     int maxmc = 8; // Max distance moving 
     char [] [] env = new char[sy][sx]; 
     int right = sx - 2, top = sy - 2, left = sx - (right + 1) , bottom = sy - (top + 1); 
     //************************************************************ 

     //************************************************************ 
     // Filling the grid with Stars 
     for(int i=0; i<sy;i++) 
     { 
      for(int j =0; j < sx; j++) 
      { 
       env[i][j] = '*'; 
      } 
     } 
     //************************************************************ 

     int mc = 0, direction = 0, count = (right * top); 
     // The actual game 
     while(count != 0) 
     { 
      direction = (int)(Math.random() * 3); 
      // Display 
      //System.out.println("\n\n\n\n\n"); 
      for(int i = 0; i < sy; i++) 
      { 
       for(int j= 0; j< sx; j++) 
       { 
        System.out.print(env[i][j]); 

       } 

       System.out.println(); 
      } 

      System.out.println("\n\n\n"); 

      Thread.sleep(700); 

      System.out.println(direction); 

      if((x <= right && x >= left && y <= top && y >= bottom)) 
      { 
       if(env[x][y] == ' ') 
       { 
        // RIGHT 
        if(direction == 0) 
        { 
         env[y][x] = '@'; 
         x--; 
         env[y][x+2] = ' '; 
        } 
        // left 
        else if(direction == 1) 
        { 
         env[y][x] = '@'; 
         x++; 
         env[y][x-2] = ' '; 
        } 
        // TOP 
        else if(direction ==2) 
        { 
         env[y][x] = '@'; 
         y--; 
         env[y+2][x] = ' '; 
        } 
        // bottom 
        else if(direction ==3) 
        { 
         env[y][x] = '@'; 
         y++; 
         env[y-2][x] = ' '; 
        } 
       } 
       else 
       { 
        if(direction == 0) 
        { 
         env[y][x] = '@'; 
         x--; 
         env[y][x+2] = ' '; 
        } 
        // left 
        else if(direction == 1) 
        { 
         env[y][x] = '@'; 
         x++; 
         env[y][x-2] = ' '; 
        } 
        // TOP 
        else if(direction ==2) 
        { 
         env[y][x] = '@'; 
         y--; 
         env[y+2][x] = ' '; 
        } 
        // bottom 
        else if(direction ==3) 
        { 
         env[y][x] = '@'; 
         y++; 
         env[y-2][x] = ' '; 
        } 
        // Keeps track of the trash 
        count--; 
       } 
      } 

     } 

    } 

} 

我的問題: 它複製「@」並停止甚至活動。我試圖讓它移動aorund,直到所有內部8x8星符號全部消失。

+3

你有沒有嘗試單步執行IDE調試器中的代碼?那是開始的地方。 –

+2

https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –

回答

0

只是爲了清理你的代碼:你的移動算法是重複的(4 if/else語句)。您只需要在if(! env[x][y] == ' ')聲明中輸入count--

對於你的問題:你的不好籤的邊界條件,你做

env[y][x] = '@'; 
x--; 
env[y][x+2] = ' '; 

但如果x=1,那麼代碼後,它的0,因爲你有

 if((x <= right && x >= left && y <= top && y >= bottom)) 

隨着x=0right=0 ,那麼它永遠不會進入聲明,count不能改變,並且它有效地循環。