Results 1 to 2 of 2
 12262011, 05:57 AM #1Member
 Join Date
 Sep 2010
 Posts
 83
 Rep Power
 0
Code seems long and confusing....need to simplify
Just wrote this code for minesweeper board....seems like there's a much easier way of doing it....need ideas
Java Code:public void startNewGame(int width, int height) { int rows = width; int columns = height; int[][] newGame = new int[width][height]; Random generator = new Random(); int mines = 10; // should be (width * height) * 0.1; 10% mines int bomb = 8; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { newGame[i][j] = 0; }} int count = mines; while (count > 0) { int x = generator.nextInt(rows); int y = generator.nextInt(columns); if(newGame[x][y] != bomb) { newGame[x][y] = bomb; count; if(x == 0 && y == 0) { if(newGame[x + 1][y] != bomb) newGame[x + 1][y]++; if(newGame[x][y + 1] != bomb) newGame[x][y + 1]++; if(newGame[x + 1][y + 1] != bomb) newGame[x + 1][y + 1]++; } else if(x == (rows  1) && y == 0) { if(newGame[x  1][y] != bomb) newGame[x  1][y]++; if(newGame[x][y + 1] != bomb) newGame[x][y + 1]++; if(newGame[x  1][y + 1] != bomb) newGame[x  1][y + 1]++; } else if(x == 0 && y == (columns  1)) { if(newGame[x][y  1] != bomb) newGame[x][y  1]++; if(newGame[x + 1][y] != bomb) newGame[x + 1][y]++; if(newGame[x + 1][y  1] != bomb) newGame[x + 1][y  1]++; } else if(x == (rows  1) && y == (columns  1)) { if(newGame[x  1][y] != bomb) newGame[x  1][y]++; if(newGame[x][y  1] != bomb) newGame[x][y  1]++; if(newGame[x  1][y  1] != bomb) newGame[x  1][y  1]++; } else if(x == 0) { if(newGame[x + 1][y] != bomb) newGame[x + 1][y]++; if(newGame[x][y  1] != bomb) newGame[x][y  1]++; if(newGame[x][y + 1] != bomb) newGame[x][y + 1]++; if(newGame[x + 1][y  1] != bomb) newGame[x + 1][y  1]++; if(newGame[x + 1][y + 1] != bomb) newGame[x + 1][y + 1]++; } else if(x == (rows  1)) { if(newGame[x  1][y] != bomb) newGame[x  1][y]++; if(newGame[x][y  1] != bomb) newGame[x][y  1]++; if(newGame[x][y + 1] != bomb) newGame[x][y + 1]++; if(newGame[x  1][y  1] != bomb) newGame[x  1][y  1]++; if(newGame[x  1][y + 1] != bomb) newGame[x  1][y + 1]++; } else if(y == 0) { if(newGame[x][y + 1] != bomb) newGame[x][y + 1]++; if(newGame[x  1][y] != bomb) newGame[x  1][y]++; if(newGame[x + 1][y] != bomb) newGame[x + 1][y]++; if(newGame[x  1][y + 1] != bomb) newGame[x  1][y + 1]++; if(newGame[x + 1][y + 1] != bomb) newGame[x + 1][y + 1]++; } else if(y == (columns  1)) { if(newGame[x][y  1] != bomb) newGame[x][y  1]++; if(newGame[x  1][y] != bomb) newGame[x  1][y]++; if(newGame[x + 1][y] != bomb) newGame[x + 1][y]++; if(newGame[x  1][y  1] != bomb) newGame[x  1][y  1]++; if(newGame[x + 1][y  1] != bomb) newGame[x + 1][y  1]++; } else { if(newGame[x][y + 1] != bomb) newGame[x][y + 1]++; if(newGame[x][y  1] != bomb) newGame[x][y  1]++; if(newGame[x  1][y] != bomb) newGame[x  1][y]++; if(newGame[x + 1][y] != bomb) newGame[x + 1][y]++; if(newGame[x + 1][y + 1] != bomb) newGame[x + 1][y + 1]++; if(newGame[x  1][y  1] != bomb) newGame[x  1][y  1]++; if(newGame[x  1][y + 1] != bomb) newGame[x  1][y + 1]++; if(newGame[x + 1][y  1] != bomb) newGame[x + 1][y  1]++; } } }

Re: Code seems long and confusing....need to simplify
You are right to suspect that that code is grossly bloated. One solution is to use a simple nested for loop to loop Math.min and Math.max to calculate the appropriate boundaries of the for loops so they won't go below 0 or beyond the maximum columns or rows of your grid. Something like so:
Java Code:while (count > 0) { int x = generator.nextInt(rows); int y = generator.nextInt(columns); if (newGame[x][y] != bomb) { newGame[x][y] = bomb; // use Math.max and Math.min to make boundaries of for loop // I'll let you figure out what to put in here. int rowMin = .... int colMin = .... int rowMax = .... int colMax = .... for (int r = rowMin; r <= rowMax; r++) { for (int c = colMin; c <= colMax; c++) { if (newGame[c][r] != bomb && // if bomb already not in place !(r == y && c == x)) { // and if not at newly made bomb cell newGame[c][r]++; } } } } }
Similar Threads

help me rewrite my long code.
By Nes_java in forum New To JavaReplies: 1Last Post: 04152011, 12:52 AM 
i need to simplify my code
By felito in forum New To JavaReplies: 7Last Post: 02062011, 05:17 PM 
Please Review My Code (Long Integer Addition)
By Saradus in forum New To JavaReplies: 12Last Post: 07052009, 01:01 PM 
Generate a random code 4 letters long
By bl00dr3d in forum New To JavaReplies: 9Last Post: 04062009, 05:32 AM 
Simple code, but confusing
By t.prasanna in forum New To JavaReplies: 5Last Post: 01222009, 11:26 AM
Bookmarks