Results 1 to 5 of 5
Thread: battle Ship game help!!
- 06-03-2009, 01:10 AM #1
battle Ship game help!!
Hi!
i am working on this text version battleship game, and i can not get the
computer to place the ships randomly.
i get arrayoutof bounds exeption error, but i dont understand why. i posted my code below, so can somebody point out were the problem is?
Java Code:/* * * A Battleship game */ import java.util.Random; public class BattleShip { public String[][] grid; private int length; private int width; private boolean placed = false; 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 boolean isValidShipLocation(int l, int h, int leng, int dir) { int countValid = 0; if (dir == 1) { for (int i = 0; i <= leng; i++) { if ((grid[l][h + i]).equals("#")); countValid++; } } else if (dir == 2) { for (int j = 0; j <= leng; j++) { if ((grid[l + j][h]).equals("#")); countValid++; } } else if (dir == 3) { for (int k = 0; k <= leng; k++) { if ((grid[l - k][h]).equals("#")); countValid++; } } else { for (int z = 0; z <= leng; z++) { if ((grid[l][h - z]).equals("#")); countValid++; } } if (countValid == 0) return true; else return false; } public void genShip(int len) { if (placed == false) { Random rand = new Random(); int x = rand.nextInt(10); int y = rand.nextInt(10); int dir = rand.nextInt(4) + 1; if (isValidShipLocation(x,y,len,dir) == true) { for (int i = 0; i <= len; i++) { if (dir == 1) grid[x][y + i] = "#"; else if (dir == 2) grid[x + i][y] = "#"; else if (dir == 3) grid[x - i][y] = "#"; else grid[x][y - i] = "#"; } placed = true; } else { genShip(len); } } } public static void main(String[]args) { BattleShip battleship = new BattleShip(10,10); battleship.genShip(5); battleship.genShip(4); battleship.genShip(3); battleship.genShip(3); battleship.genShip(2); battleship.printGrid(); } }
:eek: Any help would be appreciated !-- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
-- 2.66 Ghz Pentium Geforce NVidia 440 64mb
-
Put some System.out.println(...) statements in there, and you'll see that you're going off the grid. Or you can use System.out.printf, which I find easier to use in this situation:
i.e.,
Java Code:System.out.printf("l = %d; h = %d, j = %d%n", l, h, j); if ((grid[l + j][h]).equals("#")) ; countValid++;
- 06-03-2009, 01:38 AM #3
so is there a quick fix for this problem?
-- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
-- 2.66 Ghz Pentium Geforce NVidia 440 64mb
-
hm, fix the logic, which is a bit of busy work that I wish I had time for but don't. I've 30 charts to dictate. But I've seen your posts, and I know that you've got the smarts. I'll bet that with a bit of concentration and lots of scratch paper you'll solve it quicker than any of us could.
- 06-03-2009, 01:47 AM #5
Similar Threads
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
Game 21
By aRTx in forum Advanced JavaReplies: 3Last Post: 04-04-2009, 12:33 AM -
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02-10-2009, 06:00 AM -
Virtual Battle Programmer
By ALATECjobs in forum Jobs OfferedReplies: 2Last Post: 08-28-2008, 04:52 PM -
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