基于文本的战舰游戏打印两个相同的网格-Java

基于文本的战舰游戏打印两个相同的网格-Java,java,text,Java,Text,我不知道为什么,但是每当你或电脑在我的游戏中受到攻击时,网格都会更新。 玩家和计算机网格是多维数组中的“-”字符,当你命中它时,它会变成“x”。在游戏循环中,他们是一个玩家网格和计算机网格,我在不同的时间分别更新他们,但当他们被打印出来时,两者都是相同的。有人能帮忙吗?对不起,我是编程新手 public class Game{ public static void main(String[] args){ Grid grid = new Grid(); Computer co

我不知道为什么,但是每当你或电脑在我的游戏中受到攻击时,网格都会更新。 玩家和计算机网格是多维数组中的“-”字符,当你命中它时,它会变成“x”。在游戏循环中,他们是一个玩家网格和计算机网格,我在不同的时间分别更新他们,但当他们被打印出来时,两者都是相同的。有人能帮忙吗?对不起,我是编程新手

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-6之间的数字
公众静态播放人数(){
int randX,randY;
int direction;//将是随机的int 0-1,确定船舶将延伸的方向(上/下、左/右)
randX=(int)(Math.random()*7);
randY=(int)(Math.random()*7);
方向=(int)(Math.random()*2);
玩家1[0]=randX;
玩家1[1]=兰迪;
如果(方向==0){//向上或向下延伸2个单位(y值改变,x保持不变)
玩家1[2]=randX;
玩家人数1[4]=randX;
如果(randY>3){//如果y值大于3,则必须向下延伸,否则它将不适合
玩家1[3]=randY-1;
玩家1[5]=randY-2;
}否则如果(randY3){//如果x大于3,则必须向左延伸,否则它将不适合
玩家1[2]=randX-1;
玩家1[4]=randX-2;
}否则如果(randX3){
玩家2[3]=randY-1;
玩家2[5]=randY-2;
}else if(randY3){
玩家2[2]=randX-1;
玩家2[4]=randX-2;
}否则如果(randX3){
玩家3[3]=randY-1;
玩家3[5]=randY-2;
}else if(randY3){
玩家3[2]=randX-1;
玩家3[4]=randX-2;
}否则,如果(randX这就是问题所在:

char[][] playerGrid= Grid.gridArray;
char[][] computerGrid = Grid.gridArray;
实际上,这里只有一个
char[][]
对象。两个变量都指向同一个对象…对该对象所做的任何更新都将通过这两个变量可见

它看起来像是
Grid。gridArray
实际上是一个静态变量,这是另一个问题。您几乎肯定希望将其作为一个实例变量…然后创建两个
Grid
实例,而不仅仅是一个


基本上,我会退一步,找出
Grid
的实例代表什么。你真的需要公开
gridArray
变量吗?为什么网格不能打印自己?

请向我们展示所有相关的代码。
Grid
类是什么?看起来你使用的是相同的引用
Grid.gridAr光线
用于播放器和计算机的网格。