Difference between revisions of "CSC352 Game of Life Homework 1"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <bluebox> Create a version of what we covered in class on 1/21/17. Submit it on Moodle before Tuesday's lecture. </bluebox> <br /> =Implement a Threaded version...")
 
(Requirements)
Line 8: Line 8:
 
=Implement a Threaded version of the Game of Life in Java=
 
=Implement a Threaded version of the Game of Life in Java=
 
<br />
 
<br />
==Requirements==
+
==Tips, tricks, and requirements==
 
<br />
 
<br />
 
:* Only two threads working
 
:* Only two threads working

Revision as of 16:28, 21 February 2017

--D. Thiebaut (talk) 15:27, 21 February 2017 (EST)


Create a version of what we covered in class on 1/21/17. Submit it on Moodle before Tuesday's lecture.


Implement a Threaded version of the Game of Life in Java


Tips, tricks, and requirements


  • Only two threads working
  • The two threads are running identical code
  • The manager displays only the last generation. No need for the manager to synchronize itself with the workers every generation. This will make it simpler to code.
  • Since the threads need to share the dish arrays (and maybe other arrays or variables), one good way to share the array of strings is to put it in a static class, as follows:


class data {
    static String[] dish= {
        "                                                                                  ",
        "   #                                                                              ",
        " # #                                            ###                               ",
        "  ##                                                                              ",
        "                                                                                  ",
        "                                                      #                           ",
        "                                                    # #                           ",
        "                                                     ##                           ",
        "                                                                                  ",
        "                                                                                  "
    };
}
The class data can be declared in the same file that contains the public manager class, as well as the thread class.
Whenever a thread needs to access the dish array, it can just access data.dish, as illustrated below, with the inner loop of the life() function:


                  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 = data.dish[row].length()-1;
                        if ( j==data.dish[row].length() ) realj = 0;

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