Question: Old MathJax webview python only Introduction In this assignment you will use your newly developed skills to provide the back-end functionality of a minesweeper game.

Old MathJax webview

Old MathJax webview python only Introduction In this assignment you will use

your newly developed skills to provide the back-end functionality of a minesweeper

game. In minesweeper, the object of the game is to clear a

minefield, using logic to determine which grid spaces contain a bomb and

which do not. For more information on how it is played, see

link. You have been provided with a simple graphical user interface (GUI)

that will display a window and allow for interactive elements. When you

first launch the project, however, the game will not function as expected.

Your job is to complete all the incomplete functions in game_logic.py .

Once each of these works, minesweeper will be fully playable! Unless you

attempt the bonus problem, you will never need to write or modify

ANY code outside of game_logic.py . Just follow the instructions and requirements

for each function, and you do not need to understand the GUI

python only

Introduction In this assignment you will use your newly developed skills to provide the back-end functionality of a minesweeper game. In minesweeper, the object of the game is to clear a minefield, using logic to determine which grid spaces contain a bomb and which do not. For more information on how it is played, see link. You have been provided with a simple graphical user interface (GUI) that will display a window and allow for interactive elements. When you first launch the project, however, the game will not function as expected. Your job is to complete all the incomplete functions in game_logic.py . Once each of these works, minesweeper will be fully playable! Unless you attempt the bonus problem, you will never need to write or modify ANY code outside of game_logic.py . Just follow the instructions and requirements for each function, and you do not need to understand the GUI code. Project Setup The project should work out of the box, but in case you have errors, this is the working configuration used to develop this assignment: . Python 3.7 . arcade 2.5.6 PyCharm may ask you to install arcade, as it is included in requirements.txt. If it does, approve the installation, as arcade is the game library that this project . uses. Running main.py should open a window, as below: It's important to realise that although the buttons are presented as a 16x16 grid of buttons, the program actually runs on a standard one-dimensional list of length 256. For your reference, an Excel file grid.xlsx has been included in the project to help you visualise how indices 0-255 map onto a 16x16 grid. Part 1 [10 marks] def define_bomb_positions (positions: Set, number, possibilities: List): This is a recursive function! In this function, your job is to return a set of bomb positions that the game will use to populate the mine field. The function has three parameters: 1. positions This parameter holds the set that you will eventually return. It consists of n bomb indices, ranging from 0-255 (the valid grid positions). 2. number This parameter is the number of bomb positions that you are expected to generate. In a standard 16x16 grid of minesweeper, there are 40 bombs, and this is the number that the game provides. 3. possibilities This is simply a list containing the values 0-255 Your job is to take a random selection of 40 indices from the list of possibilities and place those into the positions set. A set requires that all values are distinct, so don't include any duplicates! The other, major constraint is that you must solve this using recursion instead of regular loops. Use of any looping mechanism other than recursion will result in a severe deduction, even if you get the right answer. Yes, this is hard, annoying, and arbitrary, but welcome to university e. Part 2 [15 marks] def number_values (grid: List, bomb_positions: Set): In this function, you will do all the work of determining the number value for each cell position that is not a bomb. In minesweeper, each numbered cell indicates how many bombs are adjacent (see below). 1 1 1 1 1 1 1 1 2 A square's number value means that it touches a bomb, but only that many. For example, a value of 2 means that the cell touches only two bombs. The function has two parameters: 1. grid This is the underlying 256 length list that represents the 16x16 grid. 2. bomb_positions These are the bomb positions that you calculated in Part 1. Your job is to calculate the value of every cell in the grid that is not a bomb. For every cell, you must count how many bombs are in the adjacent cells. The count is the value that should be recorded for that index in the grid list. The following image shows three examples of the directions you must investigate. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33. 24 35 26 37 22 29 11 A2 A AA AC AC 17 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 You will need to create an algorithm that is capable of recognising that corner cells only have 3 potential neighbours, edge cells have 5 potential neighbours, and middle cells have 8 potential neighbours. You must not just hardcode this for each of the 256 cells; put some thought into how to do this algorithmically. In the end, the provided grid array will have a value from 0-8, unless it has a bomb and then should have the character b Part 3 [15 marks] def find_blanks (grid: List[int], blank_index: int) -> List[int]: This is a recursive function! In a game of minesweeper, if you click a cell that has a bomb-adjacency value of 0, it appears as a blank square. Then, the game automatically reveals other adjacent blank squares. The result is something like in the picture below: 1 1 1 1 Your job is to find all the adjacent blank squares (if any!) from a given initial blank index. The function has two parameters: 1. grid This is the underlying 256 length list that represents the 16x16 grid. 2. blank_index This is the initial click that revealed a blank square. Your search starts from this point. Like with many recursive functions, you will need to create a new function helper that does the actual recursion and can have some extra parameters. You will need to following two extra parameters: 1. to visit Neighbouring cells that are yet to be checked. 2. visited Neighbouring cells that have already been checked. 3. blanks A list of all the adjacent blank cells. Using recursion, your solution should follow this algorithm: 3. blanks A list of all the adjacent blank cells. Using recursion, your solution should follow this algorithm: if the length of to visit is e: return blanks pop (remove) a neighbouring index off to visit and add it to to visited if the cell value is @: add cell to blanks get all of its valid neighbours in directions N, S, E, W add any neighbours not already in to visit or visited to to visit repeat the process with updated values else: repeat the process with the updated to visit Part 4 [5 marks] def check_win_condition (pressed_buttons: List[bool), bomb_positions: Set[int]): In this function, you will determine if a game has been won. In minesweeper, the winning condition is that you have revealed all the non-bomb squares. To protect you from dealing directly with GUI button objects, this function just takes a list of booleans, structured in the same way as the grid used in previous functions. All you have to do is determine whether all non-bomb positions are True, signifying that all cells have been revealed without exploding a bomb. This function takes two parameters: 1. pressed_buttons A list of 256 boolean values representing the press state of all the grid buttons. 2. bomb_positions These are the bomb positions that you calculated in Part 1. The function should return True if the win condition has been met, and False otherwise, Part 5 [5 marks] def write_score(name: str, date: datetime.date, seconds: int, is_win: bool): In this function, you simply need to write the provided scores to the CSV file high_scores.csv . The CSV file is more of a log than just high scores, as it includes information about all played games, regardless of the win state. The function has four parameters: 1. name The player's name (this is set in main.py ) 2. date The date that the game was played 3. seconds The game duration in seconds 4. is_win Whether or not the game was won You need to take these values and append a single string, in CSV format, to the high_scores.csv file. The file comes provided with some examples. Part 6 [10 marks] def get_top_five_scores(): At the end of each game, the top five scores are printed to the python console. Your job is to read the high_scores.csv file, filter for only the winning scores, sorted them from fastest (lowest number of seconds) to slowest, and then return the five fastest. You must return a list of the five scores formatted so that the game prints them as follows: ---HIGH SCORES--- 1: UPDATE_ME - WIN - 11 seconds | 2021-84-88 2: UPDATE_ME - WIN - 13 seconds | 2021-84-88 3: UPDATE_ME - WIN - 21 seconds | 2021-04-08 4: UPDATE_ME - WIN - 31 seconds | 2021-04-08 5: UPDATE ME - WIN - 32 seconds 2021-66-88 Introduction In this assignment you will use your newly developed skills to provide the back-end functionality of a minesweeper game. In minesweeper, the object of the game is to clear a minefield, using logic to determine which grid spaces contain a bomb and which do not. For more information on how it is played, see link. You have been provided with a simple graphical user interface (GUI) that will display a window and allow for interactive elements. When you first launch the project, however, the game will not function as expected. Your job is to complete all the incomplete functions in game_logic.py . Once each of these works, minesweeper will be fully playable! Unless you attempt the bonus problem, you will never need to write or modify ANY code outside of game_logic.py . Just follow the instructions and requirements for each function, and you do not need to understand the GUI code. Project Setup The project should work out of the box, but in case you have errors, this is the working configuration used to develop this assignment: . Python 3.7 . arcade 2.5.6 PyCharm may ask you to install arcade, as it is included in requirements.txt. If it does, approve the installation, as arcade is the game library that this project . uses. Running main.py should open a window, as below: It's important to realise that although the buttons are presented as a 16x16 grid of buttons, the program actually runs on a standard one-dimensional list of length 256. For your reference, an Excel file grid.xlsx has been included in the project to help you visualise how indices 0-255 map onto a 16x16 grid. Part 1 [10 marks] def define_bomb_positions (positions: Set, number, possibilities: List): This is a recursive function! In this function, your job is to return a set of bomb positions that the game will use to populate the mine field. The function has three parameters: 1. positions This parameter holds the set that you will eventually return. It consists of n bomb indices, ranging from 0-255 (the valid grid positions). 2. number This parameter is the number of bomb positions that you are expected to generate. In a standard 16x16 grid of minesweeper, there are 40 bombs, and this is the number that the game provides. 3. possibilities This is simply a list containing the values 0-255 Your job is to take a random selection of 40 indices from the list of possibilities and place those into the positions set. A set requires that all values are distinct, so don't include any duplicates! The other, major constraint is that you must solve this using recursion instead of regular loops. Use of any looping mechanism other than recursion will result in a severe deduction, even if you get the right answer. Yes, this is hard, annoying, and arbitrary, but welcome to university e. Part 2 [15 marks] def number_values (grid: List, bomb_positions: Set): In this function, you will do all the work of determining the number value for each cell position that is not a bomb. In minesweeper, each numbered cell indicates how many bombs are adjacent (see below). 1 1 1 1 1 1 1 1 2 A square's number value means that it touches a bomb, but only that many. For example, a value of 2 means that the cell touches only two bombs. The function has two parameters: 1. grid This is the underlying 256 length list that represents the 16x16 grid. 2. bomb_positions These are the bomb positions that you calculated in Part 1. Your job is to calculate the value of every cell in the grid that is not a bomb. For every cell, you must count how many bombs are in the adjacent cells. The count is the value that should be recorded for that index in the grid list. The following image shows three examples of the directions you must investigate. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33. 24 35 26 37 22 29 11 A2 A AA AC AC 17 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 You will need to create an algorithm that is capable of recognising that corner cells only have 3 potential neighbours, edge cells have 5 potential neighbours, and middle cells have 8 potential neighbours. You must not just hardcode this for each of the 256 cells; put some thought into how to do this algorithmically. In the end, the provided grid array will have a value from 0-8, unless it has a bomb and then should have the character b Part 3 [15 marks] def find_blanks (grid: List[int], blank_index: int) -> List[int]: This is a recursive function! In a game of minesweeper, if you click a cell that has a bomb-adjacency value of 0, it appears as a blank square. Then, the game automatically reveals other adjacent blank squares. The result is something like in the picture below: 1 1 1 1 Your job is to find all the adjacent blank squares (if any!) from a given initial blank index. The function has two parameters: 1. grid This is the underlying 256 length list that represents the 16x16 grid. 2. blank_index This is the initial click that revealed a blank square. Your search starts from this point. Like with many recursive functions, you will need to create a new function helper that does the actual recursion and can have some extra parameters. You will need to following two extra parameters: 1. to visit Neighbouring cells that are yet to be checked. 2. visited Neighbouring cells that have already been checked. 3. blanks A list of all the adjacent blank cells. Using recursion, your solution should follow this algorithm: 3. blanks A list of all the adjacent blank cells. Using recursion, your solution should follow this algorithm: if the length of to visit is e: return blanks pop (remove) a neighbouring index off to visit and add it to to visited if the cell value is @: add cell to blanks get all of its valid neighbours in directions N, S, E, W add any neighbours not already in to visit or visited to to visit repeat the process with updated values else: repeat the process with the updated to visit Part 4 [5 marks] def check_win_condition (pressed_buttons: List[bool), bomb_positions: Set[int]): In this function, you will determine if a game has been won. In minesweeper, the winning condition is that you have revealed all the non-bomb squares. To protect you from dealing directly with GUI button objects, this function just takes a list of booleans, structured in the same way as the grid used in previous functions. All you have to do is determine whether all non-bomb positions are True, signifying that all cells have been revealed without exploding a bomb. This function takes two parameters: 1. pressed_buttons A list of 256 boolean values representing the press state of all the grid buttons. 2. bomb_positions These are the bomb positions that you calculated in Part 1. The function should return True if the win condition has been met, and False otherwise, Part 5 [5 marks] def write_score(name: str, date: datetime.date, seconds: int, is_win: bool): In this function, you simply need to write the provided scores to the CSV file high_scores.csv . The CSV file is more of a log than just high scores, as it includes information about all played games, regardless of the win state. The function has four parameters: 1. name The player's name (this is set in main.py ) 2. date The date that the game was played 3. seconds The game duration in seconds 4. is_win Whether or not the game was won You need to take these values and append a single string, in CSV format, to the high_scores.csv file. The file comes provided with some examples. Part 6 [10 marks] def get_top_five_scores(): At the end of each game, the top five scores are printed to the python console. Your job is to read the high_scores.csv file, filter for only the winning scores, sorted them from fastest (lowest number of seconds) to slowest, and then return the five fastest. You must return a list of the five scores formatted so that the game prints them as follows: ---HIGH SCORES--- 1: UPDATE_ME - WIN - 11 seconds | 2021-84-88 2: UPDATE_ME - WIN - 13 seconds | 2021-84-88 3: UPDATE_ME - WIN - 21 seconds | 2021-04-08 4: UPDATE_ME - WIN - 31 seconds | 2021-04-08 5: UPDATE ME - WIN - 32 seconds 2021-66-88

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!