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:
- Developed a longer program.
- Developed a program which employs boolean variables and boolean expressions.
- Developed a program which uses if statements to make decisions.
- Developed a program which validates input.
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:
- An integer n, representing the maximum number of tokens each player has.
- Example: 50
- This integer must be at least 1 and no more than 100.
- If the number (n) is too low,
- the following error message should be produced: Error: tokens per player is n, which is less than 1.
- Where n is replaced with the actual data.
- If the number (n) is too high,
- The following error message should be produced.
- Error: tokens per player is n, which is greater than 100.
- Where n is replaced with the actual data.
- Three comma separated integers, representing the number of tokens player A, B and C place in the pot.
- Example: 10,20,30
- These integers must be between 0 and n (defined above) inclusive.
- If an integer is less than 0, the following error message should be produced:
- Error: player X contributed y tokens , which is negative.
- Where X is the letter for the player (A, B, or C)
- and y is the number of tokens placed in the pot.
- if an integer is greater than n, the following error message should be produced:
- Error: player X contributed y tokens , which is more than the maximum n.
- Where X is the letter for the player (A, B, or C)
- and y is the number of tokens placed in the pot.
- and n is replaced with the maximum tokens for this game.
- Finally, a floating point number f representing the multiplier.
- Example: 2.8
- This number must be between 1 and 3.
- If f is too low, the following message should be produced
- Error: the multiplier is f.ff, which is less than or equal to 1.
- Where f.ff is replaced with the value of f.
- If f is greater than or equal to 3, the following message should be produced
- Error: the multiplier is f.ff, which is greater than or equal to 3.
- Where f.ff is replaced with the value of f.
- An example input would be
100
10,20,0
1.1
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:
- All are tied at T.
- X and Y are tied at T.
- X wins with T.
- Where X and Y are the letters corresponding to the winners
- and T is the winning score.
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
- This program is longer than previous programs. My program is over 100 lines, with very few comments.
- This program requires a number of if statements.
- Use boolean variables to simplify your logic.
- Your if statements should not be nested any deeper than 3 levels.
- Your identifiers should be constructed properly, with meaningful names.
- You should document your code with comments.
- Your output should be organized, easy to read, and make
appropriate use of white space.
- Any numbers with a fixed meaning should be stored as a constant.
- You should only perform any given computation once, store the results.
- Your program should only have one return statement at the end of the code.
- Your program may not use exit, break, continue or goto.
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