COMP 1039 Problem Solving and Programming Programming Assignment 2 School of Computer and Information Science The University of South Australia October, 2021 2 of 37 Contents Introduction Assignment Overview Graduate Qualities Assignment Specification Practical Requirements Stages Submission Details Extensions and Late Submissions Academic Misconduct Marking Criteria Sample Output Useful Built-In Python Functions 3 of 37 INTRODUCTION This document describes the second assignment for Problem Solving and Programming. The assignment is intended to provide you with the opportunity to put into practice what you have learnt in the course by applying your knowledge and skills to the implementation a program that will maintain information on characters (heroes and villains). This assignment is an individual task that will require an individual submission. If you are an internal student, you will be required to submit your work via learnonline before Tuesday 9 November (swot-vac), 9am (internal students). This document is a kind of specification of the required end product that will be generated by implementing the assignment. Like many specifications, it is written in English and hence will contain some imperfectly specified parts. Please make sure you seek clarification if you are not clear on any aspect of this assignment. ASSIGNMENT OVERVIEW Manage character (hero and villain) information You are required to write a Python program that will manage character (heroes and villain) information. Character (hero and villain) information will be stored in a text file that will be read in when the program commences. Once the initial character (hero and villain) information has been read in from the file, the program should allow the user to interactively query and manipulate the character (hero and villain) information. Please ensure that you read sections titled ‘Assignment Specification’ below for further details. Please ensure that you read sections titled ‘Assignment Specification’ below for further details. 4 of 37 GRADUATE QUALITIES By undertaking this assessment, you will progress in developing the qualities of a University of South Australia graduate. The Graduate qualities being assessed by this assignment are: The ability to demonstrate and apply a body of knowledge (GQ1) gained from the lectures, workshops, practicals and readings. This is demonstrated in your ability to apply problem solving and programming theory to a practical situation. The development of skills required for lifelong learning (GQ2), by searching for information and learning to use and understand the resources provided (Python standard library, lectures, workshops, practical exercises, etc); in order to complete a programming exercise. The ability to effectively problem solve (GQ3) using Python to complete the programming problem. Effective problem solving is demonstrated by the ability to understand what is required, utilise the relevant information from lectures, workshops and practical work, write Python code, and evaluate the effectiveness of the code by testing it. The ability to work autonomously (GQ4) in order to complete the task. The use of communication skills (GQ6) by producing code that has been properly formatted; and writing adequate, concise and clear comments. The application of international standards (GQ7) by making sure your solution conforms to the standards presented in the Python Style Guide slides (available on the course website). 5 of 37 ASSIGNMENT SPECIFICATION – MANAGE CHARACTER INFORMATION Write a menu driven program called yourEmailId.py that will allow the user to enter commands and process these commands until the quit command is entered. The program will store and maintain character information (using a List of Lists). Character information will be stored in a text file that will be read in when the program commences. Once the initial character data has been read in from the file, the program should allow the user to interactively query and manipulate the character information. Input When your program begins, it will read in character (hero and villain) information from a file called characters.txt. This is a text file that stores information pertaining to characters (heroes and villains). An example input file called characters.txt can be found on the course website (under the Assessment tab). You may assume that all data is in the correct format. The name of the character (hero or villain) is stored on a separate line. The very next line contains the secret identity of the character (hero or villain). The very next line contains a character specifying whether the character is a hero (‘h’) or villain (‘v’), followed by the number of battles fought, battles won, battles lost, battles drawn, and a health value. This information is stored on one line and is separated by the space character as seen in Figure 1 below: After the program has stored the data (using a List of Lists), it will enter interactive mode as described in the following section. Wonder Woman Diana Prince h 5 5 0 0 90 Batman Bruce Wayne h 6 2 0 4 80 The Joker Jack Napier v 5 1 0 4 80 Superman Clark Kent h 7 4 0 3 100 Catwoman Selina Kyle v 12 0 6 6 50 Aquaman Arthur Curry h 8 2 2 4 30 Iron Man Tony Stark h 10 8 2 0 50 Hulk Bruce Banner h 7 2 1 4 80 Thanos n/a v 10 2 0 8 90 Figure 1: Character information file format (characters.txt). 6 of 37 Your program will maintain one List of Lists as follows: character_list = [] # List of Lists to store character information Once the above information is read in from the file, the character list will be populated as follows: character_list [Wonder Woman, Diana Prince, h, 5, 5, 0, 0, 90] [Batman, Bruce Wayne, h, 6, 2, 0, 4, 80] [The Joker, Jack Napier, v, 5, 1, 0, 4, 80] [Superman, Clark Kent, h, 7, 4, 0, 3, 100] [Catwoman, Selina Kyle, v, 12, 0, 6, 6, 50] [Aquaman, Arthur Curry, h, 8, 2, 2, 4, 30] [Iron Man, Tony Stark, h, 10, 8, 2, 0, 50] [Hulk, Bruce Banner, h, 7, 2, 1, 4, 80] [Thanos, n/a, v, 10, 2, 0, 8, 90] Note: A character and their statistics are stored as a list and are stored as one item in the character list, i.e. a List of Lists (as seen above). Interactive Mode Your program should enter an interactive mode after the character (hero and villain) information has been read from the file. The program will allow the user to enter commands and process these commands until the quit command is entered. The following commands should be allowed: 1. list: Displays for all characters, the character’s name, number of battles, battles, won, lost, drawn, and their health value. Outputs the contents of the character list (heroes and villains) as seen below in the section titled Screen Format. Please read the section at the end of this handout titled – ‘Useful Built-In Python Functions’. 2. heroes: Displays for all heroes, the character’s name, number of battles, battles, won, lost, drawn, and their health value. Outputs the contents of the character list (heroes and villains) as seen below in the section titled Screen Format. Please read the section at the end of this handout titled – ‘Useful Built-In Python Functions’. 3. villains: Displays for all villains, the character’s name, number of battles, battles, won, lost, drawn, and their health value. Outputs the contents of the character list (heroes and villains) as seen below in the section titled Screen Format. Please read the section at the end of this handout titled – ‘Useful Built-In Python Functions’. 4. search: Prompts for and reads the character’s (hero/villain’s) name and searches for the character in the character (hero and villain) list. If the character is found in the character list, the character’s name, secret identity, battles fought, won, lost, drawn, and their health value, are displayed to the screen as seen below (in the section titled Screen Format). If the character is not found in the list of characters (heroes and villains), an error message stating the character has not been found is displayed to the screen. 7 of 37 5. reset: Prompts for and reads the character’s (hero/villain’s) name and searches for the character in the character list (heroes and villains). If the character is found in the list of characters (heroes and villains), the character’s health value is reset to 100. If the character is not found in the list of characters (heroes and villains), an error message stating the character has not been found is displayed to the screen. 6. add: Prompts for and reads the character’s (hero or villain’s) name, secret identity and whether the character is a hero or villain. If the character does not already exist (i.e. a match is not found on the character’s name), the character is added to the list of characters (heroes and villains). If the character is added, the health value is initialised to 100 and all other stats (number battles, no won, no lost, no drawn), are initialised to zero and a message is displayed to the screen indicating that this has been successfully added. The character information must be added after the last character entry stored in the list (i.e. at the end of the list). If the character is already stored in the character list, an error message is displayed. No duplicate entries are allowed. 7. remove: Prompts for the character’s name. If the character is found, he/she is removed from the list of characters (heroes and villains) and a message is displayed to the screen indicating that this has been done. If the character is not found in the character list, an error message is displayed. 8. high: Displays the character with the highest number of battles won in the list of characters. Where two characters have the same number of battles won, the character with the lower number of battles fought should be displayed to the screen – see section titled ‘Screen Format’ below. If no characters are stored in the list or a character with the highest number of battles won cannot be found (i.e. all characters have zero battles won), display an error message accordingly. 9. battle: Prompts for the name of the two opponents to do battle. The program searches for each character in the list of characters (heroes and villains) and if the character is not found in the list of characters, an error message is displayed to the screen and the user is asked to enter another character (hero/villain). If the opponents are found in the list of characters (heroes and villains), they are then able to do battle and the number of battle rounds the heroes/villains will undertake (a number between 1-5 inclusive) is prompted for and read. One battle may have many (1-5 inclusive) rounds. The heroes/villains battle until either an opponent dies (health status is reduced to zero) or the number of battle rounds have been completed. For each individual battle round, the hero/villain will sustain a certain amount of damage to their health rating. The amount of damage sustained from the battle will be a randomly generated value between 0–50 inclusive. After each round, each opponents damage value (i.e. randomly generated number between 0–50 inclusive) and current health value (i.e. calculated by: health value – damage value) are displayed to the screen. After every battle (however many rounds), a winner is determined (i.e. the opponent with the higher health value wins the battle) and the opponents’ battle statistics are updated (i.e. number of battles, no won, no lost, etc…) accordingly. 10. health: Displays the list of characters (heroes and villains) in descending order of health. Where two characters have the same health status, the character with the highest number of battles fought should appear first. This command should not alter the original list of characters in any way. The information is displayed to the screen as described in the section titled ‘Screen Format’ below. 8 of 37 11. quit: Causes the program to quit, outputting the contents of the character list (List of lists) to a file (see section ‘Final Output’ below for format). Note: The program should display an appropriate message if a character is not found matching a search criteria. Appropriate messages should also be displayed to indicate whether a command has been successfully completed. Please refer to the sample output (at the end of this handout) to ensure that your program is behaving correctly and that you have the correct output messages. Each time your program prompts for a command, it should display the list of available commands. See the sample output (at the end of this handout) to ensure that you have the output format correct. For example: Please enter choice [list, heroes, villains, search, reset, add, remove, high, battle, health, quit]: Menu input should be validated with an appropriate message being displayed if incorrect input is entered. Screen Format The list, heroes and villains commands (display_characters() function) should display the character (hero and villain) information in the following format: =================================================== – Character (heroes and villains) Summary – =================================================== – P W L D Health – ————————————————— – Wonder Woman 5 5 0 0 90 – ————————————————— – Batman 6 2 0 4 80 – ————————————————— – The Joker 5 1 0 4 80 – ————————————————— – Superman 7 4 0 3 100 – ————————————————— – Catwoman 12 0 6 6 50 – ————————————————— – Aquaman 8 2 2 4 30 – ————————————————— – Iron Man 10 6 3 1 50 – ————————————————— – Hulk 7 2 1 4 80 – ————————————————— – Thanos 10 2 0 8 90 – ————————————————— 9 of 37 Likewise for the health command (which should use the same display_characters() function) should display the character (hero and villain) information in the following format: =================================================== – Character (heroes and villains) Summary – =================================================== – P W L D Health – ————————————————— – Superman 7 4 0 3 100 – ————————————————— – Thanos 10 2 0 8 90 – ————————————————— – Wonder Woman 5 5 0 0 90 – ————————————————— – Hulk 7 2 1 4 80 – ————————————————— – Batman 6 2 0 4 80 – ————————————————— – The Joker 5 1 0 4 80 – ————————————————— – Catwoman 12 0 6 6 50 – ————————————————— – Iron Man 10 6 3 1 50 – ————————————————— – Aquaman 8 2 2 4 30 – ————————————————— =================================================== The search command should display individual character (hero/villain) information to the screen in the following format: All about Catwoman –> VILLAIN Secret identity: Selina Kyle Battles fought: 12 > No won: 0 > No lost: 6 > No drawn: 6 Current health: 50% Or if a hero… All about Aquaman –> HERO Secret identity: Arthur Curry Battles fought: 8 > No won: 2 > No lost: 2 > No drawn: 4 Current health: 30% 10 of 37 The high command (display_highest_battles_won() function) should display the character with the highest number of battles won in the following format: Highest number of battles won => Iron Man with 6 opponents defeated! Final Output When your program finishes (because you entered the quit command) your program should output the contents of the list of characters to a file called new_characters.txt. The format of this file should exactly match that of the input file. 11 of 37 PRACTICAL REQUIREMENTS It is recommended that you develop this part of the assignment in the suggested stages. It is expected that your solution WILL include the use of: Your solution in a file called yourEmailId.py. Appropriate and well constructed while and/or for loops. (Marks will be lost if you use break statements or the like in order to exit from loops). You must implement each function listed below. Appropriate if, if-else, if-elif-else statements (as necessary). The following functions: o read_file(filename) This function takes a file name and reads the contents of that file into a list called character_list. The function returns the newly created list of characters (i.e. a list of lists). You must use a loop in your solution. You may use String and/or List methods in this function only. You may find the String methods split() and strip() useful here. Please note: This function will be provided for you. You may use your own function or use the function provided… the decision is yours. : ) o write_to_file(filename, character_list) This function will output the contents of the character list (list of list) to a file in the same format as the input file. The file will need to be opened for writing in this function (and of course closed once all writing has been done). The function accepts the filename of the file to write to and the list of character objects. You must use a loop in your solution. o display_characters(character_list, display_type) This function will take the list of characters and a display_type as a parameter and will output the contents of the list to the screen. If the display type is 0, all characters will be displayed to the screen. If the display type is 1, only heroes will be displayed to the screen. If the display type is 2, only villains will be displayed to the screen. This function displays the information to the screen in the format specified in the assignment specifications under the section – ‘Screen Format’. You must use a loop in your solution. Please have a read of the section at the end of this handout titled – ‘Useful Built-In Python Functions’. o find_character(character_list, name) This function will take the character’s name as input along with the list of characters (character_list) and will return the position (index) of the character found in the character_list. If the character is not found, the function returns -1. You must use a loop in your solution. You must not use list methods in your solution. o add_character(character_list, name, secret_identity, hero) This function takes the list of characters and the character’s (to be added) name, secret_identity and hero (‘h’ | ‘v’) as parameters. If the character already exists in the list of characters, display an error message to the screen. If the character does not exist in the characters (heroes and villains) list, the character is added. Create a new list with the information read in (i.e. name, secret_identity and hero) and add the character’s information (as a list) to the end of the list of characters. If the character is added, health is initialised to 100 and all other stats data (number of battles, no won, no lost, no drawn), are initialised to zero and a message is displayed to the screen indicating this has been successfully added. This function returns the list of characters. You may use the list_name.append(item) method to add the new character to the list of characters. You must call function find_character() from this function. 12 of 37 o remove_character(character_list, name) This function takes the list of characters and the character’s (to be removed) name as parameters. If the character is not found in the list of characters, display an error message to the screen. If the character does exist in the characters list, this function removes the character and displays a message to the screen indicating that the character has been removed from the characters list. This function returns the list of characters. You may use the list_name.append(item) method in this function. You must call function find_character() from this function. o display_highest_battles_won(character_list) This function takes the character list as a parameter and displays the character with the highest number of battles won in the list of characters to the screen. Where two characters have the same number of battles won, the character with the lower number of battles fought should be displayed to the screen. If no characters are stored in the list or a character with the highest number of battles won cannot be found (i.e. all characters have zero battles won), display an error message accordingly. o do_battle(character_list, opponent1_pos, opponent2_pos) This function takes the list of characters and the position of the two characters that are about to do battle (i.e. position is the location/index of the character stored in the list of characters. This is useful so we can update the character’s health value and battle statistics after every battle). This function prompts for and reads the number of battle rounds the heroes/villains will undertake (a number between 1-5 inclusive). The function allows the heroes/villains to battle until either an opponent dies (health status is reduced to zero or less) or the number of battle rounds have been completed. For each individual battle round, the hero/villain will sustain a certain amount of damage to their health rating. The amount of damage sustained from the battle will be a randomly generated value between (0 – 50 inclusive). General algorithm is as follows: while (number of battle rounds have not been completed and both opponents are still alive) randomly generate a damage value sustained from battle and update opponent 1’s health value by calling method update_health(damage1). randomly generate a damage value sustained from battle and update opponent 2’s health value by calling method update_health(damage2). display opponent 1 round results (as seen in the sample output) display opponent 2 round results (as seen in the sample output) determine the winner of the battles – the character with the most health left at the end of all the battle rounds is the winner. display the winner to the screen and also report if a character has died as a result of battle (refer to sample output for layout). update opponent 1’s battle statistics. Hint: Increment the number of battles by one, number won, lost, drawn accordingly. We increment battles by one because battles may consist of a number of battle rounds. update opponent 2’s battle statistics. Hint: Increment the number of battles by one, number won, lost, drawn accordingly. We increment battles by one because battles may consist of a number of battle rounds. 13 of 37 o sort_by_health (character_list) This function takes the list of characters and returns a copy of the list in descending order of health. Where two characters have the same health status, the character with the higher number of battles fought should appear first. This function must NOT modify the character list being passed in as a parameter. Your solution must NOT make use of List methods or any of the sort functions available in the Python Standard Library. This function returns a copy of the character list sorted in descending order of health. You may wish to create/define additional functions (which are called from within this function) to assist with this task. Good programming practice: o Consistent commenting, layout and indentation. You are to provide comments to describe: your details, program description, all variable definitions, all function definitions and every significant section of code. o Meaningful variable names. Your solutions MAY make use of the following: Built-in functions int(), input(), print(), range(), open(), close(), len(), format() and str(). Concatenation (+) operator to create/build new strings. The list_name.append(item) method to update/create lists. Access the individual elements in a string with an index (one element only). i.e. string_name[index]. Access the individual elements in a list with an index (one element only). i.e. list_name[index] or list_name[i][j]. Your solutions MUST NOT use: Built-in functions (other than the int(), input(), print(), range(), open(), close() len(), format() and str() functions). Slice expressions to select a range of elements from a string or list. i.e. name[start:end]. String or list methods (i.e. other than those mentioned in the ‘MAY make use’ of section above). Global variables as described in week 10 material. Do not use break, or continue statements in your solution – doing so will result in a significant mark deduction. Do not use the return statement as a way to break out of loops. Do not use quit() or exit() functions as a way to break out of loops. PLEASE NOTE: You are reminded that you should ensure that all input and output conform to the assignment specifications. If you are not sure about these details you should check with the sample output provided at the end of this document or post a message to the discussion forum in order to seek clarification. Please ensure that you use Python 3.9.6 or a later version (i.e. the latest version) in order to complete your assignments. Your programs MUST run using Python 3.9.6 (or latest version). 14 of 37 STAGES It is recommended that you develop this part of the assignment in the suggested stages. Many problems in later stages are due to errors in early stages. Make sure you have finished and thoroughly tested each stage before continuing. The following stages of development are recommended: Stage 1 To begin, download the provided files (available on the course website – Assessment Tab) and re-name assign2_emailid.py to yourEmailId.py. Define an empty list to store the character information. For example: character_list = [] Make sure the program runs correctly. Once you have that working, back up your program. Note: When developing software, you should always have fixed points in your development where you know your software is bug free and runs correctly. Stage 2 Write the code for functions read_file() and display_characters() as specified above. Add code to call these two functions to ensure they are working correctly. You may write your own read_file() function or you may use the one provided for you (included in the provided file). Stage 3 Now that you know the information is being correctly stored in your characters list, write the code for function write_to_file(). Add code to call this function to ensure it is working correctly. Stage 4 Implement the interactive mode, i.e. to prompt for and read menu commands. Set up a loop to obtain and process commands. Test to ensure that this is working correctly before moving onto the next stage. You do not need to call any functions at this point, you may simply display an appropriate message to the screen, for example: Sample output: Please enter choice [list, heroes, villains, search, reset, add, remove, high, battle, health, quit]: roger Not a valid command – please try again. Please enter choice [list, heroes, villains, search, reset, add, remove, high, battle, health, quit]: list In list command Please enter choice [list, heroes, villains, search, reset, add, remove, high, battle, health, quit]: heroes In heroes command Please enter choice 15 of 37 [list, heroes, villains, search, reset, add, remove, high, battle, health, quit]: villains In villains command Please enter choice [list, heroes, villains, search, reset, add, remove, high, battle, health, quit]: search In search command Please enter choice [list, heroes, villains, search, reset, add, remove, high, battle, health, quit]: add In add command Please enter choice [list, heroes, villains, search, reset, add, remove, high, battle, health, quit]: remove In remove command Please enter choice [list, heroes, villains, search, reset, add, remove, high, battle, health, quit]: high In high command Please enter choice [list, heroes, villains, search, reset, add, remove, high, battle, health, quit]: battle In battle command Please enter choice [list, heroes, villains, search, reset, add, remove, high, battle, health, quit]: health In health command Please enter choice [list, heroes, villains, search, reset, add, remove, high, battle, health, quit]: quit Menu input should be validated with an appropriate message being displayed if incorrect input is entered by the user. Stage 5 Implement one command at a time. Test to ensure the command is working correctly before starting the next command. Start with the quit and list commands as they do not need you to add anything further to the file other than ensuring that the function calls are in the correct place. You should be able to see that for most commands there is a corresponding function(s). For the remaining commands, the following implementation order is suggested (note: this is a guide only): list command (display_characters() function). heroes command (display_characters() function). villains command (display_characters() function). search command (find_character() function). reset command (calls the find_character() function). add command (add_character() function). remove command (remove_character() function). 16 of 37 high command (display_highest_battles_won() function). battle command (do_battle() function). health command (sort_by_health() function). Stage 6 Ensure that you have validated all user input with an appropriate message being displayed for incorrect input entered by the user. Add code to validate all user input. Hint: use a while loop to validate input. Stage 7 Finally, check the sample output (see section titled ‘Sample Output’ towards the end of this document) and if necessary, modify your code so that: The output produced by your program EXACTLY adheres to the sample output provided. Your program behaves as described in these specs and the sample output provided. 17 of 37 SUBMISSION DETAILS You are required to do the following in order to submit your work and have it marked: o You are required to submit an electronic copy of your program via learnonline before Tuesday 9 November (swot-vac), 9 am (internal students). All students must follow the submission instructions below: Ensure that your files are named correctly (as per instructions outlined in this document). Ensure that the following file is included in your submission: yourEmailId.py For example (if your name is James Bond, your submission file would be as follows): bonjy007.py All files that you submit must include the following comments. # # File: fileName.py # Author: your name # Email Id: your email id # Description: Assignment 2 – place assignment description here… # This is my own work as defined by the University’s # Academic Misconduct policy. # Assignments that do not contain these details may not be marked. You must submit your program before the due date via learnonline. Work that has not been correctly submitted to learnonline will not be marked. It is expected that students will make copies of all assignments and be able to