java - 在战舰游戏中创建船只
问题描述
我正在我的一个班级中创建战舰游戏。我在创建“PTBoat”并将其放置在屏幕上时遇到了麻烦。我有一个 10x10 的板,需要帮助来创建初始化船。PTBoat 的大小需要是板上的 2 个点。我想我已经弄清楚了随机的方向、列和行,但我被困在如何在棋盘类中调用它。第一部分是 PTBoat 类,后半部分是 Board 类。主要方法已经为我完全编写好了,所以我没有包括它。
/**
* Class for the PT Boat of the Battleship Game.
* Superclass: Ship
* Subclasses: none
* Attributes: String name
* int charges;
* name = "PT Boat"
* size = 2
* charges - number of depth charges on this PT Boat
* You must declare the attributes, complete the constructors and
* write any necessary getters and setters
*/
public class PTBoat extends Ship
{
// instance variables
public String PTBoat;
public int charges = 2;
public int size = 2;
/**
* Constructors for objects of class Carrier
*/
public PTBoat(String name, int size)
{
}
public PTBoat(int row, int col, int dir)
{
row = (int)(Math.random() * 10 + 1);
col = (int)(Math.random() * 10 + 1);
dir = (int)(Math.random() * 2 + 1);
}
}
/**
* Board class for the Battleship game.
*
* A board is an nxn array containing one each of: Carrier
* Battleship
* Destroyer
* Submarine
* PTBoat
*/
import java.util.Scanner;
public class Board
{
// class variables
// If the number of ships is changed, the constructor must be
// updated as well
// When debug is true, positions of ships will be shown when the
// board is displayed. This is helpful when testing the game since
// you won't have to guess the ships' locations.
final static boolean debug = true;
final static int board_size = 10;
final static int num_ships = 5;
// Characters printed when the board is displayed. Not all are used.
final static char hit = 'H';
final static char miss = 'M';
final static char used = 'S';
final static char blank = ' ';
final static char sunk = 'X';
// instance variables - replace the example below with your own
private char[][] board = new char[board_size][board_size];
private int num_sunk;
private Ship[] ships = new Ship[num_ships];
private Scanner scanIn;
/**
* Default constructor for objects of class Board
*/
public Board(Scanner s)
{
// initialise instance variables
scanIn = s;
num_sunk = 0;
initializeBoard();
// create the ships
ships[0] = new PTBoat();
hideShip(0);
ships[1] = new Submarine();
hideShip(1);
ships[2] = new Destroyer();
hideShip(2);
ships[3] = new Battleship();
hideShip(3);
ships[4] = new Carrier();
hideShip(4);
}
解决方案
您对 PTBoat 类有一些误解,让我们从几个开始:
属性之一应该是船名,在本例中为“PT Boat”,但您定义了这个:
public String PTBoat;
所以你的变量被命名为 PTBoat,与错误的类相同。它应该命名为“名称”,如下所示:
public String name;
第一个构造函数有两个参数,因此在调用时它将用于为相关属性赋值,如下所示:
public PTBoat(String name, int size)
{
this.name = name;
this.size = size;
}
第二个构造函数看起来像是要设置属性行、col 和 dir,您应该在类中定义这些属性。使用您的代码,您正在重新分配您收到的变量的值,这是无用的。稍后,当您从 Board 实例化船时,您应该使用如下构造函数之一:
ships[0] = new PTBoat("MyAwesomePTBoat", 2);
在您的说明中还提到您需要创建 getter 和 setter,因此您的变量看起来应该是私有的,而不是公共的。getter 是一种返回变量值的简单方法,setter 用于将值设置为给定的值。这些方法是公共的,因此可以从定义它们的类外部调用它们。
无论如何,我建议您查看任何基本的 Java 教程,以更好地理解所有这些概念。
推荐阅读
- c - 如何用按钮切换字符串
- dart - 如何读取分块流的一部分
- amazon-web-services - AWS::Serverless::HttpApi 最低 TLS 版本
- python - 自变量范围内的最大值和最小值
- paypal - 未调用 PayPal IPN 挂钩
- c++ - 带有运算符“<=>”的 C++ 模板类错误(错误 C2678)
- c - C程序只能在给定绝对路径时写入文件,而在给定相对路径时不能写入
- javascript - 使用 Cloud Functions 将大数据从 BigQuery 导入 Firestore
- javascript - React 处理表单组件
- c++ - 将 MatrixXd 的行传递给要修改的函数,而不在 Eigen 中创建副本