Need help with programming monopoly in R. All files attached have instructions
Stats 102A - Homework 4 Instructions: Monopoly Stats 102A - Homework 4 Instructions: Monopoly Homework questions and instructions copyright Miles Chen, Do not post, share, or distribute without permission. Homework 4 Requirements You will submit three files. The files you submit will be: 1. 102a_hw_04_script_First_Last.R Your R script file containing the functions you write for the homework assignment. Write comments as necessary. 2. 102a_hw_04_output_First_Last.Rmd Take the provided R Markdown file and make the necessary edits so that it generates the requested output. The first line of your .Rmd file should be to source the R script file you wrote. 3. 102a_hw_04_output_First_Last.pdf Your output PDF file. This is the primary file that will be graded. Make sure all requested output is visible in the output file. Failure to submit all files will result in an automatic 40 point penalty. Academic Integrity At the top of your R markdown file, be sure to include the following statement after modifying it with your name. “By including this statement, I, Joe Bruin, declare that all of the work in this assignment is my own original work. At no time did I look at the code of other students nor did I search for code solutions online. I understand that plagiarism on any single part of this assignment will result in a 0 for the entire assignment and that I will be referred to the dean of students.” If you collaborated verbally with other students, please also include the following line to credit them. “I did discuss ideas related to the homework with Josephine Bruin for parts 2 and 3, with John Wooden for part 2, and with Gene Block for part 5. At no point did I show another student my code, nor did I look at another student’s code.” Recommended Reading: a. S3 (not necessary for this HW): https://adv-r.hadley.nz/s3.html b. R6: https://adv-r.hadley.nz/r6.html Monopoly Board game simulation For this homework assignment, you will create a simulation of the classic board game, Monopoly. The goal is to find out which spaces on the board get landed on the most. You will not simulate the entire game. You will simulate only the movement of pieces, and will keep track of which squares the pieces land on. You can familiarize yourself with the game board. (Taken from Amazon’s product page.) http://ecx.images-amazon.com/images/I/81oC5pYhh2L._SL1500_.jpg Official rules https://www.hasbro.com/common/instruct/monins.pdf 1 https://adv-r.hadley.nz/s3.html https://adv-r.hadley.nz/r6.html http://ecx.images-amazon.com/images/I/81oC5pYhh2L._SL1500_.jpg https://www.hasbro.com/common/instruct/monins.pdf Rules for movement The Monopoly Board is effectively a circle with 40 spaces on which a player can land. Players move from space to space around the board in a circle (square). The number of spaces a player moves is determined by the roll of 2 dice. Most often, the player will roll the dice, land on a space, and end his turn there. If this were the entire game, the spaces would have a uniform distribution after many turns. There are, however, several exceptions which provide the primary source of variation in space landing. Go to Jail One space, “Go to Jail” sends players directly to jail (there is a jail space on the board). This space never counts as having been ‘landed upon.’ As soon as the player ‘lands’ here, he is immediately sent to jail, and the jail space gets counted as landed upon. This is the only space on the game board that moves a player’s piece. The count of how often this space is landed on will always be 0. Rolling Doubles If a player rolls doubles (two of the same number), the player moves his piece, and then gets to roll the dice again for another move. However, if a player rolls doubles three times in a row, he is sent directly to jail. (The third space that the player would have ‘landed on’ does not count, but the jail space gets counted as landed on.) Card Decks: Chance and Community Chest A player can land on a “Chance” or “Community Chest” space. When a player lands on these spaces, he draws a card from the respective deck and follows its instructions. The instructions will sometimes give money to or take money from the player with no change in the player’s position on the board. Other times, the card will instruct the player to move to another space on the board. The list of cards that can be drawn from each deck is provided. There are nine cards in the Chance deck that move the player’s token. There are two cards in the Community Chest deck that move the player’s token. All other cards do not move the player’s token. For the sake of this simulation, you only need to program actions for the cards that move the tokens. There is no need to do anything for ‘get out of jail’ or any of the other cards. A card may say ‘move to the nearest railroad’ or ‘move to the nearest utility’ or even ‘go to property . . . ’. In these cases, the player always moves forward. So if a player is on ‘Oriental Avenue,’ the nearest railroad is ‘Pennsylvania Railroad’ and NOT ‘Reading Railroad.’ For the sake of this simulation, the Chance and Community Chest get counted as landed on when the player lands on the Chance or Community Chest space. The player may also generate another count if the card moves the player to another space on the board. In those cases, a tally is counted for the Chance/Community Chest space, the token is moved, and then a tally is counted for the space where the player ends his turn. Jail Jail is the most complicated aspect of this simulation. If a player lands on space 11 (Jail) simply from rolling the dice, he is not in Jail. He is ‘just visiting’ jail. He generates a tally for landing on jail, and his play continues on as normal. A player can be sent to jail in several ways: • he rolls doubles three times in a row; • he lands on the “go to jail” space; • he draws a card that sends hims to jail. As soon as the player is sent to jail, his token moves to jail (space 11), he generates a count for landing on jail, and his turn ends immediately. 2 On the next turn, the player begins in jail and the player will roll the dice. If he rolls doubles on the dice, he gets out of jail and moves the number of spaces the dice show. However, even though he rolled doubles, he does NOT roll again. He takes his move out of jail and his turn ends. If he does not roll doubles, he stays in jail. A player cannot stay in jail for more than three turns. On the third turn he begins in jail, he rolls the dice and moves the number of spaces the dice show no matter what. Play then continues as normal. For this simulation, each time a player ends his turn in Jail, a tally will be counted as having been ‘landed upon.’ There are more rules on jail that include paying a fee to get out early, or using a get out of jail free card. We will not implement those rules. We will simply simulate a ‘long stay’ strategy for Jail. This means that the player will never pay the fee to get out jail early. He will roll the dice and only leave jail if he gets doubles or it is his third turn in jail. The Assignment Your assignment is to implement the rules of Monopoly movement. You must use R6 to create an object class Player which will be used to keep track of a player. Part 1 You will first demonstrate that you have coded the rules by showing the output of the first 20 turns using the preset dice. Your output for this section should match the published results exactly. The output should be very verbose. It should announce the player roles, where the player moves, what spaces get tallies, if they rolled doubles, etc. Part 2 The next part is to run 1,000 simulations of a two-player game that lasts 150 turns. This is a total of over 3 hundred thousand tosses of the dice - 1000 games x 150 turns x 2 players + additional rolls if the player gets doubles. Your task is to keep track of where the players land. We ultimately want to build a distribution showing which spaces are most likely to be landed upon. Advance the tokens around the board according to the rules. Keep in mind the special situations involving the cards, jail, and rolling doubles. After 150 turns, reset the game and start over. Simulate 1000 games. Your final output will be two tables of the spaces on the board and their frequencies. Each table will show the space name, how many times the space was landed upon, and the relative frequency of landing on that space. The first table is arranged in descending order of frequency of landing. (Jail should be #1, Go to jail should be last.) The second table is arrange in the order of the spaces on the board. (Go will be #1, Boardwalk will be last.) Also print a bargraph showing the frequency of how often each space is landed on. You do not have to simulate or track money at all in this simulation. Starter Code For your convenience, I have created the necessary data frames for the game board, and the two decks of cards. I have also created several helper R6 classes for you: • PresetDice: You will use this for Part 1: showing the movement of the player for 20 turns • RandomDice: You will use this for Part 2: the simulation of 1000 games. • CardDeck: For creating the Chance and Community Chest decks. This reference class ‘shuffles’ the deck, and each time a player draws a card, it shows the next card drawn. When all the cards in the deck have been used, it ‘shuffles’ the deck again. Please take the time to read through these R6 class definitions. Understanding the definitions, and what the methods do will be helpful in completing the assignment. 3 Tips At first blush, the task may seem overwhelming. • Break the task into smaller manageable parts. • Start with a simulation that moves pieces around the board and keeps track of where they land. (I’ve done this part for you in my example code.) • Then add complexity one part at a time. Each time you add something, thoroughly test it to make sure it behaves the way you want it to. • The PresetDice reference class will be very helpful in your testing