Program 3, Public Goods Game

Short Description:

Write a program which, given the input for an instance of the Public Goods Game produces either an error message for bad input or prints a summary of the results and the name of the winner.

Goals

When you finish this homework, you should:

Formal Description

The Public Goods Game is one of many games studied in the fields of Operations Research and Business. These are not traditional games, but simple games that allow the study of human behavior. You will be writing a program to assist a researcher using this game in a study.

The Public Goods Game is a multiplayer game. In this case there will be three players. Each player is given n tokens. Each player selects to place g tokens in a public pot and keeps the rest. The pot is multiplied by a factor f to form the "public good". The public good is divided evenly between all players. Each player adds their portion of the public good to their remaining tokens to achieve a final score. The player with the highest score wins.

For example consider a game where the players were each given 100 tokens, the factor is 1.1, player A contributes 10 to the pot, player B contributes 20 and player C contributes 0. The pot is (10 + 20 + 0) * 1.1 = 33. Each player receives 11 tokens from the public good. At the end A has a score of 11 + 90 = 101, B a score of 91 and C a score of 111. C wins the game.

The input to your program is coming from an external source such as a web page so you do not need to prompt for input. Unfortunately, the data source is not incredibly reliable, so while all of the data fields will be present and of the proper data type, it is highly likely that the data will not be in the correct range. Your first task is to validate the input to assure all data is in the correct range. Incorrect data should be identified and an error message produced.

The input format for your data is as follows:

Your program should only produce one error message, corresponding to the first error found processing the input in order. For example if the number of tokens per player is more than 100, this should be identified and the program should exit without producing additional error messages.

If there are no errors, you should produce the following table:

Player    Pot   Kept  Total
     A  XXXXX  XXXXX  XXXXX
     B  XXXXX  XXXXX  XXXXX
     C  XXXXX  XXXXX  XXXXX
Where the first column is six spaces wide, all others are 5 spaces wide and there are two spaces between each column. The data should be padded with blanks and right aligned. The table should be proceeded and followed by a blank line.

Finally you should print the results of the game. This should be a single line selected from the appropriate line below:

The output for the above input is :


Player    Pot   Kept  Total
     A     10     90    101
     B     20     80     91
     C      0    100    111

C wins with 111.

Discussion

Testing

I have provided a simple script to test your program for some of my input. You may use this program if you wish. If your program is named myprog.cpp or myprog.C you can type
~dbennett/SharedFiles/130/p3/runTests myprog
Note there is no .C or .cpp on the end. This will build an executable called myprog. If it is successful, it will run a number of tests. These tests are by no means exhaustive, I will be running others when I test your code.

My program was called myprog.cpp and here is a transcript of running the test code.

~dbennett/SharedFiles/130/p3/runTests myprog
make: 'myprog' is up to date.

Test Case: case1.in ... Success
Test Case: case2.in ... Success
Test Case: case3.in ... Success

Success, all tests have run correctly

I stuck a clear at the top of the test driver, so it will clear your screen.

make: 'myprog' is up to date is normal if the program has already been compiled in your directory.

Required Files

A single source code file.

Submission

Submit your program to the D2L folder Program 3