在战舰游戏中创建船只 [英] Creating boats in battleship game

查看:59
本文介绍了在战舰游戏中创建船只的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的一个班级中创建一个战舰游戏.我在创建PTBoat"并将其放置在屏幕上时遇到问题.我有一个 10x10 的板子,需要帮助来创建初始化船只.PTBoat 的大小需要是板上的 2 个点.我想我已经弄清楚了随机的方向、列和行,但我仍然坚持如何在板类中调用它.第一部分是 PTBoat 类,代码的后半部分是 Board 类.主要方法已经完全为我编写了,所以我没有包括它.

I am creating a battleship game in one of my classes. I am having trouble on creating the "PTBoat" and placing it on the screen. I have a 10x10 board and need help creating an initializing the boats. The size of the PTBoat needs to be 2 spots on the board. I think I have the random direction, column, and row figured out but am stuck on how to call it in the board class. The first portion is the PTBoat class and the second half of code is the Board Class. The main method has been fully written for me so I did not include that.

 /**
     * 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 类有一些误解,让我们从几个开始:

You have some misconceptions for the class PTBoat, let's start with a few:

其中一个属性应该是船名,在本例中为PT Boat",但您定义了这个:

One of the attributes should be the name of the ship, in this case, "PT Boat", but you defined this:

public String PTBoat;

所以你的变量被命名为PTBoat,与错误的类相同.它应该被命名为name",如下所示:

So your variable is named PTBoat, the same that the class which is wrong. It should be named "name", like this:

public String name;

第一个构造函数有两个参数,因此在调用时它将用于为相关属性赋值,如下所示:

The first constructor has two arguments so when called it will be used to give values to the related attributes, like this:

public PTBoat(String name, int size)
{
   this.name = name;
   this.size = size;
}

第二个构造函数看起来像是要设置属性 row、col 和 dir,您应该在类中定义这些属性.使用您的代码,您正在重新分配您收到的变量的值,这是无用的.稍后当你从 Board 实例化飞船时,你应该使用这样的构造函数之一:

The second constructor looks like it's intended to set up the attributes row, col and dir, that you should define in the class. With your code you are reassigning the value of the variables that you receive, which is useless. Later when you instantiate the ship from the Board you should use one of the constructors like this:

ships[0] = new PTBoat("MyAwesomePTBoat", 2);

在您的说明中还提到您需要创建 getter 和 setter,因此您的变量看起来应该是私有的,而不是公共的.getter 是一种返回变量值的简单方法,setter 用于将值设置为给定的值.这些方法是公共的,因此可以从定义它们的类外部调用它们.

In your instructions is also mentioned that you need to create getters and setters, so your variables look like they should be private, not public. A getter is a simple method that returns the value of the variable, a setter is used to set the value to a value given. Those methods are public so they can be called from outside the class where they are defined.

无论如何,我建议您查看任何基本的 Java 教程,以更好地理解所有这些概念.

Anyway, I would recommend you have a look at any basic Java tutorial to understand better all those concepts.

这篇关于在战舰游戏中创建船只的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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