gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:06:49 25 4
gpt4 key购买 nike

晚上/早上,伙计们!我在为学校项目工作的一个小程序上遇到了问题。目标是创建一个具有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com