Python assignment
Page 1 of 15 Assignment # 6 COMP1005B – Winter 2020 Due on Friday, April 17th by 11:00 pm. Submission Guidelines: • Ensure your name and student number are at the top of any files submitted. • Submit all code files as .py files • Create a .zip file of all your files, even if there's only one (this helps TAs keep organized): o On Windows, select the files you want to include, right click, choose "Send to..." then "Compressed (zipped) folder" o On a mac, select the files you want to include, right click, and choose "Compress X Items" • Name your zip file Firstname_Lastname_StudID_A6.zip, where Firstname and Lastname are to be replaced by your names, and StudID is to be replaced by your student Id number. • Submit your assignment using cuLearn. • Late assignments will not be accepted and will receive a mark of 0. • Submissions that crash (i.e., terminate with an error) on execution will receive a mark of 0. • If your internet connection at home is down or does not work, we will not accept this as a reason for handing in an assignment late, so do not wait until the last minute to submit your assignment! • You are responsible for submitting all files and ENSURING that the correct file is submitted, and the submission is completed successfully. If you are having issues with submission, contact the instructor before the due date. • You are not allowed to use any python library modules that were not explicitly covered in the lecture notes. • Absolutely NO collaborations are allowed. In this assignment, you will create a text-based board game in Python that will use 2-dimentional lists, recursions, loops and other programming logics. Create a single Python file called a6.py. You are expected to add and test several functions in this file that will help the main function to start and continue the game. Rules of the game: This game will be played using a 2D-list (board) that must always have the same number of rows and columns. The board will be displayed to the user in the following format. http://carleton.ca/culearn Page 2 of 15 0123456 +=======+ 0|G------|0 1|PPPPPPP|1 2|-------|2 3|-------|3 4|-------|4 5|-------|5 6|B-RB--R|6 +=======+ 0123456 Please note the following: • This example game board has 7 rows and 7 columns (7x7). • The row and column numbers (0,1,..,6 in this example), and the additional symbols (+,=,|) are not part of the 2D-list. These symbols must be displayed whenever the board is displayed to the user. • G: the general will be placed in some random column on row 0. • P: pawns will be placed in all columns on row 1. • B: two (2) bishops will be placed in random columns on the last row (row 6 in this example). • R: two (2) rooks will be placed in random columns on the last row as well (row 6 in this example). • B and R are called soldiers. The user of your program should be able to move these soldiers (see Valid moves below) to some other locations on this board and try to capture G to win the game. Valid moves: • G can move to any empty column on row 0 only. • P does not move at all. • B can move diagonally in four different directions, i.e., left-up, right-up, left-down and right-down. • R can move in a straight line in four different directions, i.e., left, right, up, and down. • B and R cannot pass a P on their way to a new location row,column, unless (a) B/R captures P on their first (valid) move and (b) then go to row,column in the following (valid) move. • G, B, R cannot move to a location if that location is already occupied by another soldier. Your program must also create another 2D list (trap_map - see below) that should not be visible to the user but the total number of hidden traps should be displayed in the beginning of the game. Page 3 of 15 Your program should randomly place some traps (T) on rows 2 to the second last rows on the board with some predefined probability (see rules in later part of the specifications). 0123456 +=======+ 0| |0 1| |1 2| T |2 3| T T |3 4|T T |4 5|T T T |5 6| |6 +=======+ 0123456 This board has 8 hidden traps. How the game continues: The game starts by displaying the game board and the total number of hidden traps. Then the game will continue in a loop until (a) G is captured by the soldiers or (b) all four soldiers die. The maximum board size can be 10x10 and the minimum board size can be 5x5. At every iteration of the game, 1. Your program should ask the user which soldier they want to move and where. 2. The user must enter these two information (locations) as row,column - where row and column are valid integer numbers (refer to the max/min board size to check the validity), separated by a comma. In case the user enters bad inputs (anything other than two valid integers separated by a comma), they should be asked again unless valid inputs are entered. 3. Next, your program must check if a soldier is present in the current location entered by the user, if not a message will be displayed to the user and nothing changes. 4. If a soldier is available in the current location, your program must validate the move of that soldier (see Valid Moves) from their current location to the new location. If the move is not valid, the soldier will not move, a message will be displayed to the user and nothing changes. 5. If the move is valid, your program should check if there is any hidden trap on the track from the current location to the new location. If a trap is there, that soldier dies, and the user loses 1 point. Subsequently, G will move to a random free location on row 0. The board should be displayed again showing that trap. A message and the current points will be shown to the users. 6. If there is no trap on the track, the soldier moves to the new location and one of these three cases may occur. (a) If the new location contains P, the soldier captures (replaces) P and the user gets 2 points. G will move to a random free location on row 0. The updated board should be displayed again. A message and the current points will be shown to the users. (b) Page 4 of 15 If the new location contains G, the soldier captures G and the user gets 10 points. The updated board should now display all previously hidden traps. A message and the current points will be shown to the users. (c) If the new location is empty, the soldier just moves there, and the updated board should be displayed. A message and the current points will also be shown to the users. See the sample runs at the end of this specifications. Required Functions: You must write the following functions (not necessarily in this order) as they are the required components for this assignment. You are encouraged to create any additional helper functions, if you prefer, to better organize your code. • create_board() – does not take any parameter and creates a 2D-list to represent the board with a predefined SIZE, where SIZE is a global constant, with 5 ≤ SIZE ≤ 10. The board should contain G, P, B, R according to the given rules above. The empty locations should have ‘-‘ symbols. • set_traps() – does not take any parameter and creates a 2D-list to represent the trap_map with a predefined SIZE, where SIZE is a global constant, with 5 ≤ SIZE ≤ 10. The trap_map should contain traps (T) placed randomly according to the given specifications above. Set the probability value to 20%, that is your program should place a trap in the current location with a 20 in 100 chance (or a 2 in 10 chance). This function should also display how many hidden traps the game board has. • display_board() – takes a 2D-list as an argument and prints the board to the terminal is the specified format. • validate_moves() – takes the following arguments, four integers representing the rows and columns of the current location and the new location, respectively, and a 2D-list representing the board. This function returns True if the proposed move is valid (see rules), otherwise returns False. • check_traps() – this must be a recursive function that takes the following arguments, four integers representing the rows and columns of the current location and the new location, respectively, and a 2D-list representing the trap_map. This function checks for traps on the proposed track of the soldier from their current location to the new location, and returns the location of the trap, if exists. Otherwise, it returns False. Page 5 of 15 • move_general() - takes a 2D-list as an argument representing the board. This function moves the location of G (see the rules) and returns the updated 2D-list. • move_soldier() – takes the following arguments, four integers representing the rows and columns of the current location and the new location, respectively, and two 2D-lists representing the board and trap_map, respectively. This function follows the rules of the game and move the soldier to the new location if everything goes according to the rules. It should also update the board, displays necessary messages to the terminal and returns the score that the user earned during this move