# Thread: Code seems long and confusing....need to simplify

1. Member Join Date
Sep 2010
Posts
84
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]++;
}
}
}```  Reply With Quote

2. ## 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]++;
}
}
}
}
}```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•