![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 284
Rep Power: 4
![]() |
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.
java Syntax (Toggle Plain Text)
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:
|
|
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 284
Rep Power: 4
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Coder's Block Arena - The Game AI Platform | Sane | Existing Project Development | 23 | May 6th, 2008 9:12 PM |
| Java Snake Game I made by my self | msi_333 | Java | 0 | May 29th, 2007 11:44 AM |
| - Java game programming - | BinaryStorm | Java | 8 | Oct 2nd, 2006 11:25 AM |
| Programming with Java: Tutorial | ReggaetonKing | Java | 4 | May 22nd, 2006 5:53 PM |
| Java programmers, game developers, artists, be ware! RPG game team is recruiting! | atcomputers.us | Paid Job Offers | 7 | Sep 25th, 2005 7:25 PM |