Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   java battleship game (http://www.programmingforums.org/showthread.php?t=15617)

cwl157 Apr 14th, 2008 11:42 PM

java battleship game
 
I am making a sort of battleship simulator. The placement of the ships and hitting is all just random numbers but i am having problems placing the ships. They place fine unless by chance one ship overlaps another. I have a method called canSetShip that takes the amount of space the ship needs and starting and ending locations and verticle or horizontal position and then from that i want to return true or false if it can or can not place the ship. My board is a 2d char array filled with 'w' representing water and then whenever i place a ship i set that spot equal to a capital letter depending on which ship it is. I thought it would be a simple if statement comparing the spot in the 2d char array to the char 'w' and then if they are equal increment a counter and go through until you cant anymore and if that counter reaches the number of spaces required to place that ship return true otherwise return false. However, even if one ship has been placed in a spot if that same spot is checked when the next ship calls that method it still says the char there is a 'w' even though i print the board everytime that method is called and it shows that there is no 'w', it says there is a 'w' there and it prints over the other ship. Anyway thats probably confusing but the bottom line is i want to make a method that returns true if all requirements for a ship to be placed correctly are met and false otherwise. The requirements are that the char at that space is a 'w' and that there is room inside the 10 x 10 2d char array to fit all the chars of that ship. Also on this method i think my loop is messed up because i keep getting an ArrayIndexOutOfBounds exception too. If that could be fixed also that would be good. Here is the method in question. Thanks for any input.
:

  1. static boolean canSetShip(int spacesNeeded, int startRow, int startCol, int direction, Board board)
  2.     {
  3.         int spacesNeededOne = 0;
  4.  
  5.         System.out.println("the board inside catSetShip looks like: ");
  6.         board.printBoard();
  7.  
  8.         try
  9.         {
  10.         // place ship horizontal
  11.         if (direction == 0)
  12.         {
  13.           for (int i = startCol; i <= board.ar[i].length-1; i++)
  14.           { 
  15.             if (Character.isLowerCase(board.ar[startRow][i]))
  16.             {
  17.                 System.out.println("the char at board.ar[startRow][i] is " + board.ar[startRow][i]);
  18.                 spacesNeededOne++;
  19.                 System.out.println("spacesNeededOne = " + spacesNeededOne);
  20.             } // end if
  21.  
  22.             if (spacesNeededOne == spacesNeeded)
  23.                 return true;
  24.           } // end for
  25.         } // end if
  26.  
  27.         // place ship verticle
  28.       else if (direction == 1)
  29.         {
  30.           for (int i = startRow; i <= board.ar.length-1; i++)
  31.           {
  32.             if (Character.isLowerCase(board.ar[i][startCol]))
  33.             {
  34.               System.out.println("the char at board.ar[i][startCol] is " + board.ar[i][startCol]);
  35.               spacesNeededOne++;
  36.               System.out.println("spacesNeededOne = " + spacesNeededOne);
  37.             } // end if
  38.  
  39.             if (spacesNeededOne == spacesNeeded)
  40.                 return true;
  41.           } // end for
  42.         } // end else if
  43.         } // end try
  44.         catch(ArrayIndexOutOfBoundsException e)
  45.         {
  46.             System.out.println("ArrayIndexOutOfBoundsException thrown");
  47.             return false;
  48.         } // end catch
  49.         return false;
  50.     } // end canSetShip


Also, I changed my if statements to just see if its a lowercase character because all the ships are represented as an uppercase character so i thought this check would work as well but i print each char and they say they are all 'w' even though they shouldnt be. I'll post output too. In this output you will notice the SSS gets overwritten by the last print out there is only 1 'S' followed by 2 'C' there should be 3 'S' in a row for the Sub ship
Quote:

w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w w w
w S S S w w w w w w
the char at board.ar[i][startCol] is w
spacesNeededOne = 1
the char at board.ar[i][startCol] is w
spacesNeededOne = 2
the char at board.ar[i][startCol] is w
spacesNeededOne = 3
the char at board.ar[i][startCol] is w
spacesNeededOne = 4
Setting patrol...

startRow = 7
startCol = 8
direction =0
Inside while
the board inside catSetShip looks like:
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w w w
w S S S w w w w w w
the char at board.ar[startRow][i] is w
spacesNeededOne = 1
ArrayIndexOutOfBoundsException thrown
new direction = 9
new row = 3
new col = 6
Inside while
the board inside catSetShip looks like:
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w w w
w S S S w w w w w w
new direction = 0
new row = 2
new col = 4
Inside while
the board inside catSetShip looks like:
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w w w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w w w
w S S S w w w w w w
the char at board.ar[startRow][i] is w
spacesNeededOne = 1
the char at board.ar[startRow][i] is w
spacesNeededOne = 2
Setting carrier...

startRow = 9
startCol = 2
direction =0
Inside while
the board inside catSetShip looks like:
w w w w w w w w w w
w w w w w w w w w w
w w w w P P w w w w
w w w w w w w w w w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w w w
w S S S w w w w w w
the char at board.ar[startRow][i] is w
spacesNeededOne = 1
the char at board.ar[startRow][i] is w
spacesNeededOne = 2
the char at board.ar[startRow][i] is w
spacesNeededOne = 3
the char at board.ar[startRow][i] is w
spacesNeededOne = 4
the char at board.ar[startRow][i] is w
spacesNeededOne = 5
w w w w w w w w w w
w w w w w w w w w w
w w w w P P w w w w
w w w w w w w w w w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w B w
w w w w w w w w w w
w S C C C C C w w w

cwl157 Apr 15th, 2008 6:33 PM

Re: java battleship game
 
Ok, I figured out what my error was and I cleaned up that method and the other methods that were also part of the error so its working now.


All times are GMT -5. The time now is 12:54 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC