CIS 636 Spring 2005                                     Assignment 6 – Simple Threads in Java                                                100 points

Assigned: 02/24/2005

Due: 03/17/2005 at the start of class  - after test and spring break

 

You may work individually or in pairs for this assignment. But all work must be the work of the person/people whose name is on the code! If working in pairs, the individual contributions should be relatively equal. One possibility is to work together tonight, then finish separately (to avoid communication difficulties).

Main Assignment:

We are going to use related but unsynchronized threads to work in (sort of) parallel on a problem. The task involves rating chess boards for how good they are for a player. The task will be divided up among threads so that each thread works on a particular row of the board (hence on a particular array of chess pieces). Each thread will calculate the rating for the row that it is responsible (store the result as an instance variable in the class that you create, so that you are not working with shared data – so you don’t have to worry about synchronization). You have been provided a ChessPiece class and a ChessBoard class (see my WWW page) that you can make use of.  We are working with a very simple rating scheme, like a child might use (my level of chess expertise !!). Each color is rated just based on the ratings of its remaining pieces. See getValue for piece ratings. You task is to

·        write a class that provides thread capabilities for this task. The class should include an instance variable to hold a row of ChessPieces  that the thread is to work on, an instance variable o keep track of which player you are evaluating for (a board that is good for one player is bad for his/her opponent), and an instance variable to hold the rating of the row. You may set up your threads either extending Thread or implementing Runnable; I did it using Runnable.Either way, you will need a constructor that allows getting the info mentioned above into the class.

·        write a main that 1) kludges together a board setup to allow testing your other work; 2) displays the board (ChessBoard’s toString method can be used); 3) starts a thread for each row, 4) waits for all threads to end using join(), 5) retrieves the results from each thread, and 6) produces final summary results. 

Hand in:

To avoid needless long waits, I recommend working on this on the c: drive, then upon completion, copying it to the a: drive, testing it there, creating an extra backup for yourself, then deleting it from the c: drive.

Miscellaneous:

·        MAKE SURE YOUR PROGRAM WORKS! (i.e. more than just removing compile errors).   In the sample below, the board being rated is the initial board setup, so the rating is completely neutral (0).

·        Put YOUR NAME, and e-mail address, date in comments at the beginning of the program.

·        Also, comment any significant code to document it (each method, each loop, each if, and each significant calculation should have a comment. In particular, each method needs to have a JAVADOC compatible comment).

·        Indent code following standard conventions (indent to show that something is “inside” or “part of” the preceding code (e.g. inside if’s or loops, or statements continued on a new line).  The IDE should handle this if you hit ENTER and move on to the next line (If you make changes, you may need to TAB to make things right).

·        Name your variables meaningfully, to describe their use in the program.

·        There is a fair amount to this assignment.  A good strategy for working on a challenging program is to build and test it in parts (e.g. get the setup working, then add next capability and test that, etc)

Sample Interaction:


 

WR  WKt WB  WQ  WK  WB  WKt WR 

WP  WP  WP  WP  WP  WP  WP  WP 

                               

                               

                               

                               

BP  BP  BP  BP  BP  BP  BP  BP 

BR  BKt BB  BQ  BK  BB  BKt BR 

 

Thread: Thread-0 rating: 132

Thread: Thread-5 rating: 0

Thread: Thread-3 rating: 0

Thread: Thread-4 rating: 0

Thread: Thread-6 rating: -8

Thread: Thread-1 rating: 8

Thread: Thread-7 rating: -132

Thread: Thread-2 rating: 0

Rating: 0