Results 1 to 4 of 4

Thread: Battleship game

  1. #1
    afarag is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Question Battleship game

    Hey, I am having a difficult time figuring out how to implement something where when the player places his ships, there is a check to see if it can fit into the grid such as if a 5 piece is already played and a 4 piece wants to be placed but the 5 piece is in the way. How do I report that they need to choose a different location? This is the code I have so far:

    Java Code:
    System.out.print("Player two, Where would you like your Aircraft Carrier(5) to start(Enter x and y coordinate): ");
    		int i = IO.readInt();
    		int j = IO.readInt();
    		System.out.print("Would you like it to be placed Horizontal('H') or Vertical('V')? ");
    		char direction = IO.readChar();
    		i = i - 1;
    		j = j - 1;
    		if (direction == 'H' || direction == 'h'){
    			for (int count = 0; count < (5); count++){
    			player2board[i][j] = 'A';
    				player2board[i][j++]= 'A';
    			}
    			
    		}	else {
    			for (int count = 0; count < (5); count++){
    				player2board[i][j] = 'A';
    				player2board[i++][j]= 'A';
    		
    
    		}
    		
    			}
    		showPlayer2board(player2board);
    		
    		System.out.print("Player two, where would you like your Battleship(4) to start(Enter x and y coordinate): ");
    		i = IO.readInt();
    		j = IO.readInt();
    		i = i - 1;
    		j = j - 1;
    		while (player2board[i][j] == 'A'){
    			System.out.println("Spot already used choose a new start location: ");
    			i = IO.readInt();
    			j = IO.readInt();
    			i = i - 1;
    			j = j - 1;
    		}
    		System.out.print("Player two, Would you like it to be placed Horizontal('H') or Vertical('V')? ");
    		direction = IO.readChar();
    		if (direction == 'H'){
    			for (int count = 0; count < (4); count++){
    			player2board[i][j] = 'B';
    				player2board[i][j++]= 'B';
    			}
    			
    		}	else {
    			for (int count = 0; count < (4); count++){
    				player2board[i][j] = 'B';
    				player2board[i++][j]= 'B';
    		
    
    		}
    		
    			}
    		showPlayer2board(player2board);
    		
    		System.out.print("Player 2, Where would you like your Submarine(3) to start(Enter x and y coordinate): ");
    		i = IO.readInt();
    		j = IO.readInt();
    		i = i - 1;
    		j = j - 1;
    		while (player2board[i][j] == 'A' || player2board[i][j] == 'B'){
    			System.out.println("Spot already used choose a new start location: ");
    			i = IO.readInt();
    			j = IO.readInt();
    			i = i - 1;
    			j = j - 1;
    		}
    		System.out.print("Would you like it to be placed Horizontal('H') or Vertical('V')? ");
    		direction = IO.readChar();
    		if (direction == 'H'){
    			for (int count = 0; count < (3); count++){
    			player2board[i][j] = 'S';
    				player2board[i][j++]= 'S';
    			}
    			
    		}	else {
    			for (int count = 0; count < (3); count++){
    				player2board[i][j] = 'S';
    				player2board[i++][j]= 'S';
    		
    
    		}
    		
    			}
    		showPlayer2board(player2board);
    		
    		System.out.print("Player two, where would you like your Destroyer(3) to start(Enter x and y coordinate): ");
    		i = IO.readInt();
    		j = IO.readInt();
    		i = i - 1;
    		j = j - 1;
    		
    		while (player2board[i][j] == 'A' || player2board[i][j] == 'B' || player2board[i][j] == 'S'){
    			System.out.println("Spot already used choose a new start location: ");
    			i = IO.readInt();
    			j = IO.readInt();
    			i = i - 1;
    			j = j - 1;
    		}
    		System.out.print("Would you like it to be placed Horizontal('H') or Vertical('V')? ");
    		direction = IO.readChar();
    		if (direction == 'H'){
    			for (int count = 0; count < (3); count++){
    			player2board[i][j] = 'D';
    				player2board[i][j++]= 'D';
    			}
    			
    		}	else {
    			for (int count = 0; count < (3); count++){
    				player2board[i][j] = 'D';
    				player2board[i++][j]= 'D';
    		
    
    		}
    		
    			}
    		showPlayer2board(player2board);
    		
    		System.out.print("Player 2, Where would you like your Patrol Boat(2) to start(Enter x and y coordinate): ");
    		i = IO.readInt();
    		j = IO.readInt();
    		i = i - 1;
    		j = j - 1;
    		while (player2board[i][j] == 'A' || player2board[i][j] == 'B' || player2board[i][j] == 'S' || player2board[i][j] == 'D'){
    			System.out.println("Spot already used choose a new start location: ");
    			i = IO.readInt();
    			j = IO.readInt();
    			i = i - 1;
    			j = j - 1;
    		}
    		System.out.print("Would you like it to be placed Horizontal('H') or Vertical('V')? ");
    		direction = IO.readChar();
    		if (direction == 'H'){
    			for (int count = 0; count < (2); count++){
    			player2board[i][j] = 'P';
    				player2board[i][j++]= 'P';
    			}
    			
    		}	else {
    			for (int count = 0; count < (2); count++){
    				player2board[i][j] = 'P';
    				player2board[i++][j]= 'P';
    		
    
    		}
    		
    			}
    		showPlayer2board(player2board);
    	}
    Last edited by afarag; 04-13-2014 at 04:13 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Battleship game

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Battleship game

    Hi,

    first I want to tell you some ways to restructure your code so you can modify it more easily:

    - If you have (more or less) the same code in multiple places, then you should always move that code into a separate function.
    - Try to keep functions small.
    - Try that a function is doing one thing ... (which is also given in the function name)

    Now we could concentrate on your code:

    - If you have code that might run multiple times, you should use a loop as construct. So you could have something like
    Java Code:
    do {
      // Get User Input
      // Check Locations
      // Set Ship if check was successfull
    } while (<ShipNotPlaced>);
    - If you want to check the place of the ship, you have to check all points where the ship will be and not just the first one. (So you could build up a loop as you did with placing the ship.)

    - If you want to check, if a cell is empty: Just check if the cell is not as it was initialized. That way you only have one comparison instead of multiple comparisons.

    - you might want to get rid of "magic stuff". RIght now you use characters like A, B, C for different ships. One common way to code different stati somethig could have is an enumeration. So you could build an enumerations of field content which could then be "empty", "carrier", ... If you do this, the code will be easier to read, too, because you get somethinge like if (player2board[x][y] != FieldContent.empty).

    With kind regards,

    Konrad

  4. #4
    afarag is offline Member
    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Default Re: Battleship game

    Thank you! This really helped, ill try to keep in mind the previous stuff said as well so its less of a pain to repeat stuff, still in intro to comp sic :)

Similar Threads

  1. End battleship game
    By redblack in forum New To Java
    Replies: 2
    Last Post: 07-18-2013, 05:32 AM
  2. Help with battleship game, please!
    By goodvibes in forum New To Java
    Replies: 5
    Last Post: 07-25-2012, 12:04 PM
  3. Help with Battleship Game
    By Lucid15 in forum New To Java
    Replies: 59
    Last Post: 04-11-2012, 08:37 PM
  4. Battleship Game - really need help
    By thrashsynergy in forum Java Applets
    Replies: 5
    Last Post: 04-25-2011, 08:21 PM
  5. Battleship game
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 10:42 PM

Tags for this Thread

Posting Permissions

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