hi i started coding battle ship when i was done i noticed that the user and computer always misses, even if they hit, i dont know what i did wrong

import java.util.Scanner;
public class Battleship {
	public final static String USER = "user";

	public final static String COMPUTER ="computer";

	public final static char HITVAR = 'H';
	public final static char MISS= 'M';
	public final static int NORTH = 1;
	public final static int SOUTH = 2;
	public final static int EAST = 3;
	public final static int WEST = 4;
	public final static int AIRCRAFT_CARRIER_SIZE = 5;
	public final static int BATLESHIP_SIZE = 4;
	public final static int DESTROYER_SHIP_SIZE = 3;
	public final static int SUBMARINE_SIZE = 3;
	public final static int PATROL_BOAT_SIZE = 2;
	public final static char AIRCRAFT ='S';
	public final static char BATTLESHIP ='S';
	public final static char DESTROYER = 'S';
	public final static char SUBMARINE= 'S';
	public final static char PATROL_BOAT= 'S';
	public final static int SIZE =10;

	final static char[][] USERGRID = new char[SIZE][SIZE];
	final static char[][] COMPUTERGRID = new char[SIZE][SIZE];

	public static void main(String wowYouCanEditThis[]){
		Scanner input = new Scanner(System.in);

		setComputerGrid();



		setShip(COMPUTERGRID, AIRCRAFT_CARRIER_SIZE, AIRCRAFT, 0,0, 2 );

		

		setShip(COMPUTERGRID, BATLESHIP_SIZE, BATTLESHIP, 0,2, 2);


		setShip(COMPUTERGRID, DESTROYER_SHIP_SIZE, DESTROYER, 0,9, 2 );


		setShip(COMPUTERGRID, SUBMARINE_SIZE, SUBMARINE, 4,6, 2);

		for(int i =0; i<2;i++){

			setShip(COMPUTERGRID, PATROL_BOAT_SIZE, PATROL_BOAT, 0,7, 2 );
		}



		setUserShip();
		for(int i=0;i<100;i++){
			setUserPlay();
			printComputerGrid();
			setComputerPlay();
			printUserGrid();
			
		}
		


	}
	public static void setUserGrid(){
		for( int row = 0; row< SIZE;row++){
			for(int column = 0; column<SIZE; column++){
				USERGRID[row][column]= ' ';
			}
		}

	}
	public static void setComputerGrid(){
		for( int row = 0; row< SIZE;row++){
			for(int column = 0; column<SIZE; column++){
				COMPUTERGRID[row][column]= ' ';
			}
		}

	}
	public static void printUserGrid(){
		System.out.println("PLAYERS ONE GRID");
		System.out.println("  0   1   2   3   4   5   6   7   8   9");
		System.out.println("-----------------------------------------");
		for(int row = 0; row< SIZE; row++){

			for(int column = 0; column<SIZE; column++){
				System.out.print("| " + USERGRID[row][column] + " ");

			}
			System.out.println("|");
			System.out.println("-----------------------------------------");
		}

	}
	public static void printComputerGrid(){

		System.out.println("COMPUTER GRID");
		System.out.println("   1   2   3   4   5   6   7   8   9   10");
		System.out.println("-----------------------------------------");
		for(int row = 0; row< SIZE; row++){
			for(int column = 0; column<SIZE; column++){
				System.out.print("| " + COMPUTERGRID[row][column] + " ");

			}
			System.out.println("|");
			System.out.println("-----------------------------------------");
		}


	}
	public static void setUserPlay(){
		//how the user will pick rows and columns of the COMPUTERGRID
		Scanner input = new Scanner(System.in);
		System.out.print("please choose a row and a colum of in the computers grid, where you wanna hit");
		int rowChoice = input.nextInt();
		int columnChoice = input.nextInt();
		setComputerGrid();
		
		while(columnChoice<0&&columnChoice>10&&rowChoice<0 & rowChoice>10){
			System.out.print("outta range enter tow and colomn agian");
			rowChoice = input.nextInt();
			columnChoice = input.nextInt();
			printComputerGrid();
		}
		for(int row = 0; row<10;row++){
			if(didUserHit(rowChoice,columnChoice)==false){
				COMPUTERGRID[rowChoice][columnChoice]= MISS;
			}
			else {
				COMPUTERGRID[rowChoice][columnChoice]= HITVAR;
			}
		}
		printComputerGrid();
	}
	public static void setComputerPlay(){
		//how the computer will random generate numbers of colums to USERGRID
		int computerRow = (int)(Math.random()*10);
		int computerColumn = (int)(Math.random()*10);
		while(computerColumn <0&&computerColumn>10&& computerRow <0 && computerRow>10){
			computerRow = (int)(Math.random()*10);
			computerColumn = (int)(Math.random()*10);
		}
		for (int row = 0; row<10; row++){
			if(didCompHit(computerRow,computerColumn)==false){
				USERGRID[computerRow][computerColumn]= MISS;
			}
			else {
				USERGRID[computerRow][computerColumn]= HITVAR;
			}

		}



	}
	public static boolean hasWon(String player){
		//temporary
		return false;

	}
	public static void setUserShip(){
		//sets the userShip in USERGRID
		char airCraftCarrier = 'S';
		char battleShip = 'S';
		char destroyer = 'S';
		char submarine= 'S';
		char patrolBoat= 'S';
		setUserGrid();
		printUserGrid();
		Scanner input = new Scanner(System.in);

		//aircraft carrier
		System.out.println("which row and column u want to put the arircraft carrier");
		int bigShipRow = input.nextInt();
		int bigShipColumn = input.nextInt();
		while(bigShipColumn<0&&bigShipColumn>10&&bigShipRow<0 & bigShipRow>10){
			System.out.print("outta range for air craft carrier please enter the info again");
			bigShipRow = input.nextInt();
			bigShipColumn = input.nextInt();

		}
		System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for west ");
		int direction = input.nextInt();
		System.out.println("Direction is " + direction);
		while(direction>4){
			System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for west ");
			direction = input.nextInt();

		}
		if(direction ==1){
			for(int i =0; i<5; i++){
				USERGRID[bigShipRow-i][bigShipColumn] = airCraftCarrier;
			}


		}
		else if(direction == 2){
			for(int i =0; i<5; i++){
				USERGRID[bigShipRow+i][bigShipColumn] = airCraftCarrier;
			}
		}
		else if(direction == 3){
			for(int i =0; i<5; i++){
				USERGRID[bigShipRow][bigShipColumn+i] = airCraftCarrier;
			}

		}
		else {
			for(int i =0; i<5; i++){
				USERGRID[bigShipRow][bigShipColumn-i] = airCraftCarrier;
			}

		}
		printUserGrid();

		//battleship
		System.out.println("which row and column u want to put the battleship at ");
		int BShipRow = input.nextInt();
		int BShipColumn = input.nextInt();
		while(BShipColumn<0&&BShipColumn>10&&BShipRow<0 & BShipRow>10){
			System.out.print("outta range for battle ship please enter the info again");
			BShipRow = input.nextInt();
			BShipColumn = input.nextInt();

		}
		System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for west ");
		direction = input.nextInt();
		while(direction>4){
			System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for westt ");
			direction = input.nextInt();


		}
		if(direction == 1){
			for(int i =0; i<4; i++){
				USERGRID[BShipRow-i][BShipColumn] = battleShip;
			}


		}
		else if(direction == 2){
			for(int i =0; i<4; i++){
				USERGRID[BShipRow+i][BShipColumn] = battleShip;
			}
		}
		else if(direction == 3){
			for(int i =0; i<4; i++){
				USERGRID[BShipRow][BShipColumn+i] = battleShip;
			}

		}
		else {
			for(int i =0; i<4; i++){
				USERGRID[BShipRow][BShipColumn-i] = battleShip;
			}

		}
		printUserGrid();

		//destroyer
		System.out.println("which row and column u want to put the destroyer at ");
		int destroyerShipRow = input.nextInt();
		int destroyerShipColumn = input.nextInt();
		while(destroyerShipColumn<0&&destroyerShipColumn>10&&destroyerShipRow<0 & destroyerShipRow>10){
			System.out.print("outta range for destroyer please enter the info again");
			destroyerShipRow = input.nextInt();
			destroyerShipColumn = input.nextInt();

		}
		System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for west ");
		direction = input.nextInt();
		while(direction>4){
			System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for west ");
			direction = input.nextInt();


		}
		if(direction == 1){
			for(int i =0; i<3; i++){
				USERGRID[destroyerShipRow-i][destroyerShipColumn] = destroyer;
			}

		}
		else if(direction == 2){
			for(int i =0; i<3; i++){
				USERGRID[destroyerShipRow+i][destroyerShipColumn] = destroyer;
			}

		}
		else if(direction == 3){
			for(int i =0; i<3; i++){
				USERGRID[destroyerShipRow][destroyerShipColumn+i] = destroyer;
			}

		}
		else {
			for(int i =0; i<3; i++){
				USERGRID[destroyerShipRow][destroyerShipColumn-i] = destroyer;
			}

		}
		printUserGrid();
		//submarine
		System.out.println("which row and column u want to put the submarine at ");
		int subRow = input.nextInt();
		int subColumn = input.nextInt();
		while(subColumn<0&&subColumn>10&&subRow<0 & subRow>10){
			System.out.print("outta range for submarine please enter the info again");
			subRow = input.nextInt();
			subColumn = input.nextInt();

		}
		System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for west ");
		direction = input.nextInt();
		while(direction>4){
			System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for west ");
			direction = input.nextInt();


		}
		if(direction == 1){
			for(int i =0; i<3; i++){
				USERGRID[subRow-i][subColumn] = submarine;
			}

		}
		else if(direction == 2){
			for(int i =0; i<3; i++){
				USERGRID[subRow+i][subColumn] = submarine;
			}

		}
		else if(direction ==3){
			for(int i =0; i<3; i++){
				USERGRID[subRow][subColumn+i] = submarine;
			}

		}
		else {
			for(int i =0; i<3; i++){
				USERGRID[subRow+i][subColumn-i] = submarine;
			}

		}
		printUserGrid();

		//patrol boat
		System.out.println("which row and column u want to put the patrol boat at ");
		int pBoatRow = input.nextInt();
		int pBoatColumn = input.nextInt();
		while(pBoatColumn<0&&pBoatColumn>10&&pBoatRow<0 & pBoatRow>10){
			System.out.print("outta range for patrol boat please enter the info again");
			pBoatRow = input.nextInt();
			pBoatColumn = input.nextInt();

		}
		System.out.println("which direction do you want your ship enter north south east west ");
		direction = input.nextInt();
		while(direction>4||direction<0){
			System.out.println("which direction do you want your ship enter north south east west ");
			direction = input.nextInt();


		}
		if(direction == 1){
			for(int i =0; i<2; i++){
				USERGRID[pBoatRow-i][pBoatColumn] = patrolBoat;
			}


		}
		else if(direction == 2){
			for(int i =0; i<2; i++){
				USERGRID[pBoatRow+i][pBoatColumn] = patrolBoat;
			}

		}
		else if(direction == 3){
			for(int i =0; i<2; i++){
				USERGRID[pBoatRow][pBoatColumn+i] = patrolBoat;
			}

		}
		else {
			for(int i =0; i<2; i++){
				USERGRID[pBoatRow][pBoatColumn-i] = patrolBoat;
			}

		}
		printUserGrid();

	}
	public static boolean setShip(char[][] grid, int shipSize, char shipChar, int shipRow, int shipColumn, int direction){
		
		if (direction == NORTH){
			//north
			if (shipRow - shipSize <0){
				return false;
			}
			
			for(int i = 0; i< shipSize; i++){
				if (grid[shipRow-i][shipColumn-1] != ' '){
					return false;
				}
			}
			
			for(int i=0; i<shipSize;i++){
				//while(computerAirRow +i <10 || computerAirRow -1>10|| computerAirColumn)
				grid[shipRow-i][shipColumn] = shipChar;
				

			}

		}
		else if(direction == SOUTH){
			if (shipRow + shipSize >9){
				return false;
			}
			
			for(int i = 0; i< shipSize; i++){
				if (grid[shipRow+i][shipColumn] != ' '){
					return false;
				}
			}
			for(int i=0; i<shipSize;i++){
				//while(computerAirRow +i <10 || computerAirRow -1>10|| computerAirColumn)
				COMPUTERGRID[shipRow+i][shipColumn] = shipChar;

			}

		}
		else if(direction == EAST){
			if (shipColumn + shipSize >9){
				return false;
			}
			
			for(int i = 0; i< shipSize; i++){
				if (grid[shipRow][shipColumn+i] != ' '){
					return false;
				}
			}
			for(int i=0; i<shipSize;i++){
				//while(computerAirRow +i <10 || computerAirRow -1>10|| computerAirColumn)
				COMPUTERGRID[shipRow][shipColumn-1] = shipChar;

			}

		}
		else{
			if (shipRow- shipSize <0){
				return false;
			}
			
			for(int i = 0; i< shipSize; i++){
				if (grid[shipRow][shipColumn-1] != ' '){
					return false;
				}
			}
			for(int i=0; i<shipSize;i++){
				COMPUTERGRID[shipRow][shipColumn-1] = shipChar;
			}
			
		}
		return true;

	}

	public static void gameStart(){
		//starts the game a void method

	}
[B]	public static boolean didUserHit(int row, int column){
		if(COMPUTERGRID[row][column] == 'S'){
			return true;
		}

		return false;
	}
	public static boolean didCompHit(int row, int column){
		if(USERGRID[row][column] == 'S'){
			return true;
		}

		return false;
	}
[/B]
	public static boolean hasWon(){
		//temporary
		return false;
		
	}


}

the bold is the part where i have that method

and yes this is not gui my teacher told me to this way and im not going to make seprate classes since im almost done with the code

Please use normal sentences, and capital letters.

Do you have Eclipse as your IDE? Even when you don't, you should debug. Make a breakpoint at the method you want to analyze, and check all your variables when the program is running at that point.
Most likely some method will show something wrong.

Also, please post the relevant parts of your code, not all of it.

When you found errors, ask here, and I (or someone else) will try to answer you.

public static boolean didUserHit(int row, int column){
		if(COMPUTERGRID[row][column] == 'S'){
			return true;
		}

		return false;
	}
	public static boolean didCompHit(int row, int column){
		if(USERGRID[row][column] == 'S'){
			return true;
		}

		return false;
	}

this part is not working for some reason

yes i tried i dont know how to debug properly

What program are you using to program in Java (this is called your IDE, Integrated development environment).
In most programs, you have to right-click in the bar with all the line numbers, and select: 'add breakpoint'. When you build and run your program, the program will run until it encounters this line of code where you put your breakpoint. It will stop there, and when it has stopped you can hold your mouse over any variables in your code, and read their current values.
Try it, you will understand it in no time.

Is there any reason you are making all your variables and methods static?
It seems a little strange to me.

What exactly does not work in the methods you mentioned? Do they return a wrong value, does the program crash, are there any errors?

What do you mean by "not working"? I tried your code and it returned false and added "M" to the grid when it is missed. Though, I only tried once. There are reasons I don't try again because...

1)I have to redo the whole set up again if the program throws an exception.
2)Using nextInt() from Scanner class could cause problem later if a user enters something else which is not an integer value. This will cause InputMismatchException thrown by the Scanner class.
3)The ArrayOutOfBoundException occurs all over the place because values are not validated before being used.
- Set up ship could throw the exception because a user does NOT know how big a ship is. For example, a user may attempt to place a ship at the top left and arrange it to "north" direction.
- The direction value check when set up is default to 4 which is not supposed to. Instead, you should keep looping for a valid value is entered.
- The grid value when set up is not being checked at all.
- You incorrectly check for grid value entered by a user from 0~10 which is out of bound when the game starts. It should be 0~9.
That's all for now... Need them to be fixed before I look through it further.

PS: If you do not know how to use a tool's debugger, use "System.out.println()" to display any values in question. That's the primitive debugging technique that everyone should know how to use it...

commented: nice +2

it only hits a M even if it hits thats the problem of the code
and yes i have ecclpse ok ill fix those but i just need the hitting part to work

for (int row = 0; row<10; row++){
			if(didCompHit(computerRow,computerColumn)==false){
				USERGRID[computerRow][computerColumn]= MISS;
			}
			else {
				USERGRID[computerRow][computerColumn]= HITVAR;
			}

		}

and im a begginer i dont no exception yet also that might be the reason i am using static i think

ship sizes
air craft carrier = 5
batttleship =4
destroyer =3
submarine =3
patrol boat =2

It's quite common for a beginner to put everything static - it's because they start coding in the public static void main(String[] args) method, and immediately discover that they get compiler errors if the members they reference are not also static.
More experienced Java developers tend to have a main that just instantiates a small number (eg 1) of important objects, and hands over control to a method in one of those.

I think you don't understand the concept of static, and exceptions. Those things are VERY different.

Static:
When you make a method or variable static, you call it not using a variable, but using the class it is part of.
In your case: You did not make any instance of your main class, and you just call your methods from that class.
My example:

class Test {
	
	public static void main(String[] args) {
		new Test ();
	}
	
	Test () {
		B.sayHiFromB ();
		A objectA = new A();
		objectA.sayHiFromA ();
	}
	
	private class A {
		public void sayHiFromA () {
			System.out.println ("Hi from A");
		}
	}
	private static class B {
		public static void sayHiFromB () {
			System.out.println ("Hi from B");
		}
	}
}

will output:

Hi from B
Hi from A

As you can see, I have to make an object from the A class to call the method. I don't have to make a B object, I can call it straight from the B class.

Exceptions:
An Exception is an error. Certain methods throw Exceptions, this means that an error can exsist in such a method. You should use a try/catch block surrounding the method call, to catch it:

class Test {
	
	public static void main(String[] args) {
		new Test ();
	}
	
	Test () {
		A objectA = new A();
		try {
			objectA.sayHiFromA ();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println ("Program exits.");
	}
	
	private class A {
		public void sayHiFromA () throws Exception{
			System.out.println ("Hi from A");
			throw new Exception("This gives an error, and you should catch it!");
		}
	}
}

As you can see, I have to surround the method sayHiFromA with a try/catch block. This block catches the error, and prints the error. The rest of the program still runs, and so the output is:

Hi from A
java.lang.Exception: This gives an error, and you should catch it!
at Test$A.sayHiFromA(Test.java:21)
at Test.<init>(Test.java:11)
at Test.main(Test.java:5)
Program exits.

If I would NOT have made this try/catch block, and the error would have been thrown, the entire program would crash, and the last line (Program exits.) would not have displayed.

If you do not understand it, please ask!

hmm yeah i kinda get what those both mean but in my battleship program when ever i hit a ship it says its a miss same for the computer hitting my ship

OK, good. Now, when I try your program: you have no handling for all the errors that come up when I place ships in impossible positions. Do that first, think of every possible error that can pop up, and then go on to registering the hits of the computer and the human.

...If I would NOT have made this try/catch block, and the error would have been thrown, the entire program would crash ...

Fortunately it's not quite that bad. The compiler checks that you have code to handle such Exceptions (either by try/catch or by passing it up to the calling method) and will give you a compile error if you haven't done enough. The error tells you which Exception you have to deal with, and where, so it's not too hard to fix. So the compiler won't let you continue with a program that can crash like that.

ps: yes, expert reader, I know about unchecked exceptions, but IMHO this is not a good time to confuse the issue.

I know it will not compile, but as an example, what would happen IF you wouldnt handle any exceptions.

I hope it is a little more clear.

I look at your code again. Yes, it compiles. Though, there are bugs that could easily throw an Exception (error) while running because either there is no validation or validate it incorrectly (referred to my previous post). Also, there are so many hard-coded of the code. As a result, the code is very lengthy and redundant.

I guess before I am talking about coding, maybe I should show you what the flow chart should be first... So it would be easier to talk about steps.

/*
  .-------------.
  | Start Game  |
  '-------------'
         |
         V
  +---------------+
  || Comp set up ||
  +---------------+
         |
         V
  +---------------+
  || User set up ||
  +---------------+
         |<--------------------------------------------------------+
         V                                                         |
  +----------------+                                               |
  || User selects ||                                               |
  ||   a target   ||                                               |
  +----------------+                                               |
         |                                                         |
         V                                                         |
  +----------------+                                               |
  || Display Grid ||                                               |
  +----------------+                                               |
         |                                                         |
         V                     .                                   |
        ' '                   ' '                                  |
      /     \               /     \             .-------------.    |
     . Hit?  . ---yes--->  .  Win? . ---yes---> | Player Win  |    |
      \     /               \     /             '-------------'    |
        ' '                   ' '                                  |
         | no                  | no                                |
         V                     |                                   |
  +----------------+           |                                   |
  || Comp selects ||<----------+                                   |
  ||   a target   ||                                               |
  +----------------+                                               |
         |                                                         |
         V                                                         |
  +----------------+                                               |
  || Display Grid ||                                               |
  +----------------+                                               |
         |                                                         |
         V                     .                                   |
        ' '                   ' '                                  |
      /     \               /     \             .---------------.  |
     . Hit?  . ---yes--->  .  Win? . ---yes---> | Computer Win  |  |
      \     /               \     /             '---------------'  |
        ' '                   ' '                                  |
         | no                  | no                                |
         |_____________________|___________________________________|
*/

You did OK for the computer and user set up even though the portion is more hard-coded. You can refine it later.

There are major problems in your code.

First, you are clearing the computer set up every time the user is playing. Get rid of the line where you call setCompGrid() in the setUserPlay() method.

int rowChoice = input.nextInt();
int columnChoice = input.nextInt();
//setComputerGrid();  <-- delete this line

The second major problem is to display computer grid correctly. Because you need to hide the computer ship, you must modify the display to display if the value inside the array is not equal to 'S'.

public static void printComputerGrid() {
    System.out.println("COMPUTER GRID");
    System.out.println("   0   1   2   3   4   5   6   7   8   9");  // look! should be 0~9 too
    System.out.println("-----------------------------------------");
    for(int row = 0; row< SIZE; row++) {
      for(int column = 0; column<SIZE; column++) {
        System.out.print("| ");
        // here, hide the computer ship location display
        if (COMPUTERGRID[row][column]=='S') {
          System.out.print("  ");
        }
        // if not a ship location, display whatever inside it
        else {
          System.out.print(COMPUTERGRID[row][column] + " ");
        }
      }
      System.out.println("| "+row);  // look! I added the row number for viewer
      System.out.println("-----------------------------------------");
    }
  }

I did not improve your coding style but rather modify it to correct the bug. There are ways to improve it. Anyway, that's what you need to learn.

The last major problem of your bug is in your setUserPlay() and setCompPlay() methods. After the program accepts a selected grid value from either a player or computer, it should use the value immediately, no loop! Also, look at how I fixed your ArrayOutOfBoundException.

// look! I changed the value of your condition checking to be 0~9
while(columnChoice<0 && columnChoice>9 && rowChoice<0 && rowChoice>9) {
  ...
}
// no loop!
//for(int row = 0; row<10;row++){
if(didUserHit(rowChoice,columnChoice)==false) {
  COMPUTERGRID[rowChoice][columnChoice]= MISS;
}
else {
  COMPUTERGRID[rowChoice][columnChoice]= HITVAR;
}
//}
printComputerGrid();

The above is setUserPlay(). You need to take out the for-loop from the setCompPlay() as well. Because you have this loop, it screws up your didUserHit() and didCompHit() methods.

Your next goal from now is to validate all input values before you use them. You need to come up with a way to do that; otherwise, your program will crash whenever a user enters an invalid value. That's what you need to find out by yourself before I can help you.

commented: Nice flowchar :) +3

Did you construct that flowchart diagram the hard way, or do you have a tool to help? I'm no fan of flowcharts in general, but sometimes they are very useful to explain a logic flow problem/solution, as in your example.

I used text editor to do it. :)

Wow. That's dedication. ;-)

Thanks :)

thank you guuys i still havent made the user boundaries but this is what i have so far

import java.util.Scanner;
public class Battleship {
	public final static String USER = "user";

	public final static String COMPUTER ="computer";

	public final static char HITVAR = 'H';
	public final static char MISS= 'M';
	public final static int NORTH = 1;
	public final static int SOUTH = 2;
	public final static int EAST = 3;
	public final static int WEST = 4;
	public final static int AIRCRAFT_CARRIER_SIZE = 5;
	public final static int BATLESHIP_SIZE = 4;
	public final static int DESTROYER_SHIP_SIZE = 3;
	public final static int SUBMARINE_SIZE = 3;
	public final static int PATROL_BOAT_SIZE = 2;
	public final static char AIRCRAFT ='1';
	public final static char BATTLESHIP ='1';
	public final static char DESTROYER = '1';
	public final static char SUBMARINE= '1';
	public final static char PATROL_BOAT= '1';
	public final static int SIZE =10;

	final static char[][] USERGRID = new char[SIZE][SIZE];
	final static char[][] COMPUTERGRID = new char[SIZE][SIZE];

	public static void main(String wowYouCanEditThis[]){
		setComputerShips();
		printRules();
		setComputerShips();
		setUserShip();
		
		for(int i=0;i<100;i++){
			setUserPlay();
			printComputerGrid();
			setComputerPlay();
			printUserGrid();
			
		}
		


	}
	public static void printRules(){
		System.out.println("**********************************************************************************************************************************************");
		System.out.println("Welcome to Harshal's Battleship Game here are the rules");
		System.out.println("There are 5 ships on your board and the opponents board your main objective is to sink all the opponents ");
		System.out.println("ships before the opponents sinks your ships the rows and columns are numbered from 0-9  and there are four directions");
		System.out.println("NORTH -> 1");
		System.out.println("SOUTH -> 2");
		System.out.println("EAST ->  3");
		System.out.println("WEST ->  4");
		System.out.println("so there are five ships those are Air Craft Carrier, Battleship, Destroyer, Submarine and Patrol Boat, and u cant intresect or go out of bounds");
		System.out.println("aircraft carrier size = 5 ---> *****");
		System.out.println("BattleShip size -> 4 ---> ****");
		System.out.println("Destroyer size -> 3 ---> ***");
		System.out.println("Submarine size -> 3 ---> ***");
		System.out.println("Patrol boat size -> 2 ---> **");
		System.out.println("");
		System.out.println("************************************************************************************************************************************************");
		
		
	}
	public static void setUserGrid(){
		for( int row = 0; row< SIZE;row++){
			for(int column = 0; column<SIZE; column++){
				USERGRID[row][column]= '~';
			}
		}

	}
	public static void setComputerGrid(){
		for( int row = 0; row< SIZE;row++){
			for(int column = 0; column<SIZE; column++){
				COMPUTERGRID[row][column]= 'l';
			}
		}

	}
	public static void printUserGrid(){
		System.out.println("PLAYERS ONE GRID");
		System.out.println("  0   1   2   3   4   5   6   7   8   9");
		System.out.println("-----------------------------------------");
		for(int row = 0; row< SIZE; row++){

			for(int column = 0; column<SIZE; column++){
				System.out.print("| " + USERGRID[row][column] + " ");

			}
			System.out.println("|");
			System.out.println("-----------------------------------------");
		}

	}
	public static void printComputerGrid(){

		System.out.println("COMPUTER GRID");
		System.out.println("  0   1   2   3   4   5   6   7   8   9");
		System.out.println("-----------------------------------------");
		for(int row = 0; row< SIZE; row++){
			for(int column = 0; column<SIZE; column++){
				System.out.print("| " + COMPUTERGRID[row][column] + " ");

			}
			System.out.println("|");
			System.out.println("-----------------------------------------");
		}


	}
	public static void setUserPlay(){
		int count = 0;
		//how the user will pick rows and columns of the COMPUTERGRID
		Scanner input = new Scanner(System.in);
		System.out.print("please choose a row and a colum of in the computers grid, where you wanna hit");
		int rowChoice = input.nextInt();
		int columnChoice = input.nextInt();
		
		while(columnChoice<0&&columnChoice>10&&rowChoice<0 & rowChoice>10){
			System.out.print("outta range enter tow and colomn agian");
			rowChoice = input.nextInt();
			columnChoice = input.nextInt();
		}
			if(didUserHit(rowChoice,columnChoice)==false){
				COMPUTERGRID[rowChoice][columnChoice]= MISS;
				System.out.println("SYSTEM: TARGET MISSED");
			}	
			else {
				COMPUTERGRID[rowChoice][columnChoice]= HITVAR;
				System.out.println("SYSTEM: TARGET AQUIRED");
				count++;
				System.out.println(count);
			}
		
	}
	public static void setComputerPlay(){
		int count = 0;
	
		//how the computer will random generate numbers of colums to USERGRID
		int computerRow = (int)(Math.random()*10);
		int computerColumn = (int)(Math.random()*10);
		while(computerColumn <0&&computerColumn>10&& computerRow <0 && computerRow>10){
			computerRow = (int)(Math.random()*10);
			computerColumn = (int)(Math.random()*10);
		}
	
			if(didCompHit(computerRow,computerColumn)==false){
				USERGRID[computerRow][computerColumn]= MISS;
				System.out.println("SYSTEM: COMPUTER HAS MISSED");
			}
			else {
				USERGRID[computerRow][computerColumn]= HITVAR;
				count++;
				System.out.println("SYSTEM: THE COMPUTER HAS HIT YOUR SHIP");
				System.out.println(count);
			}

		


	}
	public static boolean hasWon(String player){
		//temporary
		return false;

	}
	public static void setUserShip(){
		//sets the userShip in USERGRID
		char airCraftCarrier = 'S';
		char battleShip = 'S';
		char destroyer = 'S';
		char submarine= 'S';
		char patrolBoat= 'S';
		setUserGrid();
		printUserGrid();
		Scanner input = new Scanner(System.in);

		//aircraft carrier
		System.out.println("which row and column u want to put the arircraft carrier its 5 spaces so dont go out of bounds or intersect");
		int bigShipRow = input.nextInt();
		int bigShipColumn = input.nextInt();
		while(bigShipColumn<0&&bigShipColumn>10&&bigShipRow<0 & bigShipRow>10){
			System.out.print("outta range for air craft carrier please enter the info again");
			bigShipRow = input.nextInt();
			bigShipColumn = input.nextInt();

		}
		System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for west ");
		int direction = input.nextInt();
		System.out.println("Direction is " + direction);
		while(direction>4){
			System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for west ");
			direction = input.nextInt();

		}
		if(direction ==1){
			for(int i =0; i<5; i++){
				USERGRID[bigShipRow-i][bigShipColumn] = airCraftCarrier;
			}


		}
		else if(direction == 2){
			for(int i =0; i<5; i++){
				USERGRID[bigShipRow+i][bigShipColumn] = airCraftCarrier;
			}
		}
		else if(direction == 3){
			for(int i =0; i<5; i++){
				USERGRID[bigShipRow][bigShipColumn+i] = airCraftCarrier;
			}

		}
		else {
			for(int i =0; i<5; i++){
				USERGRID[bigShipRow][bigShipColumn-i] = airCraftCarrier;
			}

		}
		printUserGrid();

		//battleship
		System.out.println("which row and column u want to put the battleship at its 4 spaces so dont go out of bounds ");
		int BShipRow = input.nextInt();
		int BShipColumn = input.nextInt();
		while(BShipColumn<0&&BShipColumn>10&&BShipRow<0 & BShipRow>10){
			System.out.print("outta range for battle ship please enter the info again");
			BShipRow = input.nextInt();
			BShipColumn = input.nextInt();

		}
		System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for west ");
		direction = input.nextInt();
		while(direction>4){
			System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for westt ");
			direction = input.nextInt();


		}
		if(direction == 1){
			for(int i =0; i<4; i++){
				USERGRID[BShipRow-i][BShipColumn] = battleShip;
			}


		}
		else if(direction == 2){
			for(int i =0; i<4; i++){
				USERGRID[BShipRow+i][BShipColumn] = battleShip;
			}
		}
		else if(direction == 3){
			for(int i =0; i<4; i++){
				USERGRID[BShipRow][BShipColumn+i] = battleShip;
			}

		}
		else {
			for(int i =0; i<4; i++){
				USERGRID[BShipRow][BShipColumn-i] = battleShip;
			}

		}
		printUserGrid();

		//destroyer
		System.out.println("which row and column u want to put the destroyer at destroyer is 3 spaces ");
		int destroyerShipRow = input.nextInt();
		int destroyerShipColumn = input.nextInt();
		while(destroyerShipColumn<0&&destroyerShipColumn>10&&destroyerShipRow<0 & destroyerShipRow>10){
			System.out.print("outta range for destroyer please enter the info again");
			destroyerShipRow = input.nextInt();
			destroyerShipColumn = input.nextInt();

		}
		System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for west ");
		direction = input.nextInt();
		while(direction>4){
			System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for west ");
			direction = input.nextInt();


		}
		if(direction == 1){
			for(int i =0; i<3; i++){
				USERGRID[destroyerShipRow-i][destroyerShipColumn] = destroyer;
			}

		}
		else if(direction == 2){
			for(int i =0; i<3; i++){
				USERGRID[destroyerShipRow+i][destroyerShipColumn] = destroyer;
			}

		}
		else if(direction == 3){
			for(int i =0; i<3; i++){
				USERGRID[destroyerShipRow][destroyerShipColumn+i] = destroyer;
			}

		}
		else {
			for(int i =0; i<3; i++){
				USERGRID[destroyerShipRow][destroyerShipColumn-i] = destroyer;
			}

		}
		printUserGrid();
		//submarine
		System.out.println("which row and column u want to put the submarine at ");
		int subRow = input.nextInt();
		int subColumn = input.nextInt();
		while(subColumn<0&&subColumn>10&&subRow<0 & subRow>10){
			System.out.print("outta range for submarine please enter the info again sub is 2 spaces ");
			subRow = input.nextInt();
			subColumn = input.nextInt();

		}
		System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for west ");
		direction = input.nextInt();
		while(direction>4){
			System.out.println("which direction do you want your ship enter 1 for north 2 for south 3 for east 4 for west ");
			direction = input.nextInt();


		}
		if(direction == 1){
			for(int i =0; i<3; i++){
				USERGRID[subRow-i][subColumn] = submarine;
			}

		}
		else if(direction == 2){
			for(int i =0; i<3; i++){
				USERGRID[subRow+i][subColumn] = submarine;
			}

		}
		else if(direction ==3){
			for(int i =0; i<3; i++){
				USERGRID[subRow][subColumn+i] = submarine;
			}

		}
		else {
			for(int i =0; i<3; i++){
				USERGRID[subRow+i][subColumn-i] = submarine;
			}

		}
		printUserGrid();

		//patrol boat
		System.out.println("which row and column u want to put the patrol boat at 2 spaces for that");
		int pBoatRow = input.nextInt();
		int pBoatColumn = input.nextInt();
		while(pBoatColumn<0&&pBoatColumn>10&&pBoatRow<0 & pBoatRow>10){
			System.out.print("outta range for patrol boat please enter the info again ");
			pBoatRow = input.nextInt();
			pBoatColumn = input.nextInt();

		}
		System.out.println("which direction do you want your ship enter north south east west ");
		direction = input.nextInt();
		while(direction>4||direction<0){
			System.out.println("which direction do you want your ship enter north south east west ");
			direction = input.nextInt();


		}
		if(direction == 1){
			for(int i =0; i<2; i++){
				USERGRID[pBoatRow-i][pBoatColumn] = patrolBoat;
			}


		}
		else if(direction == 2){
			for(int i =0; i<2; i++){
				USERGRID[pBoatRow+i][pBoatColumn] = patrolBoat;
			}

		}
		else if(direction == 3){
			for(int i =0; i<2; i++){
				USERGRID[pBoatRow][pBoatColumn+i] = patrolBoat;
			}

		}
		else {
			for(int i =0; i<2; i++){
				USERGRID[pBoatRow][pBoatColumn-i] = patrolBoat;
			}

		}
		printUserGrid();

	}
	public static boolean setShip(char[][] grid, int shipSize, char shipChar, int shipRow, int shipColumn, int direction){
		
		if (direction == NORTH){
			//north
			if (shipRow - shipSize <0){
				
				return false;
			}
			
			for(int i = 0; i< shipSize; i++){
				if (grid[shipRow-i][shipColumn] != 'l'){
					return false;
				}
			}
			
			for(int i=0; i<shipSize;i++){
				//while(computerAirRow +i <10 || computerAirRow -1>10|| computerAirColumn)
				grid[shipRow-i][shipColumn] = shipChar;
				

			}

		}
		
		else if(direction == SOUTH){
			if (shipRow + shipSize >9){
				return false;
			}
			
			for(int i = 0; i< shipSize; i++){
				if (grid[shipRow+i][shipColumn] != 'l'){
					return false;
				}
			}
			for(int i=0; i<shipSize;i++){
				//while(computerAirRow +i <10 || computerAirRow -1>10|| computerAirColumn)
				COMPUTERGRID[shipRow+i][shipColumn] = shipChar;

			}

		}
		else if(direction == EAST){
			if (shipColumn + shipSize >9){
				return false;
			}
			
			for(int i = 0; i< shipSize; i++){
				if (grid[shipRow][shipColumn+i] != 'l'){
					return false;
				}
			}
			for(int i=0; i<shipSize;i++){
				//while(computerAirRow +i <10 || computerAirRow -1>10|| computerAirColumn)
				COMPUTERGRID[shipRow][shipColumn+i] = shipChar;

			}

		}
		else{
			if (shipColumn- shipSize <0){
				return false;
			}
			
			for(int i = 0; i< shipSize; i++){
				if (grid[shipRow][shipColumn-i] != 'l'){
					return false;
				}
			}
			for(int i=0; i<shipSize;i++){
				COMPUTERGRID[shipRow][shipColumn-i] = shipChar;
			}
			
		}
		return true;

	}
	public static void setComputerShips(){
		int row = (int)(Math.random()*10);
		int column = (int)(Math.random()*10);
		int direction = (int)(Math.random()*4 +1);
		setComputerGrid();
		
		while(setShip(COMPUTERGRID, AIRCRAFT_CARRIER_SIZE, AIRCRAFT, row,column, direction )==false){
			 row = (int)(Math.random()*10);
			 column = (int)(Math.random()*10);
			 direction = (int)(Math.random()*4 +1);
			
		
		}

		

		while(setShip(COMPUTERGRID, BATLESHIP_SIZE, BATTLESHIP, row,column, direction )== false){
			 row = (int)(Math.random()*10);
			 column = (int)(Math.random()*10);
			 direction = (int)(Math.random()*4 +1);
		}

		while(setShip(COMPUTERGRID, DESTROYER_SHIP_SIZE, DESTROYER, row,column, direction )==false){
			row = (int)(Math.random()*10);
			 column = (int)(Math.random()*10);
			 direction = (int)(Math.random()*4 +1);
		}


		while(setShip(COMPUTERGRID, SUBMARINE_SIZE, SUBMARINE, row,column, direction)== false){
			row = (int)(Math.random()*10);
			 column = (int)(Math.random()*10);
			 direction = (int)(Math.random()*4 +1);
		}



		while(setShip(COMPUTERGRID, PATROL_BOAT_SIZE, PATROL_BOAT, row,column, direction )==false){
			row = (int)(Math.random()*10);
			 column = (int)(Math.random()*10);
			 direction = (int)(Math.random()*4 +1);
			
		}
		
		
	}

	public static void gameStart(){
		//starts the game a void method

	}
	public static boolean didUserHit(int row, int column){
		if(COMPUTERGRID[row][column] == '1'){
			return true;
		}

		return false;
	}
	public static boolean didCompHit(int row, int column){
		if(USERGRID[row][column] == 'S'){
			return true;
		}

		return false;
	}
	public static boolean hasWon(int count){
		if (count ==17 ){
		return true;
		}
		else{
			return false;
		}
		
		
		
	}


}

wow i never saw Taywins code, i just saw it now THNKAS TAWIN OMG THAT HELPED A LOT THANKS!!!!!!!!!!!!!!!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.