Week 9 – Auto-Adjusting Difficulty Levels
Welcome to my ninth Senior Project blog. This week I finally finished the main part of my project, which was getting the game to automatically change difficulty levels. I’ll be spending the rest of my time working on my presentation, as well as continuing to test out the game to see if any bugs need to be fixed or if there are any changes that I feel would improve it. In this blog post, I will go over the process for updating the difficulty level based on the user’s prior wins and losses, as well as going through the main portion of the code that handles this.
How It Works
Every time a new game is started, I record the result of the previous game. In addition to that, I will be adding a new method, updateDifficulty(), which will change the difficulty level if needed, as well as keep track of this in stats.txt. This method will look at the last five games the user has played. If the user has played fewer than five games at the current difficulty level (results from previous difficulty levels shouldn’t affect the current difficulty level’s results), the method does nothing. Otherwise, it will count the number of wins in those last five games. At 0 or 1 wins, the difficulty level will decrease. At 2 or 3 wins, it will stay the same. And if the user wins 4 or 5 out of their last five games, the difficulty level will increase.
The method above is the code that handles updating the difficulty level that a user is playing at. It is called in the newGame() method, so updateDifficulty() will be called between every game. The method starts off by opening the text file and reading into a String, record. If record is shorter than 5 characters long, then the user hasn’t played 5 games yet, and the method stops. If it is longer than 5 characters, last5 is another String used to store the last five characters of record, with [-5:] being used to get them.
The next thing to check is if all of these games were played at the same difficulty level. If ‘I’ or ‘D’ are in found in the String, last5, then the user has played fewer than five games at the current difficulty level, and I want to move on without changing the difficulty. At this point, I am done reading the file, so I close the file reader on line 152.
At this point, I know that the user has played at least 5 games without changing difficulty levels, so I can check whether or not to increase or decrease the level. I start by counting the number of wins, into a variable called wins. If wins is greater than or equal to four (meaning the user has 4 or 5 wins in their last five games played), then I will increase the difficulty level, as well as reopen the text file to append an ‘I’ in order to track that the difficulty has been increased. If wins is less than or equal to 1 (so 0 or 1 wins in the last five games), the opposite happens. I decrease the difficulty, and reopen the text file to append a ‘D’ to show that the difficulty has been decreased.
I’ll be spending the rest of my time on this project either working on my presentation, or continuing to play the game to find and fix any bugs, or to make improvements as needed. For example, I am currently trying to have the difficulty adjust at four games (instead of five) if the user wins or loses them all. I will also try to find a way to make this game playable for others, preferably without needing to install Pygame.