Creating boats in battleship game

可紊 提交于 2019-12-24 20:15:30

问题


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

回答1:


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

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

public String PTBoat;

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

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);

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.

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



来源:https://stackoverflow.com/questions/59165810/creating-boats-in-battleship-game

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!