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