Results 1 to 3 of 3

Thread: Comparing Elements in an Array

  1. #1
    Join Date
    Dec 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Comparing Elements in an Array

    ok so in my program i have a boolean array and i need to compare one element with the neighbors around it. neighbors consist of the 8 cells immediately adjacent to the one being examined (top and bottom, right and left, and then both diagnols). You also have take into account if you are on an edge of the matrix (bottom or top rows, leftmost or rightmost
    columns), because you will need to make sure you don't try to count the neighbors that would appear off the matrix. The return value of this method should be a number between 0 and 8. This all has to go into my countNeighbors method. Heres my code just in case u need it.


    Code:
    import java.util.Scanner;
    import java.util.Random;
    
    
    public class test
    {
    
        // the size of the grid (GRIDSIZE x GRIDSIZE)
        final private static int GRIDSIZE = 18;
    
        /********************************************************************************/
        public static void main ( String args[] )
        {
            boolean[][] board = new boolean[GRIDSIZE][GRIDSIZE];
            char choice;
            int x = 1;
            Scanner sc = new Scanner ( System.in );
    
            do
            {
                System.out.print ( "Start with a (r)andom board, the (q)ueen bee shuttle or the (g)lider pattern? ");
                choice = sc.next().charAt(0);
            } while ( choice != 'r' && choice != 'q' && choice != 'g' );
    
            clearGrid (board);
            setup(board,choice);
    
            do
            {
                System.out.printf ("Viewing generation #%d:\n\n", x++);
                displayGrid(board);
                genNextGrid(board);
                System.out.print ("\n(q)uit or any other key + ENTER to continue: ");
                choice = sc.next().charAt(0);
            } while ( choice != 'q' );
    
        }
    
        /********************************************************************************/
        public static void setup (boolean[][] board, char which )
        {
            Random randomNumbers = new Random();
    
            clearGrid(board);
    
            if ( which == 'q' )
            {
                // Set up the Queen Bee Shuttle pattern
                board[5][1] = true;board[5][2] = true;board[6][3] = true;board[7][4] = true; 
                board[8][4] = true;board[9][4] = true;board[10][3] = true;board[11][2] = true;
                board[11][1] = true;        
            }
            else if ( which == 'g' )
            {
                // Set up a Glider
                board [17][0] = true; board[16][1] = true; board[15][1] = true;
                board[16][2] = true;
                board [17][2] = true;
            }
            else
            {
                // set up random
                for (int row = 0; row < board.length; row++ )
                {
                    for (int col = 0; col < board[row].length; col++ )
                    {
                        if ( randomNumbers.nextInt() % 2 == 0 )
                            board[row][col] = true;
                    }
                }
            }
    
        }
    
        /********************************************************************************/
        public static void displayGrid (boolean[][] grid)
        {
            // Start printing the top row of numbers
            System.out.print ("   ");
            for (int x = 1; x <= grid.length; x++)
            {
                if ((x / 10) != 0)
                    System.out.printf ( "%d", x / 10 );
                else
                    System.out.print ( " " );
            }
    
            System.out.println();
            System.out.print( "   " );
    
            for (int x = 1; x <= grid.length; x++)
            {
                System.out.printf ( "%d", x % 10 );
            }
            System.out.println();
    
            for (int r = 0; r < grid.length; r++)
            {
                System.out.printf ( "%d", r+1 );
                if (r + 1 < 10)
                    System.out.print ( "  " );
                else
                    System.out.print ( " " );
                for (int c = 0; c < grid.length; c++)
                {
                    if (grid[r][c] == true)
                        System.out.print ( "*" );
                    else
                        System.out.print ( " " );
                }
                System.out.println();
            }
        }
    
    
        /*******************************************************************************/
    
     
        public static void clearGrid ( boolean[][] board )
        {
    
         for (int i = 0; i < board.length; i++)
         {
         for( int j = 0; j < board.length; j++)
         {
         boolean[i][j] = false;
         }
         }
    
        }
    
        public static void genNextGrid ( boolean[][] board )
        {
        }
    
        public static int countNeighbors ( final boolean[][] board, final int row, final int col )
        {
        }
    }

  2. #2
    Join Date
    Sep 2008
    Location
    London or Slovakia
    Posts
    27
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default

    That is nice. And we are supposed to do with it?
    If you have errors provide stack trace, if program doesn't do what is supposed to do explain the trouble making section. Do not expect people will go and debug your code, because of you blue eyes...

  3. #3
    Join Date
    Jan 2009
    Location
    Athens, GA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    OMG The memories. I had to do basically the same thing for a MineSweeper game, except I was using an two dimensional int array. And also had to do it recursively under a certain condition.

    The way I did it was:
    1. I had local variables to indicated if a cell was at a particular edge (top, bottom, right, left).
    2. Then a WHOLE bunch of nested if statements. It first checked where the cell was, then would determine how to count after that. It's really long, and I don't know of a more efficient way to do it.

    If you'd like, you can check out my code (https://minesweeper.dev.java.net/sou...70&view=markup). Look at the checkAdjacentCells method.

Bookmarks

Posting Permissions

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