user2989486
user2989486

Reputation: 31

Getting an error from the grid[x] I believe

I keep getting an error on the last 3 methods, I believe it is because of the grid[x] but I can't figure it out. I can't seem to get the right syntax for it.

127: error: ')' expected for (int c = 0; c < grid[r].length; c++;) java:160: error: ')' expected if (counterNeighbors(grid, x, y == 3)

import java.util.Scanner;
import java.util.Random;


public class life 
{

// 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();
    }
}


/*******************************************************************************/

// put the three methods you must write here and make sure to document
// them!

public static void clearGrid ( boolean[][] grid )
{
for (int r = 0; r < grid.length; r++)
{
    for (int c = 0; c < grid[r].length; c++;)
    {

        grid[x][y] = false;
    }
}
// goes through the grid and sets every single spot to false or empty
}

public static void genNextGrid ( boolean[][] grid )
{

boolean [][]negen = new boolean [GRIDSIZE][GRIDSIZE];

for (int r = 0; r < grid.length; r++)
{
    for (int c = 0; c < grid[r].length; c++;)
    {


    if (grid[x][y] == false)
    {
        if (counterNeighbors(grid, x, y) == 3)
        {
            nextgen[x][y]= true;
        }
        else
        {
            nextgen[x][y] = false;
        }
    }
    if (grid[x][y] == true)
    {
        if (counterNeighbors(grid, x, y == 3)
        {
            nextgen[x][y] = true;
        }
        else
        {
            nextgen[x][y] = false;
        }
    }
// creates the next grid within the simulation, calls upon the counter Neighbors method to decide
// whether cells are dead or alive


for (int r = 0; r < grid.length; r++)
{
    for (int c = 0; c < grid[r].length; c++;)
    {

        if (nextgen[x][y] != grid[x][y]
        {
            grid[x][y] = nextgen[x][y]
        }
    }
}


}

public static int countNeighbors ( final boolean[][] grid, final int row, final int col )
{
for (int r = 0; r < grid.length; r++)
{
    for (int c = 0; c < grid[r].length; c++;)
    {
        bounds[x][y] = grid[x][y];
    }
}
for (int r = 0; r < grid.length; r++)
{
    for (int c = 0; c < grid[r].length; c++;)
    {

        if((x == row +1) && (y== col +1))
        {
            continue;
        }
        if (bounds[x][y] == true)
        {
            neighbors = neighbors + 1;
        }
    }
}
return neighbors;
// this counts the neighbors each cell has to be used in other methods to determine alive and dead cells.       




}

Upvotes: 0

Views: 136

Answers (2)

SpringLearner
SpringLearner

Reputation: 13854

 for (int c = 0; c < grid[r].length; c++;)
    {

        grid[x][y] = false;
    }

remove ; after c++

like this

 for (int c = 0; c < grid[r].length; c++)
    {

        grid[x][y] = false;
    }

Upvotes: 2

shiraz
shiraz

Reputation: 1218

In your public static void clearGrid ( boolean[][] grid ) method Remove the ; from

  for (int c = 0; c < grid[r].length; c++;)

line

Upvotes: 1

Related Questions