2013-01-16 46 views
-1

我不知道爲什麼,但無論何時您或計算機在我的遊戲中受到一擊,網格都會更新。 玩家和電腦網格是' - '字符的多維數組,當你得到命中時,它變成'x'。在遊戲循環中,他們是一個玩家網格和計算機網格,我在不同的時間單獨更新它們,但是當它們打印出來時它們都是相同的。有人可以幫忙嗎?對不起,我是新手編程基於文本的戰艦遊戲打印兩個相同的網格 - Java

public class Game{ 
    public static void main(String[] args){ 
    Grid grid = new Grid(); 
    Computer computer = new Computer(); 
    Player player = new Player(); 
    String playerGuess; 
    player.setPlayerShips(); 
    computer.setComputerShips(); 
    char[][] playerGrid= Grid.gridArray; 
    char[][] computerGrid = Grid.gridArray; 

    System.out.println("Welcome to BATTLESHIP"); 
    System.out.println("You are to sink your opponents 3 ships, each 3 units in length."); 
    System.out.println("The ships can be both vertical and horizontal."); 
    System.out.println("Enter your coordinate guess in the form A1, B5, F6, etc."); 
    System.out.println("Since the grid is 7x7, coordinates go from A1-G7."); 
    System.out.println("Letters are vertical, numbers are horizontal."); 
    System.out.println("You will also have 3 ships placed randomly, which the computer will also try to guess."); 
    System.out.println("During the game, enter exit if you would like to quit."); 
    System.out.println(); 
    while(true){ 
     System.out.println("Your Grid"); 
     grid.printGrid(playerGrid); 
     System.out.println("Opponent's Grid"); 
     grid.printGrid(computerGrid); 
     System.out.println(); 
     playerGuess=player.getGuess(); 
     if(playerGuess.equals("exit")){ 
     break; 
     }else{ 
     playerGuess=grid.convert(playerGuess); 
     } 
     player.setFirstCo(playerGuess); 
     player.setSecondCo(playerGuess); 
     System.out.println(); 
     if(player.isHit(player.firstCo, player.secondCo)){ 
     player.addHits(player.firstCo, player.secondCo); 
     System.out.println("Hit!"); 
     System.out.println(); 
     computerGrid=Grid.newGrid(computerGrid,player.firstCo,player.secondCo); 
     }else{ 
     System.out.println("Miss."); 
     System.out.println(); 
     } 
     if(player.hasWon()){ 
     System.out.println("Congratulations, you have sunk all your opponents ships!"); 
     break; 
     } 

     computer.guess=computer.getGuess(); 
     computer.lastGuess=computer.guess; 
     if(computer.isHit(computer.guess[0],computer.guess[1])){ 
     computer.addHits(computer.guess[0],computer.guess[1]); 
     System.out.println("Computer has hit!"); 
     System.out.println(); 
     playerGrid=grid.newGrid(playerGrid, computer.guess[0], computer.guess[1]); 
     if(computer.hasWon()){ 
      System.out.println("Computer has sunk all your ships! You lose."); 
      break; 
     } 
     }else{ 
     System.out.println("Computer has missed."); 
     System.out.println(); 
     } 
} 
} 
} 

我得到了網格單獨打印,但是我的地方船的方法有問題。有人可以看看嗎?假設選擇隨機的x,y座標(每艘船3點),併爲3艘船做這個。它有時連續放4點,沒有其他船(我認爲這些船隻重疊,但我試圖修補)。無論如何,如果你能提供幫助,請提前致謝。

//set player ships coordinates, can be numbers from 0-6 
    public static void setPlayerShips(){ 
     int randX, randY; 
     int direction; //will be random int 0-1, determines direction ship will extend(up/down, left/right) 

     randX=(int)(Math.random()*7); 
     randY=(int)(Math.random()*7); 
     direction=(int)(Math.random()*2); 

     playerShip1[0]=randX; 
     playerShip1[1]=randY; 
     if(direction==0){//extend upwards or downwards 2 units(y values change, x stays the same) 
      playerShip1[2]=randX; 
      playerShip1[4]=randX; 
      if(randY>3){//if y value is greater than 3, has to extend down or it wont fit 
       playerShip1[3]=randY-1; 
       playerShip1[5]=randY-2; 
      }else if(randY<2){//if y value is less than 2, has to extend up or it wont fit 
       playerShip1[3]=randY+1; 
       playerShip1[5]=randY+2; 
      }else{//if direction doesnt matter, just extend upwards 
       playerShip1[3]=randY+1; 
       playerShip1[5]=randY+2; 
      } 
     }else if(direction==1){//extends left or right 2 units(y values stay the same, x changes) 
      playerShip1[3]=randY; 
      playerShip1[5]=randY; 
      if(randX>3){//if x is greater than 3, must extend left or it wont fit 
       playerShip1[2]=randX-1; 
       playerShip1[4]=randX-2; 
      }else if(randX<2){//if x is less than 2, must extend right or it wont fit 
       playerShip1[2]=randX+1; 
       playerShip1[4]=randX+2; 
      }else{//if direction doesnt matter, just extend right 
       playerShip1[2]=randX+1; 
       playerShip1[4]=randX+2; 
      } 
     } 
     //do same for both other ships, do quick checks to make sure original coordinates arent the same 
     do{ 
      randX=(int)(Math.random()*7); 
      randY=(int)(Math.random()*7); 
     }while(randX==playerShip1[0] && randY==playerShip1[1]); 
     direction=(int)(Math.random()*2); 

     playerShip2[0]=randX; 
     playerShip2[1]=randY; 
     if(direction==0){ 
      playerShip2[2]=randX; 
      playerShip2[4]=randX; 
      if(randY>3){ 
       playerShip2[3]=randY-1; 
       playerShip2[5]=randY-2; 
      }else if(randY<2){ 
       playerShip2[3]=randY+1; 
       playerShip2[5]=randY+2; 
      }else{ 
       playerShip2[3]=randY+1; 
       playerShip2[5]=randY+2; 
      } 
     }else if(direction==1){ 
      playerShip2[3]=randY; 
      playerShip2[5]=randY; 
      if(randX>3){ 
       playerShip2[2]=randX-1; 
       playerShip2[4]=randX-2; 
      }else if(randX<2){ 
       playerShip2[2]=randX+1; 
       playerShip2[4]=randX+2; 
      }else{ 
       playerShip2[2]=randX+1; 
       playerShip2[4]=randX+2; 
      } 
     } 
     do{ 
      randX=(int)(Math.random()*7); 
      randY=(int)(Math.random()*7); 
     }while((randX==playerShip1[0]&& randY==playerShip1[1])&&(randX==playerShip2[0] && randY==playerShip2[1])); 
     direction=(int)(Math.random()*2); 

     playerShip3[0]=randX; 
     playerShip3[1]=randY; 
     if(direction==0){ 
      playerShip3[2]=randX; 
      playerShip3[4]=randX; 
      if(randY>3){ 
       playerShip3[3]=randY-1; 
       playerShip3[5]=randY-2; 
      }else if(randY<2){ 
       playerShip3[3]=randY+1; 
       playerShip3[5]=randY+2; 
      }else{ 
       playerShip3[3]=randY+1; 
       playerShip3[5]=randY+2; 
      } 
     }else if(direction==1){ 
      playerShip3[3]=randY; 
      playerShip3[5]=randY; 
      if(randX>3){ 
       playerShip3[2]=randX-1; 
       playerShip3[4]=randX-2; 
      }else if(randX<2){ 
       playerShip3[2]=randX+1; 
       playerShip3[4]=randX+2; 
      }else{ 
       playerShip3[2]=randX+1; 
       playerShip3[4]=randX+2; 
      } 
     } 
    } 
+0

請向我們展示所有相關的代碼。什麼是「Grid」類?似乎你在玩家和電腦的網格中使用了相同的參考'Grid.gridArray'。 – Howard

回答

2

這就是問題所在:

char[][] playerGrid= Grid.gridArray; 
char[][] computerGrid = Grid.gridArray; 

你只實際上有一個char[][]對象這裏。兩個變量都指向同一個對象...對該對象所做的任何更新都將通過這兩個變量可見。

看起來像實際上Grid.gridArray靜態變量,這是另一個問題。你幾乎可以肯定地想讓它變爲實例變量...然後創建兩個Grid的實例,而不僅僅是一個。

基本上,我會後退一步,找出Grid的實例。你真的需要暴露gridArray變量嗎?爲什麼網格不能自行打印?