game of life

i am stuck with writting the methods for this problem

Page 1 of 1

7 Replies - 2465 Views - Last Post: 14 December 2009 - 01:33 PM Rate Topic: -----

#1 dmarasco   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-December 09

game of life

Posted 01 December 2009 - 10:57 AM

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


public class dmarasco_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 ®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');
	} // End method main()

	/**************************************************************************/
	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;
					}
				}
} // End for
		} // End if
	} // End method setup()

	/**************************************************************************/
	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.print("\n");
		System.out.print("   ");

		for (int x = 1; x <= grid.length; x++)
		{
			System.out.printf("%d", x % 10);
		}
		System.out.print("\n");

		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)
			}
				else
				{
					System.out.print(" ");
				}
			} // End for
			System.out.print("\n");
		} // End for
	} // End method displayGrid()

	/**************************************************************************/
	
	public static void clearGrid(boolean[][] board)
	{
	}

	public static void genNextGrid(boolean[][] board)
	{
	}

	public static int countNeighbors(final boolean[][] board, final int row,
									 final int col )
	{
	}
} // End class netID_Life


*Edited: [ /code] tag added

This post has been edited by pbl: 01 December 2009 - 04:50 PM


Is This A Good Question/Topic? 0
  • +

Replies To: game of life

#2 ianian112   User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: game of life

Posted 01 December 2009 - 12:55 PM

here is show to generate the next grid.
call the check on every live cell, then every dead cell around it, check if the cell is alive and has 2 or 3 neighbors, then add it to another grid which you will draw.
if the cell is dead check if it has 3 neighbors then if true add it to the new grid with the value true
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: game of life

Posted 01 December 2009 - 01:11 PM

Please, :code:

Also, do you have a specific component you need help with? We will be glad to point you in the right direction or help you debug your current work; however, we will not finish the code or enhance it for you.

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: game of life

Posted 01 December 2009 - 04:48 PM

as this question is asked often... I put a Game of Life in the CodeSnippet
you can remove the GUI part if you want it pure console mode
Was This Post Helpful? 0
  • +
  • -

#5 dmarasco   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-December 09

Re: game of life

Posted 02 December 2009 - 12:45 PM

View Postpbl, on 1 Dec, 2009 - 03:48 PM, said:

as this question is asked often... I put a Game of Life in the CodeSnippet
you can remove the GUI part if you want it pure console mode


i cannot find your snippet you speak of.. where is it at?
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: game of life

Posted 02 December 2009 - 01:02 PM

View Postdmarasco, on 2 Dec, 2009 - 01:45 PM, said:

View Postpbl, on 1 Dec, 2009 - 03:48 PM, said:

as this question is asked often... I put a Game of Life in the CodeSnippet
you can remove the GUI part if you want it pure console mode


i cannot find your snippet you speak of.. where is it at?


http://www.dreaminco...dosmember=73490, you click the person's name, and then 'My Contibutions'
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: game of life

Posted 02 December 2009 - 03:32 PM

Or simply My contributions under the person's name at the left of the post
Was This Post Helpful? 0
  • +
  • -

#8 Mobius 1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-December 09

Re: game of life

Posted 14 December 2009 - 01:33 PM

I, too am having issues with this program.
I have the same exact initial code, but I just don't know where to begin on it.
I just need to be pointed in the right direction, I guess...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1