java - 二维数组错误检查,战舰游戏

标签 java arrays string

晚上/早上,伙计们! 我在为学校项目工作的一个小程序上遇到了问题。目标是创建一个具有 2D 阵列的玩家对 PC 战舰程序。用户应限制为 15/10/5 枚鱼雷/。该程序运行得很好(甚至比我预期的还要好),但不幸的是,当用户尝试在同一地点射击两次时,它会计算鱼雷的数量。另外,如果用户尝试向船所在的同一个“方格”射击两次,它将算作“击中”,这意味着您可以通过在该方格射击来获胜。

我尝试通过在 public static int fire 方法中声明来修复它:

if(board[row-1][col-1].equals("X") || board[row-1][col-1].equals("M") { 鱼雷++; }

不幸的是,没用...这是整个方法:

    public static int fire(String[][] board, int hits, int torpedoes)
   {
      int row = 0, col = 0;
      System.out.println("You have " + torpedoes + " torpedoes left...");
      System.out.println("Select a row to fire in: ");
      row = in.nextInt();
      while(row > 8 || row < 1)
      {
         System.out.println("Enter a valid row (1 - 8) and try again...");
         row = in.nextInt();
      }
      System.out.println("Select a column to fire in: ");
      col = in.nextInt();
      while(col > 8 || col < 1)
      {
         System.out.println("Enter a valid column (1 - 8) and try again...");
         col = in.nextInt();
      }

      if(board[row-1][col-1].equals("S"))
      {
         hits++;
         System.out.println("Hit!");
         board[row-1][col-1] = "X";
      }
      else
      {
         System.out.println("Miss!");
         board[row-1][col-1] = "M";
      }
      return hits;
   }

以及完整的程序(如果它可以帮助某人了解整个事情):

import java.util.*;
import java.util.Scanner;
import java.util.Arrays;

public class BattleshipsButBetter
{
   static Scanner in = new Scanner(System.in);
   public static boolean hideShip = true; //Make the ship hidden or not for testing
   public static void main(String[] args)
   {
      do
      {
         System.out.println("Privet, comrade.");
         System.out.println("Welcome to modified battleship program!");


         String[][] board = new String[8][8];
         createBoard(board);
         createShip(board, 4);

         int torpedoes = 0;
         int hits = 0;
         int difficulty = getDifficulty();

            if(difficulty == 1)
            {
               torpedoes = 15;
            }
            else if(difficulty == 2)
            {
               torpedoes = 10;
            }
            else
            {
               torpedoes = 5;
            }

         System.out.println("You have only " + torpedoes + " torpedoes to sink the ship... good luck!");

         while(torpedoes > 0 && hits < 4)
         {
            showBoard(board);
            hits = fire(board, hits, torpedoes);
            torpedoes--;
         }
         results(hits, torpedoes);  
      }while(repeat());
   }

   public static int getDifficulty()
   {
      System.out.println("Select a difficulty: \n 1. Normal \n 2. Hard \n 3 or any other number = impossible!");
      return in.nextInt();
   }

   public static void createBoard(String[][] board)
   {
      for(int x = 0; x < board.length; x++)
         for(int y = 0; y < board[0].length; y++)
            board[x][y] = "~";
   }

   public static void showBoard(String[][] board)
   {
      System.out.println();
      System.out.println("  1 2 3 4 5 6 7 8");
      for(int x = 0; x < board.length; x++)
      {
         if(hideShip == false)
         {
            System.out.print(x + 1);
            for(int y = 0; y < board[0].length; y++)
            {
               System.out.print(" " + board[x][y]);
            }
            System.out.println("");
         }
         else
         {
            System.out.print(x + 1);
            for(int y = 0; y < board[0].length; y++)
            {
               if(board[x][y].equals("S"))
               {
                  System.out.print(" " + "~");
               }
               else
               {
                  System.out.print(" " + board[x][y]);
               }
            }
            System.out.println("");
         }
      }
      System.out.println();
   }

   public static void createShip(String[][] board, int size)
   {
      if(Math.random() < 0.5)
      {
         int col = (int)(Math.random()*5);
         int row = (int)(Math.random()*7);
         for(int i = 0; i < size; i++)
         {
            board[row][col+i] = "S";
         }
      }
      else
      {
         int col = (int)(Math.random()*7);
         int row = (int)(Math.random()*5);
         for(int i = 0; i < size; i++)
         {
            board[row+i][col] = "S";
         }
      }
   }

   public static int fire(String[][] board, int hits, int torpedoes)
   {
      int row = 0, col = 0;
      System.out.println("You have " + torpedoes + " torpedoes left...");
      System.out.println("Select a row to fire in: ");
      row = in.nextInt();
      while(row > 8 || row < 1)
      {
         System.out.println("Enter a valid row (1 - 8) and try again...");
         row = in.nextInt();
      }
      System.out.println("Select a column to fire in: ");
      col = in.nextInt();
      while(col > 8 || col < 1)
      {
         System.out.println("Enter a valid column (1 - 8) and try again...");
         col = in.nextInt();
      }

      if(board[row-1][col-1].equals("S"))
      {
         hits++;
         System.out.println("Hit!");
         board[row-1][col-1] = "X";
      }
      else
      {
         System.out.println("Miss!");
         board[row-1][col-1] = "M";
      }
      return hits;
   }

   public static void results(int hits, int torpedoes)
   {
      if(hits < 4)
         System.out.println("Sorry, you didn't sink the ship :(");
      if(torpedoes < 1)
         System.out.println("You have lost all of your torpedoes!");
      else
         if(hits >= 4)
         {
            System.out.println("Congratulations, comrade, you sank the ship. GG WP!");
         }
   }

   public static boolean repeat()
   {
      int repeat;
      System.out.println();
      Scanner in = new Scanner(System.in);
      do
      {
         System.out.println("Would you like to play again? 1. YES, 2. NO");
         repeat = in.nextInt();
         if(repeat < 1 || repeat > 2)
         {
            System.out.println(repeat + " is not a valid entry.");
         }
      }
      while(repeat < 1 || repeat > 2);
      System.out.println();
      return repeat == 1;
   }
}

最佳答案

您修复游戏的方法是正确的,但有一个问题:Java 中的整数是按值传递的,因此对 fire 方法内的整数鱼雷对其外部没有任何影响。

为了使其正常工作,我在程序中添加了一个静态类 FireResult。此类存储调用 fire 方法产生的命中鱼雷计数:

import java.util.*;
import java.util.Scanner;
import java.util.Arrays;

public class BattleshipsButBetter {

    static Scanner in = new Scanner(System.in);
    public static boolean hideShip = true; //Make the ship hidden or not for testing
    private static FireResult fr = new FireResult();

    public static void main(String[] args)
    {
        do
        {
            System.out.println("Privet, comrade.");
            System.out.println("Welcome to modified battleship program!");


            String[][] board = new String[8][8];
            createBoard(board);
            createShip(board, 4);

            int torpedoes = 0;
            int hits = 0;
            int difficulty = getDifficulty();

            if(difficulty == 1)
            {
                torpedoes = 15;
            }
            else if(difficulty == 2)
            {
                torpedoes = 10;
            }
            else
            {
                torpedoes = 5;
            }

            System.out.println("You have only " + torpedoes + " torpedoes to sink the ship... good luck!");

            while(torpedoes > 0 && hits < 4)
            {
                showBoard(board);
                fire(board, hits, torpedoes);
                // Get results from FireResult static class
                hits = fr.hits;
                torpedoes = fr.torpedoes;

                // Original logic continues
                torpedoes--;
            }
            results(hits, torpedoes);
        }while(repeat());
    }

    public static int getDifficulty()
    {
        System.out.println("Select a difficulty: \n 1. Normal \n 2. Hard \n 3 or any other number = impossible!");
        return in.nextInt();
    }

    public static void createBoard(String[][] board)
    {
        for(int x = 0; x < board.length; x++)
            for(int y = 0; y < board[0].length; y++)
                board[x][y] = "~";
    }

    public static void showBoard(String[][] board)
    {
        System.out.println();
        System.out.println("  1 2 3 4 5 6 7 8");
        for(int x = 0; x < board.length; x++)
        {
            if(hideShip == false)
            {
                System.out.print(x + 1);
                for(int y = 0; y < board[0].length; y++)
                {
                    System.out.print(" " + board[x][y]);
                }
                System.out.println("");
            }
            else
            {
                System.out.print(x + 1);
                for(int y = 0; y < board[0].length; y++)
                {
                    if(board[x][y].equals("S"))
                    {
                        System.out.print(" " + "~");
                    }
                    else
                    {
                        System.out.print(" " + board[x][y]);
                    }
                }
                System.out.println("");
            }
        }
        System.out.println();
    }

    public static void createShip(String[][] board, int size)
    {
        if(Math.random() < 0.5)
        {
            int col = (int)(Math.random()*5);
            int row = (int)(Math.random()*7);
            for(int i = 0; i < size; i++)
            {
                board[row][col+i] = "S";
            }
        }
        else
        {
            int col = (int)(Math.random()*7);
            int row = (int)(Math.random()*5);
            for(int i = 0; i < size; i++)
            {
                board[row+i][col] = "S";
            }
        }
    }

    public static void fire(String[][] board, int hits, int torpedoes)
    {
        int row = 0, col = 0;
        System.out.println("You have " + torpedoes + " torpedoes left...");
        System.out.println("Select a row to fire in: ");
        row = in.nextInt();
        while(row > 8 || row < 1)
        {
            System.out.println("Enter a valid row (1 - 8) and try again...");
            row = in.nextInt();
        }
        System.out.println("Select a column to fire in: ");
        col = in.nextInt();
        while(col > 8 || col < 1)
        {
            System.out.println("Enter a valid column (1 - 8) and try again...");
            col = in.nextInt();
        }

        if(board[row-1][col-1].equals("X") || board[row-1][col-1].equals("M"))
        {
            torpedoes++;
        }
        else if(board[row-1][col-1].equals("S"))
        {
            hits++;
            System.out.println("Hit!");
            board[row-1][col-1] = "X";
        }
        else
        {
            System.out.println("Miss!");
            board[row-1][col-1] = "M";
        }

        fr.hits = hits;
        fr.torpedoes = torpedoes;
    }

    public static void results(int hits, int torpedoes)
    {
        if(hits < 4)
            System.out.println("Sorry, you didn't sink the ship :(");
        if(torpedoes < 1)
            System.out.println("You have lost all of your torpedoes!");
        else
        if(hits >= 4)
        {
            System.out.println("Congratulations, comrade, you sank the ship. GG WP!");
        }
    }

    public static boolean repeat()
    {
        int repeat;
        System.out.println();
        Scanner in = new Scanner(System.in);
        do
        {
            System.out.println("Would you like to play again? 1. YES, 2. NO");
            repeat = in.nextInt();
            if(repeat < 1 || repeat > 2)
            {
                System.out.println(repeat + " is not a valid entry.");
            }
        }
        while(repeat < 1 || repeat > 2);
        System.out.println();
        return repeat == 1;
    }

    public static class FireResult {
        int hits;
        int torpedoes;
    }
}

希望有帮助;)

关于java - 二维数组错误检查,战舰游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52807423/

相关文章:

java - 在JavaFx中,如何从网格 Pane 中删除特定节点及其坐标?

类中的 C++ 2d "dynamic"数组?

arrays - Dart 。不能'改变列表的元素

c - 使用 stdlib.c 中的 qsort() 根据每个 *char 中的第三个字母对 C 中的字符串数组进行排序

java - 解析字符串以获取特定值

java - WebView 无法从 wordpress 网站下载媒体(POST)

java - 无法获取 org.hibernate.persister.entity.SingleTableEntityPersister 的构造函数。空指针异常

c++ - 在c++中通过字符串获取输入

java - Spring Data MongoDB 聚合 $out 管道

python - 如何将字节元组转换为整数并返回?