Results 1 to 3 of 3
- 06-03-2009, 04:31 AM #1
BattleShip game ship on top of ship
Hi!
i am working on a text version battleship game, but my problem is the ships that are generated by the computer are placed one on top of another.
Does anybody have any solutions on how this can be fixed? :confused:
My code:
Any suggestions would be very helpful !!!!Java Code:import java.util.Random; public class BattleShip { public String[][] grid; private int[] takenX = new int[100]; private int[] takenY = new int[100]; private int length; private int width; public BattleShip(int l, int w) { grid = new String[l][w]; length = l; width = w; initGrid(); } public void initGrid() { for (int r = 0; r < grid.length; r++) for (int c = 0; c < grid[r].length; c++) { grid[r][c] = "O"; } } public void printGrid() { String g = ""; g += " "; for (int i = 1; i <= (grid.length); i++) { g += (i + " "); } g += "\n\n"; for (int r = 0; r < grid.length; r++) { if (r >= 9) g += ((r + 1) + " "); else g += ((r + 1) + " "); for (int c = 0; c < grid[r].length; c++) { g += ((grid[r][c]) + " "); } g += "\n"; } System.out.println(g); } public boolean isValidLocation(int l, int h) { if ((grid[l][h]).equals("O")) return false; else return true; } public void placeShip(int l) { Random rand = new Random(); int x = rand.nextInt(10 - (l - 1)); int y = rand.nextInt(10 - (l - 1)); int dir = rand.nextInt(2) + 1; if (dir == 1) { for (int i = 0; i < l; i++) { grid[x + i][y] = "#"; } } else { for (int j = 0; j < l; j++) { grid[x][y - 1] = "#"; } } } public static void main(String[]args) { BattleShip battleship = new BattleShip(10,10); battleship.placeShip(5); battleship.printGrid(); } }-- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
-- 2.66 Ghz Pentium Geforce NVidia 440 64mb
-
Some suggestions:
1) Use either an enum for horizontal / vertical or a boolean such as "placeHorizontal" since there are only two possibilities, and a boolean or enum would be much more readable than dir == 1 or dir ==2.
2) Select horizontal vs. vertical placement first as it will effect the possible range of positions available for the ship on both axi. For instance, if the ship is of length 5 and you randomly place it horizontally, then there are 6 possible starting x locations, and 10 possible starting y locations. Conversely if the ship is to be placed vertically, then swap the previous statements.
3) Easiest I think is a placeShip method that repeatedly selects a random location until one is found that doesn't overlap. A do-while loop could work nicely here.
- 06-03-2009, 12:42 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Similar Threads
-
battle Ship game help!!
By linux1man in forum New To JavaReplies: 4Last Post: 06-03-2009, 01:47 AM -
Battleship game
By kathyla18 in forum New To JavaReplies: 2Last Post: 02-26-2009, 09:42 PM -
Java Battleship Game Help PLEASE
By mars_red in forum New To JavaReplies: 0Last Post: 02-12-2008, 01:09 AM -
Java BattleShip game help
By mars_red in forum Advanced JavaReplies: 0Last Post: 02-12-2008, 12:58 AM -
error with traverse a relations ship
By darkbalder in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 12-11-2007, 05:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks