CSC352 Game of Life Lab 2017

From dftwiki3
Revision as of 17:02, 8 February 2017 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- =Game of Life= <br /> <bluebox> In this lab you will write a multithreaded version of Conway's Game of Life. </bluebox> <br /> =Serial Game of Life= <br /> [http...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut (talk) 17:02, 8 February 2017 (EST)


Game of Life


In this lab you will write a multithreaded version of Conway's Game of Life.


Serial Game of Life


Wikipedia has a good page on Conway's game of life. Please read the first section containing the rules.
The program below is a serial version of the game of life taken from RosettaCode.org.

It displays the generation of cells using simply ascii characters on the console. To run it, copy/paste it in a file called GameOfLife.java in your 352b account.
Compile and run it as follows:

javac GameOfLife.java
java GameOfLife

and adjust the size of your terminal/console to make sure you can see the pattern evolving. The generations are stable for a few seconds, then one of the moving patterns (glider) hits the blinker, and the population of cells start growing until it freezes with just a few cells blinking.



/*  
 Game of life
 D. Thiebaut
 Heavily adapted from code found in java section at this
 URL:  https://rosettacode.org/wiki/Conway%27s_Game_of_Life#Java

 This code works in console mode, displaying successive generations
 of the game of life on the screen, and clearing the screen between
 each one.

 The initial pattern is defined in the array dish (as in petri dish).

 To compile and run:

 javac GameOfLife.java
 java GameOfLife


*/

public class GameOfLife{
    public static void main(String[] args){
	String[] dish2 = {
	    "     ",
	    "  #  ",
	    "  #  ",
	    "  #  ",
	    "     " 
	};
	String[] dish= {
	    "                                                                                  ",
	    "   #                                                                              ",
	    " # #                                            ###                               ",
	    "  ##                                                                              ",
	    "                                                                                  ",
	    "                                                      #                           ",
	    "                                                    # #                           ",
	    "                                                     ##                           ",
	    "                                                                                  ",
	    "                                                                                  ",
	    "                                                                                  ",
	    "                                                                                  ",
	    "             #                                                                    ",
	    "           # #                                                                    ",
	    "            ##                                                                    ",
	    "                                                                                  ",
	    "                                                                                  ",
	    "                                                                                  ",
	    "                                                                                  ",
	    "                                                                                  ",
	    "                                                                                  ",
	    "                                                                                  ",
	    "                                                                                  ",
	    "                                                                                  ",
	    "                                                                                  ",
	    "                                                                                  ",
	    "                                                                                  ",
	    "                                                                                  "
	};
	int gens= 30000;
	for (int i= 0;i < gens;i++) {
	    //System.out.println("Generation " + i + ":\n\n\n");
	    print(dish);
	    dish= life(dish);
	    
	    clearScreen();
	    // add a bit of a delay to better see the visualization
	    // remove this part to get full timing.
	    try {
		Thread.sleep(50);                
	    } catch(InterruptedException ex) {
		return;
	    }
	}
    }

    public static void clearScreen(){
	final String ANSI_CLS = "\u001b[2J";
        final String ANSI_HOME = "\u001b[H";
        //System.out.print(ANSI_CLS + ANSI_HOME);
        System.out.print( ANSI_HOME);
        System.out.flush();
    }
    
    public static String[] life(String[] dish){
	String[] newGen= new String[dish.length];

	for ( int row= 0; row < dish.length; row++ ) {//each row

	    newGen[row]= "";

	    for ( int i= 0; i < dish[row].length(); i++ ) {//each char in the row

		int neighbors = 0;
		char current = dish[row].charAt(i);

		// loop in a block that is 3x3 around the current cell
		// and count the number of '#' cells.  
		for ( int r=row-1; r<=row+1; r++ )  {

		    // make sure we wrap around from bottom to top
		    int realr = r;
		    if ( r==-1 )         realr = dish.length-1;
		    if ( r==dish.length) realr = 0;

		    for ( int j=i-1; j<=i+1; j++ ) {
			
			// make sure we wrap around from left to right
			int realj = j;
			if ( j==-1 )                 realj = dish[row].length()-1;
			if ( j==dish[row].length() ) realj = 0;

			if (r==row && j==i ) continue; // current cell is not its
			                               // neighbor
			if (dish[realr].charAt(realj) == '#' )
			    neighbors++;
		    }
		}

		if ( current=='#' ) 
		    if (neighbors < 2 || neighbors > 3) 
			newGen[row] +=  " "; 
		    else
			newGen[row] += "#";  

		if ( current==' ' )
		    if ( neighbors == 3 )
			newGen[row] += "#";
		    else
			newGen[row] += " ";

		
	    }
	}
	return newGen;
    }
    
    public static int getNeighbors(char above, char below, char left, char right ) {
	int ans= 0;
	if ( above == '#'  ) ans++;
	if ( below == '#'  ) ans++;
	if ( left  == '#'  ) ans++;
	if ( right == '#'  ) ans++;
	return ans;
    }
    
    public static void print( String[] dish ) {

	for ( String s: dish )
	    System.out.println(s);
	
    }
}