Homework 11



Due: 11:59 PM on Friday, November 19, 2004

Reading: No additional reading from last week

1 problem, 50 points (extra credit is also possible)



Problem 1     Connect Four -- Building a game player     [topics: some AI topics and more practice with classes ]

In this problem, you will be extending the Connect Four code from last week's assignment so that it actually plays the game (rather than simply hosting a game between two human players). In doing so, you will build a new class, named Player, which will have several methods for selecting Connect Four moves.

Using Assignment 10, Problem 1

You will need to start with your code for Hw10Pr1 (the CS5App and Board classes. Similar code is provided in Hw11Pr1.zip, but you should feel free to replace any of that code with your own! Note that the winsFor, allowsMove, and isFull methods will need to be copied from your Hw10 to your Hw11 code! In addition, you will be creating two or three more methods in the Board class this week.


The CS5App class

The main method of your CS5App class will be similar to last week's, but with some crucial differences to take advantage of the Player class. This description will highlight the differences. Your main method inside the CS5App class should do the following:

Additional methods required in the CS5App class

None are required, though I created one called getPly that queries the human user for a lookahead type and returns it along with one called getTie that does the same thing for the tiebreaking strategy.


The Board class

Additional methods to be placed in the Board class

Your Board class needs three additional methods beyond those required in Assignment 10:


The Player class

Required data members to have in the Player class



Methods required for the Player class


Samples

We will test your code by running a human vs. a human and then asking (with the -1, -2, and -3 options) for the scores that your code assigns to each possible move. We will also run your computer players against each other to check for tie-breaking and lookahead.

Here are some of the test cases we will try -- check to be sure your code works!

    

    TEST CASE 1

    With the following board, obtained through the moves 4-3-4-3-4-5

     | | | | | | | |        (X's move)
     | | | | | | | |
     | | | | | | | |
     | | | | |X| | |
     | | | |O|X| | |
     | | | |O|X|O| |
     ---------------
      0 1 2 3 4 5 6

    It is X's move, and asking for 1-ply scores should produce these scores:

    X's move (-N for an N-ply hint):  -1

     Scores:
     0      1      2      3     4       5      6  
     50.0   50.0   50.0   50.0  100.0   50.0   50.0      

    Of course the best move is column 4, which wins for X.

    -----------------------------------------------------------------

    TEST CASE 2

    With the following board, obtained through the moves 0-6-1-0-2

     | | | | | | | |        (O's move)
     | | | | | | | |
     | | | | | | | |
     | | | | | | | |
     |O| | | | | | |
     |X|X|X| | | |O|
     ---------------
      0 1 2 3 4 5 6

    It is O's move, and asking for 1-ply scores should produce
      no distinction among the columns:

    O's move (-N for an N-ply hint):  -1

     Scores:
     0      1      2      3      4      5      6  
     50.0   50.0   50.0   50.0   50.0   50.0   50.0      


    2-ply (or 3-ply) lookahead should show that column 3 is the best:

    O's move (-N for an N-ply hint):  -2

     Scores:
      0      1      2      3      4      5      6  
      0.0    0.0    0.0   50.0    0.0    0.0    0.0      

    -----------------------------------------------------------------

    TEST CASE 3

    With the following board, obtained through the moves 1-2-1-1-2-4-4-4-4-0

     | | | | | | | |        (X's move)
     | | | | | | | |
     | | | | |X| | |
     | |O| | |O| | |
     | |X|X| |X| | |
     |O|X|O| |O| | |
     ---------------
      0 1 2 3 4 5 6

    It is X's move, and asking for 3-ply scores should produce
      a perfect score in column 3:

    X's move (-N for an N-ply hint): -3

     Scores:
     0      1      2      3      4      5      6  
     50.0   50.0   50.0  100.0   50.0   50.0   50.0      


    Then, if you move 'X' to column 3, you can new ask 'O' what it thinks:

     | | | | | | | |        (O's move)
     | | | | | | | |
     | | | | |X| | |
     | |O| | |O| | |
     | |X|X| |X| | |
     |O|X|O|X|O| | |
     ---------------
      0 1 2 3 4 5 6

    O's move (-N for an N-ply hint): -3

     Scores:
      0      1      2      3      4      5      6  
      0.0    0.0    0.0    0.0    0.0    0.0    0.0      

    O's move (-N for an N-ply hint): -2

     Scores:
      0      1      2      3      4      5      6  
      0.0    0.0    0.0    0.0    0.0    0.0    0.0      

    O's move (-N for an N-ply hint): -1

     Scores:
     0      1      2      3      4      5      6  
     50.0   50.0   50.0   50.0   50.0   50.0   50.0      


   Which, perhaps, sends the message that things might look
   better if you don't look too far ahead... .

   -----------------------------------------------------------------

    TEST CASE 4

    With an almost identical board, obtained through the moves 1-2-1-1-2-4-4-4-4

     | | | | | | | |        (O's move)
     | | | | | | | |
     | | | | |X| | |
     | |O| | |O| | |
     | |X|X| |X| | |
     | |X|O| |O| | |
     ---------------
      0 1 2 3 4 5 6

    Ask O what it thinks of its situation at 4-ply and below:

    O's move (-N for an N-ply hint):  -4

     Scores:
     0      1      2      3      4      5      6  
     0.0    0.0    0.0    0.0    0.0    0.0    0.0      

    O's move (-N for an N-ply hint):  -3  (or -2)

     Scores:
     0      1      2      3      4      5      6  
     50.0   50.0   50.0   0.0    50.0   50.0   50.0      

     because, in this case, it only sees the immediate threat.
  



Submission    Be sure to submit your CS5App.java file under homework 11, problem 1.




Extra Credit

For extra credit this week, create a more "intelligent" evaluate method in your Player class, called tournamentEvaluate.

tournamentEvaluate should return 0.0 on boards where the player has lost, but can return any positive score for other boards. The idea is to provide more information than just the generic score of 50.0 in the most common case -- when there is no clear winner or loser. Note that your tournamentEvaluate should not try to "lookahead" further -- rather, it should simply do a "better" job of deciding which boards favor which players.

Here are some possibilities to consider as you write tournamentEvaluate: There are many more possibilities -- and lots of ways to tweak the weighting of different factors when evaluating board positions.

To judge your tournamentEvaluate method, we will run a tournament of the Players that people submit for this extra credit. You will receive some credit (3 points), as long as your tournamentEvaluate method does _at least_ as well as the simple evaluate method described in the Player class. Additional credit (up to 15-20 points) will be awarded according to how your Player does in the round-robin tournament.

Submission    For this extra credit, submit your Hw11Pr1 code including tournamentEvaluate . Be sure to leave your code so that the basic, default behavior is to use the normal evaluate. We will set up a separate file with all of the tournament entries.

Additional Extra Credit!

You might want to look further ahead than 4 moves! For this extra credit, extend the connect-four Player class to allow the user to choose an N-ply lookahead for any choice of N. You will need a plyN method, and a special interface to let the user get the N-ply scores for any N (however you'd like to do it is OK). You should include a way to print the N-ply lookahead scores, too, from the human interface so that we can check it! There is an example board on the last slide of the handouts whose scores vary up to ply 8 or more... .

Warning: as you might expect, the program slows down as N gets larger. N=42 will solve the game completely, but it also won't finish...