Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Fubarable

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

  1. #1
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default 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]++;
                        }
                }
            }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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]++;
                      }
                   }
                }
             }
          }
    Danieldcc likes this.

Similar Threads

  1. help me rewrite my long code.
    By Nes_java in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 12:52 AM
  2. i need to simplify my code
    By felito in forum New To Java
    Replies: 7
    Last Post: 02-06-2011, 04:17 PM
  3. Please Review My Code (Long Integer Addition)
    By Saradus in forum New To Java
    Replies: 12
    Last Post: 07-05-2009, 01:01 PM
  4. Generate a random code 4 letters long
    By bl00dr3d in forum New To Java
    Replies: 9
    Last Post: 04-06-2009, 05:32 AM
  5. Simple code, but confusing
    By t.prasanna in forum New To Java
    Replies: 5
    Last Post: 01-22-2009, 10:26 AM

Posting Permissions

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