CS140 Lab 4 - Scoring Duplicate Bridge Games


Duplicate Bridge is, plain and simple, the best card game in the world. At a high level (sufficient for this lab), here's how it works.


An Example Game -- Score Files

We are going to deal with scoring files from bridge games. The files will only be for players of one direction (either NS or EW). For the purposes of the lab, it does not matter which direction. The files have the following format. Suppose there are n pairs. They will be numbered 1 through n. The first n lines of the file will give the names of the pairs preceded by the pairs' numbers. Note these do not have to be in order. Line n+1 will consist solely of the word SCORES. The rest of the lines will each have n scores -- these are the scores of each of the pairs on each of the boards. If a score is "PASS", then the score is really zero. If the score is "----", then the pair did not play that board. For example, look at the file scores/Easy_Test.txt.

   4 Tiger Woods - Phil Mickelson
   2 Gene Simmons - Richard Simmons
   1 Bill Gates - Warren Buffett
   3 Cher - Madonna
SCORES
-430 -400 -520 -1100
-180 -130 -130  PASS
-100  650  650  ----

Here is a fictious game composed of four pairs who play a three-board game. First, note that Bill Gates/Warren Buffett are pair 1, even though they are not the first pair listed. They scored -430 on the first board, -180 on the second, and -100 on the third. Tiger Woods/Phil Mickelson are pair 4, and they only played the first two boards. They went for a big negative number on board one, and scored 0 on board two.


Scoring

You can score bridge games in a variety of ways. For example, you can simply add up the scores of each pair on each board, and compare them. For example, in the above example, Bill & Warren would have a total score of -710 and an average score of -710/3 = -236.667. Tiger & Phil would have a total score of -1100 and an average score of -1100/2 = -550 (remember, they only played two boards).

Unfortunately, since we all do not play the same boards, and sometimes we don't play the same number of boards, total score is a bad metric. Instead, each score may be converted into a number of matchpoints. The number of matchpoints that a pair gets on a board is equal to the number of pairs that the pair scored better than on that board. For example, on board #1 above, Bill & Warren scored better than two other pairs (Cher & Madonna and Tiger & Phil). Therefore, they receive two matchpoints for that board. Tiger and Phil receive zero since they didn't beat anyone.

To be specific, a pair scores one matchpoint for each other pair that they beat on a board, and a half a matchpoint for each pair that they tie. For example, on board #3, Cher/Madonna and the two Simmons each get 1.5 matchpoints because they beat one pair and tied one pair.

Thus, a second way to score a bridge game is to total the matchpoints for each pair on each board. For example, on these four boards, Bill/Warren receive 2 matchpoints on board one, and zero on boards two and three -- that's 2 total matchpoints. Gene/Richard receive 3 matchpoints on board one, and 1.5 each on boards two and three. Thus, they have six total matchpoints.

Unfortunately, again, total matchpoints is not the best metric for comparing scores, because there are times where pairs play different numbers of boards, and, worse yet, sometimes the number of pairs that play different boards also differs. This happens in our example, because Tiger/Phil only play two boards. You would like to have a metric that, regardless of the number of pairs and boards, gives you an idea of well you did. The way to do that is to present a pair's score on a board as a percentage. This is a percentage of the pairs that the pair beat, and is equal to the number of matchpoints that the pair receives on the board, times 100, divided by the total number of players who played that board minus one.

For example, Gene/Richard beat all three other pairs, and thus get 100% for that board. Bill/Warren beat two out of three and thus get 66.67%. On board three, Bill/Warren beat no one, and thus get zero percent. Cher/Madonna and the Simmons each got 1.5 out of 2.0 total matchpoints, for 75%.

Using percentages, you may score a game by the average percentage of a pair on each board. Thus, in the example above, Bill/Warren would score (66.67+0+0)/3 = 22.22% for the game. Tiger/Phil would score (0+100)/2 = 50.00%. Gene/Richard would score (100+50+75)/3 = 75.00. And finally, Cher/Madonna would score (33.33+50+75)/3 = 52.78.


Simpscore.c

Your job on this lab is to write simpscore.c, which takes one command line argument -- a score file in the above format. It reads in this score file, and then prints out the following output. For each pair, it prints out: It does this with one line per pair, and the order should be ascending by pair number.

Here is simpscore running on our example:

UNIX> cd /home/plank/cs140/Labs/Lab3
UNIX> simpscore scores/Easy_Test.txt
      -710  -236.67        2.0      22.22  3 Bill Gates - Warren Buffet
       120    40.00        6.0      75.00  3 Gene Simmons - Richard Simmons
         0     0.00        4.0      52.78  3 Cher - Madonna
     -1100  -550.00        3.0      50.00  2 Tiger Woods - Phil Mickelson
UNIX> 
Note, you can pipe the output to various incarnations of sort to sort by the various types of total score. Below we sort by total score and by average matchpoints:
UNIX> cd /home/plank/cs140/Labs/Lab3
UNIX> simpscore scores/Easy_Test.txt | sort -nr
       120    40.00        6.0      75.00  3 Gene Simmons - Richard Simmons
         0     0.00        4.0      52.78  3 Cher - Madonna
      -710  -236.67        2.0      22.22  3 Bill Gates - Warren Buffet
     -1100  -550.00        3.0      50.00  2 Tiger Woods - Phil Mickelson
UNIX> simpscore scores/Easy_Test.txt | sort -nr +3
       120    40.00        6.0      75.00  3 Gene Simmons - Richard Simmons
         0     0.00        4.0      52.78  3 Cher - Madonna
     -1100  -550.00        3.0      50.00  2 Tiger Woods - Phil Mickelson
      -710  -236.67        2.0      22.22  3 Bill Gates - Warren Buffet
UNIX> 

If the score file is in the incorrect format, simpscore should exit with an error message denoting the offending line of the file. Think of what would consitute a bad file -- make sure that you handle all sorts (e.g. missing pair numbers, bad pair numbers, no names, not enough scores on a score line, non-numbers (or PASS or ----), etc.).

Score files will have no more than 100 pairs, and no more than 40 boards.

There is a working executable in /home/plank/cs140/Labs/Lab3/simpscore. There are also score files in the directory /home/plank/cs140/Labs/Lab3/scores -- these are scores of real games played in Knoxville/Maryville recently (although I've anonymous-ized the names). They are all in the proper format.

Make sure the output of your program matches the output of the /home/plank/cs140/Labs/Lab3/simpscore character for character. They may differ in error statements, but for correct input files, they should be exactly the same.

You should use the fields library for this program.


Hints

I would have a struct for each pair, consisting of the following fields: To start with, I would allocate an array of 100 pointers to these structs. When I read a pair name, I would allocate a struct using the pair's number (minus 1), and initialize all the fields. Then when I read in the score lines, I update all the fields of the pairs who played the board. Finally, when I am done with the input file, I'll print out all the information in the correct format.

Calculating matchpoints may seem like a challenge to you. What I did was the following -- when reading a line of scores, I would allocate an array that holds all the scores for a board. Then, for each pair who played the board, I'd calculate their matchpoints by traversing the array, and adding 1 whenever I saw a score worse than the pair's score, and adding .5 whenever I saw a tie. Finally, I subtracted .5 (because I added .5 when I saw the pairs score in the array). You'll learn later that this is not the most efficient way to do this, but it will suffice for now.


Bridge Links

If you are interested, the American Contract Bridge League (ACBL) has all sorts of bridge information, etc, including free software that teaches you how to play. Their web site is http://www.acbl.org.

The Knoxville Association of Bridge Clubs also has a web site at http://www.discoveret.org/bridge -- they have teachers there and games for newcomers to the game of bridge. If you are the type of person who likes cards and competition, give it a try!