In this mini-project, you will be implementing a web-based rock-paper-scissors game. You will be given starter code for a Flask app (the same technology you will be using in Project 2), and extending it to make your game.
Download the starter files, and extract them.
Set up a virtual environment and install the project dependencies:
$ python3 -m venv env
$ source env/bin/activate
$ pip install -r requirements.txt
$ pip install -e .
You can run the Flask app using the command:
./run.sh
Then, visit localhost:8000 in your browser to access the app. You should see a picture of a rock, with buttons to increment and decrement a counter.
To familiarize yourself with the code, complete the following tasks:
Change the project from a counter to a rock-paper-scissors game.
Use Markov chains to predict what the user is going to play next, by building a data structure similar to this, where the keys are a sequence of moves the user has previously made, and the values are the counts of the moves the user has made after that sequence.
{
# means after playing RR, human played R twice, P once, S once
"RR": { "R": 2, "P": 1, "S": 2 },
"SS": { "R": 1, "P", 2, "S": 0 },
...
}
Here is a walkthrough of how to build it. Suppose that the user has played the following sequence of moves:
RRPRRS
Based on this, we know that when the previous 2 moves were “RR” the next move was “P” one time and “S” one time, when the previous 2 moves were “RP”, the next move was “R”, and when the previous 2 moves were “PR”, the next move was “R”. Therefore, the data structure we could build is:
{
"RR": { "R": 0, "P": 1, "S": 1 },
"RP": { "R": 1, "P": 0, "S": 0 },
"PR": { "R": 1, "P": 0, "S": 0 }
}
Then, when making a move, the AI looks at the last 2 moves the player made, and plays the move that would defeat the most likely move by the player. In the example above, if the last 2 moves were PR, the AI would play P.