Battleship 2D Array; problem wi/ int & char

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

43 Replies - 3702 Views - Last Post: 05 April 2011 - 09:38 AM Rate Topic: -----

#1 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 174
  • Joined: 01-March 11

Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 06:35 PM

I have to create a program that allows the user to input coordinates onto a 5x5 2D array in order to locate & destroy a hidden enemy ship. The game begins with a board like this:
     A B C D E
    1 - - - - -
    2 - - - - -
    3 - - - - -
    4 - - - - -
    5 - - - - -

I then have to randomly assign a 2x2 enemy ship without displaying it on the board (right now I'm using a second array to hold the location, unless there is an easier way). The user then fires a shot by specifying the row (1-5) & column (A-E) values. If the shot hits the enemy ship, then a '*' is displayed in that location. If it doesn't hit, then an 'x' is displayed in that location. What I'm having trouble with is:
1. I'm not sure if I'm correctly randomly assigning the enemy ship correctly.
2. The user is supposed to enter the column letter, but I don't know how to convert that from int.
3. I'm confused in how to compare the user's input with the array of the enemy ship to see if it was a hit or not.

Here is my code thus far:
import java.util.Scanner;

public class assignment17 {
	static Scanner in = new Scanner(System.in);

	public static void main(String[] args) {
		System.out.println("     A B C D E");
		System.out.println("    1 - - - - -");
		System.out.println("    2 - - - - -");
		System.out.println("    3 - - - - -");
		System.out.println("    4 - - - - -");
		System.out.println("    5 - - - - -");
		
		final int ROWS = 6;
		final int COLS = 6;
		char [] [] values = { {' ', 'A', 'B', 'C', 'D', 'E'},
							 {'1', '-', '-', '-', '-', '-'},
							 {'2','-', '-', '-', '-', '-'},
							 {'3','-', '-', '-', '-', '-'},
							 {'4','-', '-', '-', '-', '-'},
							 {'5','-', '-', '-', '-', '-'}};
        for (int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLS; col++) {
            	System.out.print(" " + values[row][col]);
            }
            System.out.println("\n");
        }



		final int shipROW = 2;
		final int shipCOL = 2;
		int [] [] shipValues = new int [(int)Math.random() * values.length][(int)Math.random() * values[0].length];
		
		System.out.println("Enter a Row Number: ");
		int guessRow = in.nextInt();
		System.out.println("Enter a Column Letter: ");
		int guessColumn = in.nextInt();

		//I am having problem with this if statement
		if ([guessRow][guessColumn] = shipValues){
			  values [guessRow][guessColumn] = '*';
			  System.out.println("hit");
		        for (int row = 0; row < ROWS; row++) {
		            for (int col = 0; col < COLS; col++) {
		            	System.out.print(" " + values[row][col]);
		            }
		            System.out.println("\n");
		        }
		}else{
			values [guessRow][guessColumn] = 'x';
			System.out.println("miss");
	        for (int row = 0; row < ROWS; row++) {
	            for (int col = 0; col < COLS; col++) {
	            	System.out.print(" " + values[row][col]);
	            }
	            System.out.println("\n");
	        }
			}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Battleship 2D Array; problem wi/ int & char

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 06:43 PM

First noneed to have

		System.out.println("     A B C D E");
		System.out.println("    1 - - - - -");
		System.out.println("    2 - - - - -");
		System.out.println("    3 - - - - -");
		System.out.println("    4 - - - - -");
		System.out.println("    5 - - - - -");



and this

		char [] [] values = { {' ', 'A', 'B', 'C', 'D', 'E'},
							 {'1', '-', '-', '-', '-', '-'},
							 {'2','-', '-', '-', '-', '-'},
							 {'3','-', '-', '-', '-', '-'},
							 {'4','-', '-', '-', '-', '-'},
							 {'5','-', '-', '-', '-', '-'}};


Use only the second one as it is the one you will modify and reprint anyhow. The first one is useless
Was This Post Helpful? 0
  • +
  • -

#3 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 174
  • Joined: 01-March 11

Re: Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 06:49 PM

Ok. Thank you, but from there on how do I go about fixing the problems I have?
Was This Post Helpful? 0
  • +
  • -

#4 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 174
  • Joined: 01-March 11

Re: Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 07:08 PM

i got rid of the first one & am now only using the second one.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 07:26 PM

this is NOt a valid Java statement

if ([guessRow][guessColumn] = shipValues){

[guessRow][guessColumn]

are referencing an array row/column

so it should be

(someArray[guessRow][guessColumn]

the = operator cannot be used in a if statment should be == for testing for equality

if (someArray[guessRow][guessColumn] == shipValues){
Was This Post Helpful? 0
  • +
  • -

#6 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 174
  • Joined: 01-March 11

Re: Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 07:47 PM

That is the problem i'm having because the array i have is in the char form, but the shipValues is in the int form so I am completely lost in where i go from there.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 07:51 PM

View PostAaronHopkins, on 03 April 2011 - 09:47 PM, said:

That is the problem i'm having because the array i have is in the char form, but the shipValues is in the int form so I am completely lost in where i go from there.

Java will automatically convert char to int if you used them in int operations

so you shouln;t worry about it
Was This Post Helpful? 1
  • +
  • -

#8 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 174
  • Joined: 01-March 11

Re: Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 08:09 PM

Ok so i have this now:
		int [] [] values = { {' ', 'A', 'B', 'C', 'D', 'E'},
							 {'1', '-', '-', '-', '-', '-'},
							 {'2','-', '-', '-', '-', '-'},
							 {'3','-', '-', '-', '-', '-'},
							 {'4','-', '-', '-', '-', '-'},
							 {'5','-', '-', '-', '-', '-'}};
        for (int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLS; col++) {
            	System.out.print(" " + values[row][col]);
            }
            System.out.println("\n");
        }



		final int shipROW = 2;
		final int shipCOL = 2;
		int [] [] shipValues = new int [(int)Math.random() * values.length][(int)Math.random() * values[0].length];
		
		System.out.println("Enter a Row Number: ");
		int guessRow = in.nextInt();
		System.out.println("Enter a Column Letter: ");
		int guessColumn = in.nextInt();

		//I am having problem with this if statement
		if (values[guessRow][guessColumn] == shipValues){


& it's giving me an error with this:
if (values[guessRow][guessColumn] == shipValues){


saying it's incompatible operand types int and int[][]
Was This Post Helpful? 0
  • +
  • -

#9 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 174
  • Joined: 01-March 11

Re: Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 08:15 PM

to make sure, am i assigning the random location for the enemy ship correctly?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 08:23 PM

shipValues is declared here as a int[][]:

int [][] shipValues = new int [(int)Math.random() * values.length][(int)

how can you compare the value of an int[][] like in this statement ???

if (values[guessRow][guessColumn] == shipValues){
Was This Post Helpful? 0
  • +
  • -

#11 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 174
  • Joined: 01-March 11

Re: Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 08:28 PM

I see what you're telling me is wrong, but I'm not quite exactly sure how to fix it since I'm not sure if I'm assigning the random location correctly.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 08:45 PM

The int[][] values just stores the display. That is it. When a ship is hit, update the corresponding position in values.

I'm not exactly sure why the enemy's array is of random length and width. I get that you want to randomize the ships in the enemy's grid, but it should still be of the same dimmensions as the board.

Quote

I then have to randomly assign a 2x2 enemy ship without displaying it on the board (right now I'm using a second array to hold the location, unless there is an easier way)

After reading this from your OP, I would suggest designing a Ship class to encapsulate the top-left coordinate of the ship, as well as the numRows and numCols it occupies. If you continue to expand on this project, a Ship class will make your life a whole lot easier when managing multiple Ships.

Basically, this is the basis of what I would do:
class Ship{
    private int x, y; //the (x,y) coordinate for the top-left point of the ship
    private int width, height; //the dimmensions of the ship

    public Ship(){
       x = y = 0;
       width = height = 1;
    }

    public Ship(int x, int y, int width, int height){
       this.x = x;
       this.y = y;
       this.width = width;
       this.height = height;
    }
}



Now all you have to do is check to see if the selected coordinate falls within the bounds of the ship.

If you don't like the class design option, then I would just store the x and y coordinate of the top-left corner of the enemy ship. You know it's a 2x2, so no need for the additional int[][] shipValues.

Hope this helps some. :)
Was This Post Helpful? 0
  • +
  • -

#13 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 174
  • Joined: 01-March 11

Re: Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 08:51 PM

i think i will pass on the class option, but thank you though, but i'm not quite understanding the storing of the x & y & how it's randomized?
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 08:59 PM

Let's examine a 5x5 grid. You want to store a 2x2 ship within this 5x5 grid. Since I'm suggesting storing the top-left coordinate only, the X cells represent valid locations. If you try to store the top-left coordinate in any of the spaces with a - sign, you will go outside of the grid. Do you see why?
|X|X|X|X|-|
|X|X|X|X|-|
|X|X|X|X|-|
|X|X|X|X|-|
|-|-|-|-|-|



So to store the random coordinate, both the x and y coordinates must be between 0 and 3 inclusive. As an example, if my point is (2,3), then my ship takes the spots (2,3)(2,4)(3,3)(3,4) b/c it is a 2x2.

Does this help some?
Was This Post Helpful? 0
  • +
  • -

#15 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 174
  • Joined: 01-March 11

Re: Battleship 2D Array; problem wi/ int & char

Posted 03 April 2011 - 09:04 PM

i kind of see it, but i have no idea on how to come about in coding and storing. like how do i even randomize the ship?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3