Assignment 2: The Prisoner's Dilemma ==================================== Your task for this assignment is to write a program that lets a person play the famous `Prisoner's Dilemma `_ game against the computer. The user enters how many rounds of the game they would like to play, and then they play that many rounds of the `Prisoner's Dilemma `_ against the computer. At the end, the total score for the user and computer is printed, along with a message saying who won (or if it's a tie). The Prisoner's Dilemma ---------------------- Imagine you, player A, and a friend, player B, have just robbed a bank and have been caught by the police. You are interrogated at the same time but in different rooms, so neither of you know what the other player says. Both of you can make one of two different "moves" in the interrogation: - **Stay silent**, i.e. don't say anything to the police about your friend's involvement in the robbery. Traditionally, this is called **cooperating**. - **Betray your friend** by telling the police your friend was involved. Traditionally, this is called **defecting**. Since there are two players, A and B, and they each have two possible moves, *cooperate* or *defect*, there are 4 possible outcomes: - A cooperates (i.e. stays silent), B cooperates (i.e. stays silent) - A gets 1 year in jail - B gets 1 year in jail - A defects (i.e. betrays B), B defects (i.e. betrays A) - A gets 2 years in jail - B gets 2 years in jail - A cooperates (i.e. stays silent), B defects (i.e. betrays A) - A gets 3 years in jail - B gets 0 years in jail - A defects (i.e. betrays B), B cooperates (i.e. stays silent) - A gets 0 years in jail - B gets 3 years in jail Think about what you would do in this game. The best possible outcome for *you* is to spend 0 years in jail, and the only way that can happen is if you betray your friend. But if your friend thinks the same way, then you both end up betraying the other and get 2 years in jail each. This is worse than if you had both kept silent, in which case you would have only gotten 1 year in jail! So you might decide that staying silent is better. But this is risky: if your partner betrays you while you stay silent, you get 3 years in jail --- the worst possible outcome for you! The `Prisoner's Dilemma `_ is a well-studied problem, and different strategies have been shown to work well in certain situations. For example, in computer tournaments where programs play against other programs in a series of games, the strategy known as *tit- for-tat* works surprisingly well. *Tit-for-tat* has two rules: #. Cooperate on the first move. #. For every move after the first move, *tit-for-tat* chooses whatever the opponent chose on their *previous* move. In a tournament consisting of many games *tit-for-tat* performs extremely well, and, overall, is able to beat far more complex strategies. Error Handling -------------- If the user enters invalid data, then use the ``cmpt::error()`` function from the ``cmpt_error.h`` file to raise an helpful error message. The exact validation rules for this assignment are as follows: - The number of rounds the user plays must be an integer that is greater than 0. - When it's the user's turn to play, their only choices are the lowercase character 'c' (for cooperate) and the lowercase character 'd' (for defect). Any other input is an error. The Computer's Strategy ----------------------- For this assignment, the computer must use the *tit-for-tat* strategy described above. Sample Run ---------- :: --------------------------------------- Welcome to the Prisoner's Dilemma Game! --------------------------------------- How many rounds would you like to play? 3 Okay, 3 rounds it is. Round 1: Cooperate (c) or defect (d)? c You chose cooperate. The computer chose cooperate. You get 1 penalty point. The computer gets 1 penalty point. Round 2: Cooperate (c) or defect (d)? d You chose defect. The computer chose cooperate. You get 0 penalty points. The computer gets 3 penalty points. Round 3: Cooperate (c) or defect (d)? d You chose defect. The computer chose defect. You get 2 penalty points. The computer gets 2 penalty points. Game over! You have a total of 3 penalty points. The computer has a total of 6 penalty points. You win! Submission ---------- Put your program in a single file called ``a2.cpp``. It should include only these files: ``iostream``, ``string``, and ``cmpt_error.h``. Do **not** include any other files. When it's ready to be submitted, compress ``a2.cpp`` into a zip-file archive called ``a2_submit.zip`` by typing the following command in a Linux console window:: $ zip a2_submit.zip a2.cpp Don't type the ``$`` --- that's the command prompt. Do **not** compress your program in any other way --- only ``.zip`` please! Your ``.zip`` file should only contain ``a2.cpp``. Do **not** put your makefile, ``cmpt_error.h``, or any other file in it. Include the name of the course (*CMPT 130*), the semester (*Fall 2017*), and your name and student number in comments at the top of the file. Citing Help ----------- If you received help from any person (other than yourself), book (such as the textbook), website (such at the course notes), etc., then you **must cite this help in a comment at the top of your program**. If you do not include such a comment, we will assume the program is entirely your own work. Not citing help (even if you accidentally forget) is considered academic dishonesty and will be dealt with seriously. Marking Scheme -------------- The marking scheme for this assignment is on Canvas_. If your program does not compile correctly, your mark for the entire assignment will be 0. You may also receive 0, or a very low mark, if your assignment compiles but runs in some way that is difficult to judge if it works correctly.