| 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.
:
static boolean canSetShip(int spacesNeeded, int startRow, int startCol, int direction, Board board) { int spacesNeededOne = 0; System.out.println("the board inside catSetShip looks like: "); board.printBoard(); try { // place ship horizontal if (direction == 0) { for (int i = startCol; i <= board.ar[i].length-1; i++) { if (Character.isLowerCase(board.ar[startRow][i])) { System.out.println("the char at board.ar[startRow][i] is " + board.ar[startRow][i]); spacesNeededOne++; System.out.println("spacesNeededOne = " + spacesNeededOne); } // end if if (spacesNeededOne == spacesNeeded) return true; } // end for } // end if // place ship verticle else if (direction == 1) { for (int i = startRow; i <= board.ar.length-1; i++) { if (Character.isLowerCase(board.ar[i][startCol])) { System.out.println("the char at board.ar[i][startCol] is " + board.ar[i][startCol]); spacesNeededOne++; System.out.println("spacesNeededOne = " + spacesNeededOne); } // end if if (spacesNeededOne == spacesNeeded) return true; } // end for } // end else if } // end try catch(ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException thrown"); return false; } // end catch return false; } // 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
|
|