Results 1 to 2 of 2
  1. #1
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Need help with input validation, loop logic

    I have a pseudo battleship program. The user picks the size of the game grid, but it must be between 2 and 10 spaces square. I am having a brain fart(an extended one actually). I created a method to make sure the input was in the valid range. Which it does, but if the user puts in an invalid input twice it stalls. I thought my While loop while(validated) would keep re-looping so no matter how many incorrect answer the user gives it keeps telling him its invalid input, but it doesn't. I want it to keep telling the user that is invalid input until they input with in the correct range. Here's my code, thanks in advance!:

    Java Code:
    import java.util.*;
    
    
    public class BattleShipGame 
    {
    	//Scanner object to take input from the user
    	static Scanner console = new Scanner(System.in);
    	public static void main(String[] args) 
    	{
    		//variable declarations
    		int counter = 1;
    		int gridSize = 0;
    		int Xcoordinate = 0;
    		int Ycoordinate = 0;
    		char fireReturn = ' ';
    		int bypass = 0;
    		boolean validated;
    		
    		//Begin game statement and prompt user for input
    		System.out.println("Welcome to the Battleship Game!");
    		System.out.println("If you wish to quit at any time enter a -1 for a coordinate.");
    		System.out.println("Please enter the size of the game board grid (at lease 2 but less than 11):");
    		gridSize = console.nextInt();
    		
    		validated = validateRange(gridSize, 1, 11);
    		 if(!validated)
    		{
    			System.out.println("Invalid entry! Enter a number between 2 and 10:");
    			gridSize = console.nextInt();
    			validated = validateRange(gridSize, 1, 11);
    		}
    		
    		while(validated)
    		{
    			//Instantiates the gameboard
    			String[][] gameBoard = new String[gridSize][gridSize];
    		
    				
    			//fill game board with x's and place the battleship "B"
    			LoadGameBoard(gameBoard);
    			placeBattleShip(gameBoard);
    			
    		
    				//Begin game loop
    				while(Xcoordinate >= 0 && Ycoordinate >= 0)
    				{
    					System.out.println("Enter the X coordinate for your shot: " + "\n" + 
    					                        "Remember the size of your game board is: " + gridSize + " X " + gridSize + "\n" +
    								            "Your entry must be between 0 and " + (gridSize - 1));
    						Xcoordinate = console.nextInt();
    						
    						if(Xcoordinate == -1)
    						{
    							counter--;
    							System.out.println("Thank you for playing, you took " + counter + " shot(s)");
    						}
    						else
    						{
    						
    							System.out.println("Enter the Y coordinate for your shot: " + "\n" + 
    				                    "Remember the size of your game board is: " + gridSize + " X " + gridSize + "\n" +
    						            "Your entry must be between 0 and " + (gridSize - 1));
    							Ycoordinate = console.nextInt();
    							
    							if(Xcoordinate >= 0 && Ycoordinate >= 0)
    							{
    								System.out.println("FIRE!!!!   ZZZZZZRRRRRTTTTTCHHTTTTSHT");
    								fireReturn = Fire(gameBoard, Ycoordinate, Xcoordinate);
    							
    							if(fireReturn == 'H')
    							{
    								System.out.println("Direct hit! you sank my Battleship!");
    								System.out.println("It took a total of " + counter + " shots to win");
    								printBoard(gameBoard);
    								Xcoordinate = -1;
    							}
    							
    							else if(fireReturn == 'M')
    							{
    								System.out.println("You missed, try again");
    							}
    							else
    							{
    								System.out.println("You missed, that position was a repeat, try again");
    							}
    							
    							counter++;
    							}
    							else
    							{
    								counter--;
    								System.out.println("Thank you for playing. " + "You took a total of " + counter + " shots");
    								bypass = 1;
    								printBoard(gameBoard);
    							}
    							
    						}//end of else for 1st coordinate condition	
    							
    				}//end of while "game loop"
    		}
    		
    	}//end of main
    	
    	static public void LoadGameBoard(String[][] board)
    	{
    		for(int row = 0; row < board.length; row++ )
    		{
    			for(int col = 0; col < board[row].length; col++)
    			{
    				board[row][col] = "X";
    			}
    		}
    	}//end LoadGameBoard
    	
    	static public void placeBattleShip(String[][] board)
    	{
    		int coordinateX = 0;
    		int coordinateY = 0;
    		
    		coordinateX = (int)(Math.random() * board.length);
    		coordinateY = (int)(Math.random() * board.length);
    		
    		board[coordinateY][coordinateX] = "B";
    		
    	}//end of placeBattleship
    	
    	static public char Fire(String[][]board,  int x, int y)
    	{
    		char result = ' ';
    		
    		if(board[y][x].equalsIgnoreCase("B"))
    		{
    			result = 'H';
    		
    		}
    		else if(board[y][x].equalsIgnoreCase("X"))
    		{
    			result = 'M';
    			board[y][x] = "M";
    		}
    		else
    		{
    			result = 'A';
    			board[y][x] = "A";
    		}
    		return result;
    		
    	}//end of Fire
    	
    	 static public void printBoard(String[][] board)
    	{
    		for(int row = 0;row < board.length; row++)
    		{
    			for(int col = 0;col < board[row].length; col++)
    			{
    				System.out.print(board[row][col] + " ");
    			}
    			System.out.println();
    		}
    		
    	}//end of printBoard
    	 
    	 static public boolean validateRange(int a, int b, int c)
    	 {
    		 if(a > b && a < c)
    			 return true;
    		 else
    			 return false;
    	 }
    
    }//end of class

  2. #2
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: Need help with input validation, loop logic

    You should change code statement
    Line 26: if(!validated) to while(!validated)
    Line 33: while(validated) to if(validated)

Similar Threads

  1. Input Validation Error
    By donewithmytime in forum Advanced Java
    Replies: 1
    Last Post: 01-29-2012, 02:02 PM
  2. Logic Error using loop and array
    By janey4115 in forum New To Java
    Replies: 1
    Last Post: 11-17-2011, 06:24 PM
  3. Input validation help
    By AdamG in forum New To Java
    Replies: 3
    Last Post: 10-17-2011, 09:29 PM
  4. Input Validation Help
    By Spyderpig in forum New To Java
    Replies: 3
    Last Post: 02-18-2011, 11:58 AM
  5. Input Validation
    By kickflipper1087 in forum New To Java
    Replies: 5
    Last Post: 11-03-2008, 05:47 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
  •