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

From dftwiki3
Jump to: navigation, search
(Testing)
Line 56: Line 56:
 
Figure out a way to test your application and make sure for your threaded game outputs the correct last generation.
 
Figure out a way to test your application and make sure for your threaded game outputs the correct last generation.
 
<br />
 
<br />
 +
<br />
 +
 
=Code for Reuse=
 
=Code for Reuse=
 
<br />
 
<br />

Revision as of 16:36, 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


  • Code your program for only two threads
  • The two threads should be running identical code
  • Use BlockingQueues for the threads to talk to each other.
  • Since the manager waits for the threads to finish all their generations before displaying the last generation, the manager can join() the threads, or you can use a queue between workers and manager to indicate that both threads are done.
  • 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. It doesn't need to be passed to the threads, since it is static and lives in a static class, in shared memory.
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++;
                    }


Testing


Figure out a way to test your application and make sure for your threaded game outputs the correct last generation.

Code for Reuse