Results 1 to 2 of 2
- 02-05-2012, 03:06 AM #1
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
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
- 02-06-2012, 10:07 AM #2
Member
- Join Date
- Feb 2012
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Input Validation Error
By donewithmytime in forum Advanced JavaReplies: 1Last Post: 01-29-2012, 02:02 PM -
Logic Error using loop and array
By janey4115 in forum New To JavaReplies: 1Last Post: 11-17-2011, 06:24 PM -
Input validation help
By AdamG in forum New To JavaReplies: 3Last Post: 10-17-2011, 09:29 PM -
Input Validation Help
By Spyderpig in forum New To JavaReplies: 3Last Post: 02-18-2011, 11:58 AM -
Input Validation
By kickflipper1087 in forum New To JavaReplies: 5Last Post: 11-03-2008, 05:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks