Results 1 to 7 of 7
  1. #1
    AaronHopkins is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Battleship 2D Array Program

    here is basically the instructions for the full program i have to write so that it is clear. up to this point, it seems to work except:

    1. when i run it, the ship should take up 4 values/spots on the board so in end, there should be 4 '*''s, but i run it fully through & notice only one spot is hit while the rest are 'x''s. am i checking correctly?
    2. when i check the input for the column letter, if i enter anything other than a letter, such as a number, it prints that it is an "invalid option", but nothing happens. the user should be able to guess again.
    3. lastly, i'm stumped in how to make sure that whatever the user inputs to fire, isn't a location that's already been chosen.



    i'm not completely done wi/ the code, but this what i have thus far:
    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    public class assignment17 {
    	static Scanner in = new Scanner(System.in);
    
    	public static void main(String[] args) {
    		int choice = 0;
    		board();
    		char[][] values = { { ' ', 'A', 'B', 'C', 'D', 'E' },
    				{ '1', '-', '-', '-', '-', '-' },
    				{ '2', '-', '-', '-', '-', '-' },
    				{ '3', '-', '-', '-', '-', '-' },
    				{ '4', '-', '-', '-', '-', '-' },
    				{ '5', '-', '-', '-', '-', '-' } };
    		do{
    			choice = menu();
    			switch(choice){
    				case 1:	System.out.println("Enter a Row Number: ");
    						int guessRow = rowCheck();
    						System.out.println("Enter a Column Letter: ");
    						String guessColumn = columnCheck1();
    						int colLocation = columnCheck(guessColumn);
    						int ROWS = 6;
    						int COLS = 6;
    						hitormiss(guessRow, colLocation, ROWS, COLS, values);
    						break;
    				case 2: 
    						break;
    				case 3:	
    						
    						break;
    				default:System.out.println("Invalid Input");
    						break;
    			}
    		}while(choice != 3);
    	}
    
    	// method to display the main menu
    	public static int menu() {
    		System.out.println("Menu:");
    		System.out.println("1. Fire Shot");
    		System.out.println("2. Show Solution");
    		System.out.println("3. Quit");
    		int menuOption = 0;
    		boolean test = true;
    		while (test) {
    			if (in.hasNextInt()) {
    				menuOption = in.nextInt();
    				test = false;
    			} else {
    				String junk = in.next();
    				System.out.println("Invalid Input");
    			}
    		}
    		return menuOption;
    	}
    
    	// method to display board
    	public static void board() {
    		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");
    		}
    	}
    
    	// method to check input for guessing the row
    	public static int rowCheck() {
    		int guessRow = 0;
    		boolean test = true;
    		while (test) {
    			if (in.hasNextInt()) {
    				guessRow = in.nextInt();
    				if (guessRow < 1 || guessRow > 5) {
    					System.out.println("Invalid Input - Retry");
    				} else if (guessRow >= 1 || guessRow < 6) {
    					test = false;
    				}
    			} else {
    				String dummy = in.next();
    				System.out.println("Invalid Input - Retry");
    			}
    
    		}
    		return guessRow;
    	}
    
    	// method to guess column
    	public static int locationCol() {
    		System.out.println("Enter a Column Letter: ");
    		in.nextLine();
    		String guessColumn = in.nextLine();
    		char c = guessColumn.charAt(0);
    		int colLocation = (c - 'A') + 1;
    		return colLocation;
    	}
    
    	public static String columnCheck1() {
    		in.nextLine();
    		String guessColumn="";
    		boolean test = true;
    		while (test) {
    			if (in.hasNextLine()) {
    				guessColumn = in.nextLine();
    					test = false;
    				}
    			 else {
    				int dummy = in.nextInt();
    				System.out.println("Invalid Input - Retry");
    			}
    
    		}
    		return guessColumn;
    	}
    	
    	
    	// method to check input for guessing the column
    	public static int columnCheck(String guessColumn) {
    		char c=' ';
    		do{
    				c = guessColumn.charAt(0);
    				switch (c) {
    				case 'A':
    					
    					break;
    				case 'B':
    					
    					break;
    				case 'C':
    					
    					break;
    				case 'D':
    					
    					break;
    				case 'E':
    					
    					break;
    				default:
    					System.out.println("Invalid Option");
    					
    				}
    				}while (c != 'A' && c != 'B' && c != 'C' && c != 'D' && c != 'E');
    			
    			int colLocation = (c - 'A') + 1;
    			return colLocation;
    		}
    	
    
    	// method to check if shot hit or miss
    	public static void hitormiss(int guessRow, int colLocation, int ROWS,
    			int COLS, char[][] values) {
    		Random rand = new Random();
    		int x = rand.nextInt(4); // goes from 0-3, excludes 4
    		int y = rand.nextInt(4);
    		if (guessRow == x && colLocation == y) {
    			values[guessRow][colLocation] = '*';
    			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 if (guessRow == x + 1 && colLocation == y + 1) {
    			values[guessRow][colLocation] = '*';
    			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 if (guessRow == x + 1 && colLocation == y) {
    			values[guessRow][colLocation] = '*';
    			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 if (guessRow == x && colLocation == y + 1) {
    			values[guessRow][colLocation] = '*';
    			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][colLocation] = '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");
    			}
    		}
    	}
    }

  2. #2
    AaronHopkins is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Just in case you can't see the instructions.

    http://i56.tinypic.com/98d0y0.jpg

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Your post basically boils down to "Here is my gazillion lines of code. It doesn't work. Fix it for me".

    Post only the relevant code. Ask a specific question.

  4. #4
    AaronHopkins is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    i'm sorry. when i prompt the user to input a column letter, i need to check their input for validity & to make sure it's a letter & if its not, tell them its invalid & prompt them to retry. this code works, but after it tells them that it is invalid, nothing else happens when in reality, they should be able to enter another input.
    Java Code:
    	public static String columnCheck1() {
    		in.nextLine();
    		String guessColumn="";
    		boolean test = true;
    		while (test) {
    			if (in.hasNextLine()) {
    				guessColumn = in.nextLine();
    					test = false;
    				}
    			 else {
    				int dummy = in.nextInt();
    				System.out.println("Invalid Input - Retry");
    			}
    
    		}
    		return guessColumn;
    	}
    lastly, i just need some point of direction to figure out how to check whatever the user inputs to fire, isn't a location that's already been chosen because i don't even know how to start with checking for that. thank you very much for your time & i am sorry again.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Where to start?

    Firstly, the method is misnamed. It doesn't check anything (the columnCheck method does that). All that method does is get user input.
    Java Code:
    in.nextLine();
    This reads a line of input and throws it away. Is it supposed to do that?

    Look at the if statement. If the condition is false and it enters the else you attempt to read an int. But if it has failed hasNextLine then there is nothing to read. This should throw an exception.

    You are making it much harder than it needs to be.
    Java Code:
    declare boolean
    loop {
        get input
        if input is a letter {
            change boolean
        } else {
            print error message/prompt user to renter
        }
    }
    return input

  6. #6
    AaronHopkins is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    that's what i basically have though? but it's not letting me re-enter values if an invalid input is entered. & i just ran it again right now, & when i enter a letter that's not A-E, an infinite loop displaying Invalid Option is printed.
    Last edited by AaronHopkins; 04-05-2011 at 04:24 AM.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    That is because you are now in the columnCheck method that has the loop but no way to input that value again because that is back in the columnCheck1 method. Perhaps you should combine the 2 methods together.

Similar Threads

  1. BattleShip Helm Needed with GUI
    By Kinyo in forum Eclipse
    Replies: 0
    Last Post: 03-15-2009, 12:04 AM
  2. Battleship game
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 09:42 PM
  3. Battleship help..im confused
    By stepjerd1 in forum Java 2D
    Replies: 4
    Last Post: 01-23-2009, 01:35 AM
  4. Java Battleship Game Help PLEASE
    By mars_red in forum New To Java
    Replies: 0
    Last Post: 02-12-2008, 01:09 AM
  5. Java BattleShip game help
    By mars_red in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2008, 12:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •