CPSC 231 Tutorial Exercise 4 and Homework Assignment 4 A Prison Escape Game (Learning Objective: understand the use of classes and objects, and file operations)Tutorial Exercise 4 Weight:...

homework assignment 4. you can scroll down in the file to find it.


CPSC 231 Tutorial Exercise 4 and Homework Assignment 4 A Prison Escape Game (Learning Objective: understand the use of classes and objects, and file operations) Tutorial Exercise 4 Weight: 1.25% of final grade Submission: Nothing Demonstrate your code to your tutorial TA. You may demonstrate as many times as you wish until you get it right. Your TA can provide feedback each time you demonstrate. To receive marks for the exercise, you must demonstrate to your TA before the end of your enrolled tutorial session on either Wednesday November 30th or Thursday December 1st. Detailed Descriptions The video game industry is a multi-billion dollar industry and it is growing so fast that it is making more money than movie, TV, and music industries combined. Source: 2022 Essential Facts About the Video Game Industry, Entertainment Software Association. In this exercise, you will start to create your own game called Prison Escape. Step 1: Watch the video posted on D2L to see how the game is played when completed. This is a turn-based game. Every turn, the player character can move by one square, and each of the guards can move by one square. The player's goal is to escape through the door. If the player character moves within a guard's attack range, the guard immediately kills the player character. Step 2: Create a new py file to work on. Name your file classes.py. If it is not called this name, rename it. Step 3: Download the new file, main.py, from D2L. main.py must be placed in the same directory as your own classes.py file. Download guards.txt and map.txt from D2L and place them in the same directory. The map.txt file contains the layout of the map for the game. The guards.txt file contains the locations and behaviour of the guards. Step 4: Do not change anything in main.py. Start writing all your code in classes.py. You are allowed to import math, numpy. Nothing else should be imported. In your code, do not import main! It is the other way around: main.py imports classes. Except the import statements (if you choose to import math or numpy), you should not have any code that is outside a class. Import must happen at the top of the code. All your other code should be defined inside a class. Your classes and their methods are going to be used by main.py. You can use anything else that is not forbidden in this document, including creating additional methods in your classes. Step 5: Create a new class called game_map. game_map has the following methods you must create: __init__(self, map_file, guard_file) Parameters: map_file, a string representing the name of the map file guard_file, a string representing the name of the guard file Returns: nothing. The __init__ method should read the content from map_file and print it to the screen, line by line. The game will not run until you create the rest of the methods, so please continue. get_grid(self) Parameters: Nothing Returns: a list. For now, just have it return the empty list [] get_guards(self) Parameters: Nothing Returns: a list. For now, just have it return the empty list [] update_player(self, direction) Parameter: direction, a string whose value is one of the four: "U", "D", "L", "R" Returns: Nothing. For now, just put pass as the code in the body update_guards(self) Parameters: Nothing Returns: Nothing. For now, just put pass as the code in the body player_wins(self) Parameters: Nothing Returns: a Boolean. For now, just have it return False player_loses(self) Parameters: Nothing Returns: a Boolean. For now, just have it return False Now, write your code in the __init__ method to read the content from map_file and print it to the screen, one line at a time. Run main.py. If you get it right, you should see the following printed in the output. The game window should just be empty. In the map file, # represents a wall. P represents the starting location of the player character. E represents the door to escape to. G (not shown in the image above) represents a guard. A space represents an empty space. You can assume this file always has 12 rows, and each row always has 16 columns. Other than that, your code has to read whatever is in the file. If it says that you don't have numpy or pygame, please install it first. The video on D2L shows you how to do it using PyCharm. Demonstrate to your TA that you have done this work. Homework Assignment 4 Weight: 8% of final grade Due date: Friday December 9th, at 11:59pm Estimated time needed to complete Assignment 4: about the same as what you spent on Assignment 3. Submission: one classes.py file only, submit on the D2L Dropbox. You may submit as many times as you wish, and only the latest submission will be marked. Late Submissions: Submitting on December 10th will use up one personal day. Submitting on December 11th will use up two personal days, etc. You have a total of 5 personal days for the entire semester. No penalties for using personal days. An assignment will not be accepted if it is late and all personal days are already used. Academic Integrity: This work must be completed individually. Please follow academic integrity rules as discussed during lecture. You may discuss your ideas in English (not in Python) with other students as much as you like, but make sure that when you write your code that it is your own. A good rule of thumb is to never let anyone else see your code, except your instructor and TAs. Detailed Descriptions Continue working from your tutorial exercise file. Step 6: Create a new class called guard. guard has the following methods you must create: __init__(self, row, col, attack_range, movements) Parameters: row, an int representing the current row of this guard col, an int representing the current column of this guard attack_range, an int representing the attack range of this guard movements, a list representing the list of movements the guard will do. Returns: nothing. The __init__ method should remember all the information given to it through the parameters. You can decide on the best ways to do that. The attack_range defines how far the guard can shoot the player character. It can be 1 or more. For example, if attack_range is 1, the guard can kill anyone that is 1 square away (see all the squares marked by x in the diagram): if attack_range is 3, the guard can kill anyone that is at most 3 squares away (see diagram): Guards can shoot through walls (don't ask me how), so hiding behind walls won't work! get_location(self) Parameters: Nothing Returns: a tuple of (int, int), representing the row and column of this guard's current location. move(self, current_grid) Parameter: current_grid, a 2D list of 12 rows by 16 columns representing the current grid of the map. Returns: a tuple of (int, int), representing the row and column of this guard's new location. This method moves the guard by one square according to the next command on the guard's movements list, and returns its new location. Guards cannot walk through walls or the player character or the exit, or walk off screen. For example, if it encounters a command "L" and its left square is not empty (or any other invalid The movements list always has the following format [, , … ] Each move is a string that is one of four values: "L" – meaning move left one square "R" – meaning move right one square "U" – meaning move up one square "D" – meaning move down one square This is a turn-based game, so every turn, each guard makes one move. For example, if the guard's movements list is ["L", "D", "U", "R"], that means: 1st turn – this guard moves left 1 square 2nd turn – this guard moves down 1 square 3rd turn – this guard moves up 1 square 4th turn – this guard moves right 1 square 5th turn – the list jumps back to the beginning, so this guard moves left 1 square. …. moves), it remains where it is for this turn. Essentially it just wastes a turn doing nothing. Next turn it will go to the next command on its movements list. Complete this requirement last, after you've done the rest of the assignment. Use current_grid to determine where the walls are, etc. but current_grid will not be created until Step 7. enemy_in_range(self, enemy_row, enemy_col) Parameters: enemy_row and enemy_col, the row and column of the guard's enemy. Returns: a Boolean. True if the enemy at enemy_row and enemy_col is in attack range of this guard. False otherwise. Step 7: We will go back to the game_map class and fill in the content of this class. __init__(self, map_file, guard_file) Parameters: map_file, a string representing the name of the map file guard_file, a string representing the name of the guard file Returns: nothing. The __init__ method should read the content from map_file and remember it. You can decide on the best ways to do that. The __init__ method should read the content from guard_file and remember it. For every guard, create a new object using the guard class you defined in Step 6. Keep all the objects in a list. File reading should be protected with try/except to prevent file reading errors from crashing the code. Exit properly if file not found or file reading errors are encountered. get_grid(self) Parameters: Nothing Returns: a 2D list. The return value is a 2D list of 12 rows by 16 columns. It represents the current grid of the map. You can assume the grid is always 12 by 16. Every item in the 2D list is a string of length 1: "#" represents a wall. "P" represents the player character. There is only one player character. "E" represents the door to escape to. There is only one door. "G" represents a guard, and there can be many guards. " " (one space) represents an empty space. The 2D list should not contain any other symbols. get_guards(self) Parameters: Nothing In the guard_file, every row defines a new guard. For example: This row is one guard. The first two numbers, 5 and 5, represent the starting location of the guard – row 5 and column 5. The third number, 3, represents the attack range. This guard can kill anyone who comes within 3 squares of itself. The rest of the line represents the movements. This guard moves up, up, up, left, left, left, down, down, down, right, right, right, and then repeats from the beginning. You cannot assume there are always 3 guards. Your code should read whatever is in the guard_file. Returns: a list. The return value is a list of current guard objects. update_player(self, direction) Parameter: direction, a string whose value is one of the four: "U", "D", "L", "R" Returns: Nothing. This method updates the current location of the player character on the grid according to the given direction parameter – moving it one square up, down, left, or right. The player character cannot walk through walls or the guards, or walk off screen. For example, if it encounters the direction "L" and its left square is not empty (or any other invalid moves), it remains where it is for this turn. Essentially it just wastes a turn doing nothing while everyone else moves. Complete this requirement last, after you've done the rest of the assignment. update_guards(self) Parameters: Nothing Returns: Nothing. This method updates the current location of every guard for one turn. For each guard object, calling its move method will move it and have its new location returned. The move method was defined in step 6. Guards move in the order that they are defined in the guards file. player_wins(self) Parameters: Nothing Returns: a Boolean. Returns True if the player character is currently standing on the exit square. False otherwise.
Dec 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here