基于文本的战舰游戏打印两个相同的网格 - Java [英] Text based battleship game printing two of the same grids - Java

查看:24
本文介绍了基于文本的战舰游戏打印两个相同的网格 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么,但是每当您或计算机在我的游戏中受到攻击时,网格都会更新.播放器和计算机网格是-"字符的多维数组,当您点击时它会更改为x".在游戏循环中,它们是玩家网格和计算机网格,我在不同的时间分别更新它们,但是当它们被打印出来时,它们都是相同的.有人可以帮忙吗?抱歉,我是编程新手

I dont know why but whenever you or the computer gets a hit in my game, the grids both get updated. The player and computer grids are multidimensional arrays of '-' chars and when you get a hit it changes to 'x'. In the game loop, their is a player grid and computer grid and I update them each seperately at different times but when they get printed their both the same. Can someone help? Sorry I'm new to programming

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 个点,而没有其他船只(我认为这些船只只是重叠,但我尝试进行修复).无论如何,如果您能提供帮助,请提前致谢.

i got the grids to print separately but theres something wrong with my place ships method. Can someone take a look at it? It's suppose to choose random x,y coordinates(3 points for each ship) and do this for 3 ships. It places 4 points in a row sometimes and no other ships(I think the ships are just overlapping, but I tried to put a fix in). Anyways, thanks in advance if you can help.

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

推荐答案

这是问题所在:

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

您在此处实际上只有一个 char[][] 对象.两个变量都引用同一个对象……对该对象所做的任何更新都将通过这两个变量可见.

You've only actually got a single char[][] object here. Both variables refer to the same object... any updates made to that object will be visible via both variables.

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

It looks like Grid.gridArray is actually a static variable, which is another problem. You almost certainly want to make it an instance variable... and then create two instances of Grid rather than just one.

基本上,我会退后一步,弄清楚 Grid 的实例意味着什么.你真的需要公开 gridArray 变量吗?为什么网格不能打印自己?

Basically, I would take a step back and work out what an instance of Grid is meant to represent. Do you really need to expose the gridArray variable at all? Why can a grid not print itself?

这篇关于基于文本的战舰游戏打印两个相同的网格 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆