COMP 1006 - Assignment 10

Due : Wednesday April 10rd, 2013 at 9pm

Submit a single zip file called userID.assignment10.zip to cuLearn with all of your solutions to the following problems. Submit early and submit often. Here, userID is your Carleton userID number.


Rock-Paper-Scissors: In this problem, you will create classes of players for the rock-paper-scissors game.

The players will be children of the following (partially defined) class:

public abstract class Player implements Comparable{
   public String name;   // name of player
   public    int id;     // identifier for the player
   protected int wins;   
   protected int losses;
   protected int ties;

   public abstract String play(Player opponent);
   // returns one of "rock", "paper", or "scissors"

   public void update(String myGesture, 
                      String opponentGesture,
                      Player opponent);
   // this method will update the player's stats 
   // (wins, losses, ties) based on the gestures (inputs)
   // for a recent game played against opponent (also input)

   public Player(String name, int id){...}
   // constructor that initializes player's name and id

You will need to fill in the code for the constructor and the update methods for the Player class. You can add other "hidden" attributes and methods as you wish (Note: if you add things to Player, be sure it is something that ALL children classes will also use). You will also need to implement three classes that extend the Player class:

public class SimplePlayer extends Player{...}
// A SimplePlayer will always play the same 
// gesture (either rock, paper, or scissors)
// in every game it plays, regardless
// of who its opponent is.  The gesture is 
// randomly chosen when the SimplePlayer is created.

 
public class RandomPlayer extends Player{...}
// A RandomPlayer will always play a random
// gesture (rock, paper, or scissors) in 
// every game it plays, regardless of who 
// its opponent is.  


public class SmartPlayer extends Player{...}
// A SmartPlayer will try to use past knowledge
// of games played against a particular 
// opponent when playing them again.

You can add any hidden attributes and methods to the children classes as you wish.


Tournament: Create a class called Tournament. This class should simulate a tournament of rock-paper-scissors games between a collection of Players (children of Players). The Tournament class should have a static method

public static void playTournament(String filename){...}
that plays the tournament.

The playTournament() method should read in data from the file (in the current directory, called filename). The file is a text file that has the following format:

num
name id kind-of-player
name id kind-of-player
...
name id lind-of-player
That is, it starts with a line with a single integers which tells it how many more lines are in the file. The next num lines are the name, id and type of the num players. For example, a file might look like
4
cat 123 Random
dog 231 Smart
eel 410 Random
cod 876 Simple 

After reading in the player data, the method simulates a tournament in two stages: the round robin stage happens first where each player plays every other player five times.

After the round robin stage happens, you should sort (use the sort() method from Collections) the players based on the players score, defined by

score = (3.0*wins + ties)/(wins+losses+ties)

After the players are sorted, you should display to the screen a summary of the round robin stage. (That is, you should print out all the players in the tournament (their name) along with their stats for this stage. They should be printed in the sorted order. All information for a single player should be on the same line. The wins, losses, and ties information should be nicely aligned in columns. After they are sorted, the elimination round begins. In the elimination round, the player with the top score plays the player with the lowest score (the loser is eliminated); then the player with the second highest score plays the player with the second lowest score (the loser is eliminated); etc. (You can assume that there is an even number of players in the tournament.) After half of the players are eliminated, we repeat the elimination based on the remaining players. (You can assume that there is still en even number of players in this round). This continues until there is only one player left, who is the winner of the tournament.

The playTournament() method should display on the screen the winner of the tournament (along with the stats of the winning player).

Note: Your Tournament class should have the following methods:

public static void playSingleGame(Player p1, Player p2){...}
// this method plays a single game between two players
// this method will call play() for each player and then
// call update() for each player (after each call to play() is made)


public static void playEliminationGame(Player p1, Player p2){...}
// this method plays keeps games until one of the 
// players beats the other. (they might tie several times
// before this happens).  The loser is eliminated from
// the tournament.
// If the players tie too many games in a row (say 100), then 
// it is safe to assume that both players are simple players
// and are each picking the same hand gesture.  In this case,
// the player with the higher score will be declared the winner.

Note: I will consider implementing the round robin part of the tournament as completing this class. The elimination round will be considered as bonus.


Submit your Player.java, SimplePlayer.java, RandomPlayer.java, SmartPlayer.java, and your Tournament.java files in your assignment zip file.