mardi 2 mai 2017

Java battleship program not printing board correctly

I am having a problem with my java battleship program not printing the coordinates that the user enters. When the user guesses a spot on the board its supposed to update the space with an "X" if its a hit, if not then the board remains the same. But when a user guesses wrong on my program, it prints everything except the space where the user guessed. I believe that my else statement where the board updates is the problem, but any modification I did resulted in the board not printing anything.

import java.util.Random;
import java.util.Scanner;



class Battleship
{
     public static char randChar(){


       final String alphabet = "ABCDE";
       final int N = alphabet.length();

       char rand;


       Random r = new Random();


        rand = alphabet.charAt(r.nextInt(N));
         return rand;

    }


                public static void main (String[] args) throws java.lang.Exception
                {
                                char[] letters = {' ', 'A', 'B', 'C', 'D', 'E'};
                                int[] numbers ={1,2,3,4,5};
                                int[][] ships = new int[7][2];
                                char colGuess;
                                int rowGuess;
                                Boolean boardFlag=false;

                                Scanner scan = new Scanner(System.in);


                                //creates the board
                                for (int i = 0 ; i <= 5 ; i++){
                                  for (int j = 0 ; j <= 5 ; j++){
                                    if (i == 0) {
                                                 System.out.print(letters[j]+" ");
                                    } else if (j == 0){
                                      System.out.print(numbers[i-1] );
                                    } else {
                                                 System.out.print(letters[j]+""+numbers[i-1]);
                                }          
                                                System.out.print(" ");
                                }
                                System.out.println();
                                }

                                //assigns ships to random spots
                                assignShips(ships);

                                System.out.println("Enter your guess for the column:");
                                colGuess = scan.next().charAt(0);

                                //converts to uppercase
                                colGuess =  Character.toUpperCase(colGuess);



                                System.out.println("Enter your guess for the row:");
                                rowGuess = scan.nextInt();

                                //shows player what they entered
                                System.out.println("you entered: "+(char)colGuess+rowGuess);

                                //calls method to check for a hit
                                fire(colGuess,rowGuess,ships);
                                boardFlag=fire(colGuess,rowGuess,ships);
                                System.out.println(boardFlag);


                                //updates the board
                                for (int i = 0 ; i <= 5 ; i++){
                                  for (int j = 0 ; j <= 5 ; j++){
                                    if (i == 0) {
                                                 System.out.print(letters[j]+" ");
                                    } else if (j == 0){
                                      System.out.print(numbers[i-1] );
                                    } else {

                                      if(letters[j]==colGuess && numbers[i-1]==rowGuess){
                                        if(boardFlag==true){

                                           System.out.print(" "+"X");
                                        }



                                      }else{


                                        System.out.print(letters[j]+""+numbers[i-1]);

                                      }

                                }          
                                                System.out.print(" ");
                                }
                                System.out.println();
                                }







                }

                public static void assignShips(int[][] ships){
                 Random random = new Random();

                 for(int ship=0; ship<7;ship++){
                  ships[ship][0]=randChar();
                  ships[ship][1]=random.nextInt(5);

                  //gives location of ships, for testing purposes
                  System.out.print("Ship:" + (ship+1)+ " is located at"+(char)ships[ship][0]+ships[ship][1]+"\n");
                 }
                }

                //checks user input to see if we have a hit
                public static Boolean fire(char colGuess, int rowGuess,int[][] ships){
                  Boolean hitFlag=false;

                  for(int ship=0; ship<ships.length; ship++){
                    if(colGuess ==ships[ship][0] && rowGuess == ships[ship][1]){
                     hitFlag=true;
                    }
                  }

                  if(hitFlag==true){
                        System.out.println("we hit em at "+(char)colGuess+rowGuess+" chief!");





                   }else{ 

                     System.out.println("sorry chief we missed em");

                   }


                   return hitFlag;


                }
}

Aucun commentaire:

Enregistrer un commentaire