Need help programming

I'm trying to develop a one-way Battleship game, and I have almost everything set up. I only need to include an array that holds 5 Ships objects at this time. The class that I created for each ship is called Ships.java. I used to have problems initializing the array, but this was resolved.

The problem arises when I try to pull the length of the vessel (2, 3, 4 or 5) from the index in the array. Iโ€™m not sure how to conceptually go about placing ships.

I feel like I have tried every combination of do-whiles, for loops and if statements. I even tried the switch.

The goal is for the computer to select positions for five ships and set each cell in the grid (ROWSxCOLS) to NC_SHIP (no click, plus ship). The problem is that it checks the position of cells adjacent to a random location on the grid. It is also necessary to check whether the relevant vessel will comply (pulling from ships [i] .getShipLength ()).

Here is the code that I still have:

int shipsPlaced = 0; for (int i = 0; i < ships.length; i++) { boolean shipPlaced = false; do { int randomRow = (int)(Math.random()*ROWS); int randomCol = (int)(Math.random()*COLS); int p = 0; if (randomRow - ships[p].getShipLength() >= 0 && gameBoard[(randomRow - p)][randomCol] == NC_EMPTY) { for (int x = 0; x < ships[x].getShipLength(); x++) { gameBoard[(randomRow - x)][randomCol] = NC_SHIP; shipsPlaced = shipsPlaced + 1; if (x == ships[x].getShipLength()) { shipPlaced = true; p = p + 1; } } } }while (shipPlaced == false); } 

Everything has been initialized and installed if it does not appear here. The problem is the math / logic used to place ships in "random" locations.

+6
source share
3 answers

First of all: all your ships will go horizontally, you must also randomize the direction of the ship.

There are two ways to deal with this problem:

  • Set your starting position first, then see if the ship is suitable.
  • List all available items first, then make a selection for a number equal to the length of the list

1 - Do a recursive search for a random starting position (x, y) (which should be free if you do not transfer the position). In the lookForPos recursive method , make randomPlacementDirection , and from it a flag (for example, is horizontal). If it does not fit (the length from the beginning to the end position overflows the size of the matrix), re-throw . Close positions (position, position + 1, position + 2, ..., position + n), where "n" is the length of your ship, and position - a pair of x, y, and +1 affects only one of the cardinals (in depending on whether it is horizontal or not), if any of them are also busy re-throw . In the end, you get what you need.

2 - Make a list of all the positions where it fits (the "for" structure), both horizontal and vertical, and then randomize the length of the list.

+2
source

What will i do:

  • Get a random place to try and place your ship there. - how did you do

  • In the iteration loop to this point (go through the whole array - it seems you need 2 cycles - one to the end of the array, and the next - from the beginning to this point)

  • call the freeRowsInARow() and freeColsInARow() methods witch returns the number of the largest vessel that can be inserted there, starting from this point (x, y)

  • Check if your ship has size <= returnedValue (from the methods mentioned above), if so - call the method by inserting it there (choose the method - vertical or horizontal) and an interrupt cycle (with return ); if no-ofc continue the search.

0
source

So, your problem is the random placement of battleships on the board. Interesting. This is how I do it.

First, suppose we have the Ship class:

 public class Ship { int size; public Ship(int size) { this.size = size; } public int getSize() { return size; } } 

Then I would have a BattleshipBoard class as follows:

 public class BattleshipBoard { private final int rows; private final int cols; private char[][] board; public BattleshipBoard(int rows, int cols) { this.rows = rows; this.cols = cols; board = new char[rows][cols]; } public void place(Ship[] ships) { Arrays.sort(ships, new Comparator<Ship>() { @Override public int compare(Ship s1, Ship s2) { return Integer.valueOf(s1.size).compareTo(Integer.valueOf(s2.size)); } }); for (int j = 0; j < rows; j++) for (int k = 0; k < cols; k++) board[j][k] = '-'; // Empty position char[][] checked = new char[rows][cols]; Random random = new Random(); for (int i = ships.length - 1; i >=0; i--) { for (int j = 0; j < rows; j++) for (int k = 0; k < cols; k++) checked[j][k] = 'U'; // Unchecked position boolean placed = false; while (! placed) { int r = random.nextInt(rows); int c = random.nextInt(cols); if (checked[r][c] == 'U') { checked[r][c] = 'C'; // Checked position if (board[r][c] == '-') { int direction = random.nextInt(4); // 0 = North; 1 = East; 2 = South; 3 = West; if (canPlace(ships[i], r, c, direction)) { place(ships[i], r, c, direction); placed = true; } } } } } } private void place(Ship ship, int row, int col, int direction) { int size = ship.getSize(); switch (direction) { case 0: // North for (int i = row; i >= row - (size - 1); i--) board[i][col] = 'S'; break; case 1: // East for (int i = col; i <= col + (size - 1); i++) board[row][i] = 'S'; break; case 2: // South for (int i = row; i <= row + (size - 1); i++) board[i][col] = 'S'; break; default: // West for (int i = col; i >= col - (size - 1); i--) board[row][i] = 'S'; break; } } private boolean canPlace(Ship ship, int row, int col, int direction) { int size = ship.getSize(); boolean thereIsRoom = true; switch (direction) { case 0: // North if (row - (size - 1) < 0) thereIsRoom = false; else for (int i = row; i >= row - (size - 1) && thereIsRoom; i--) thereIsRoom = thereIsRoom & (board[i][col] == '-'); break; case 1: // East if (col + (size - 1) >= cols) thereIsRoom = false; else for (int i = col; i <= col + (size - 1) && thereIsRoom; i++) thereIsRoom = thereIsRoom & (board[row][i] == '-'); break; case 2: // South if (row + (size - 1) >= rows) thereIsRoom = false; else for (int i = row; i <= row + (size - 1) && thereIsRoom; i++) thereIsRoom = thereIsRoom & (board[i][col] == '-'); break; default: // West if (col - (size - 1) < 0) thereIsRoom = false; else for (int i = col; i >= col - (size - 1) && thereIsRoom; i--) thereIsRoom = thereIsRoom & (board[row][i] == '-'); break; } return thereIsRoom; } public void printBoard() { for (int i = 0; i < rows; i++) System.out.println(Arrays.toString(board[i])); } } 

Then, if you have something like this:

 public static void main(String[] args) { Ship[] ships = new Ship[] { new Ship(1), new Ship(3), new Ship(2), new Ship(3) }; BattleshipBoard battleshipBoard = new BattleshipBoard(7, 7); battleshipBoard.place(ships); battleshipBoard.printBoard(); } 

you can get the following output (depending on your Random generator):

 [-, -, -, -, -, -, -] [-, -, -, -, -, -, -] [-, -, S, -, -, -, -] [-, -, -, -, -, -, -] [-, S, -, S, S, -, -] [-, S, -, -, -, -, -] [-, S, -, -, S, S, S] 

which is the random placement of four ships of sizes 1, 2, 3 and 3.

A few comments:

  • the place() method tries to randomly place ships by choosing a random starting position (r,c) and a random direction

  • the ships array is sorted, so first we start with the largest ships (itโ€™s easier to place them when our board takes up less space)

  • the checked matrix is โ€‹โ€‹used to avoid checking the same random position (r,c) more than once when trying to place a ship

  • The canPlace() method returns true if the passed Ship can be placed on the board , starting at position (r,c) and moving to the passed direction

  • the place() method puts the passed Ship on the board , starting at position (r,c) , and goes to the passed direction

0
source

Source: https://habr.com/ru/post/885657/


All Articles