Public Group

# Java BattleShip help

This topic is 3994 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

how do i read col and rows and make ships show. this is what i have so far please help.  public class Board { // 0 = blank, 1 = "X", -1 = "O" int grid[][]; String q = " | A | B | C | D | E | F | G | H | I | J |"; String c = " |___|___|___|___|___|___|___|___|___|___|"; Board() { grid = new int[10][10]; for (int r = 0; r < grid.length; r++) for (int c = 0; c < grid[r].length; c++) grid[r][c] = 0; } int getGrid(int c, int r) { return grid[c][r]; } } draw(human, comp); input=IN.readLine(); int Variable = Integer.parseInt(IN.readLine()); } static void draw(Player P, Computer C) { String eq = " ========================================= =========================================\n"; String bar = " _________________________________________ _________________________________________\n"; System.out.print("\t | +_-_BATTLESHIP_-_+ |\n" + eq); System.out.print(" PLAYER COMPUTER\n" + bar); String q =" | A | B | C | D | E | F | G | H | I | J | | A | B | C | D | E | F | G | H | I | J |\n"+bar; System.out.print(q); String row = " |___|___|___|___|___|___|___|___|___|___| |___|___|___|___|___|___|___|___|___|___|"; for (int r = 0; r < 10; r++) { System.out.print((r + 1)); if (r < 9) System.out.print(" "); for (int c = 0; c < 10; c++) { System.out.print("| " + P.getMark(c, r) + " "); } System.out.print("| "); for (int c = 0; c < 10; c++) { System.out.print("| " + C.getMark(c, r) + " "); } System.out.print("|\n" +row+"\n"); } } public class Player { Board fred = new Board(); Ship carrier = new Ship(5, "Aircraft Carrier"); Ship patrol =new Ship(2, "Patrol Boat"); Ship Cruiser= new Ship( 3, "Cruiser"); Ship Destroyer= new Ship(4, "Destroyer"); Ship Submarine = new Ship( 3, "Submarine"); // ships char getMark(int c, int r) { switch (fred.getGrid(c, r)) { case 1: return '*'; //hit case -1: return 'O'; //miss case 50: return 'A';//Aircraft Carrier 5 case 40: return 'C';//Cruiser 3 case 30: return 'D';//Destroyer 4 case 20: return 'S';//Submarine 3 hits case 10: return 'P';//Patrol Boat 2 hits default: return ' '; } } } public class Ship{ String name; int length; Ship(int L, String N){ length = L; name = N; } int getLength() { return length; } String getName() { return name; } }  Thanks in advance.

##### Share on other sites
You need to store your game board in a 2-dimensional array, not strings. It would look something like this:

char board[][] = {{0, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z},{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},{2, ...{3, ...}

The 0's are just empty spaces, but you'd access the individual position with a NUMBER first then a LETTER (in the order only).

This is pseudo-code pretty much, but you get the idea. A 2D array is indexed by rows then columns, so the index of F would be board[0][5]. Look into more examples of 2D arrays to get the hang of them and you'll find it's pretty intuitive to implement for your purposes.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634060
• Total Posts
3015300
×