Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPT 120 Topic:  Case Study.

Similar presentations


Presentation on theme: "CMPT 120 Topic:  Case Study."— Presentation transcript:

1 CMPT 120 Topic:  Case Study

2 Learning Outcome At the end of this course, a student is expected to:
Create (design) small to medium size programs using Python: Decompose a solution into parts: decompose a Python program into functions Translate pseudocode to/from Python programs

3 Last Lecture List of Lists File I/O (input and output)

4 Today’s Menu Practise designing (Step 1 and Step 2) a solution to a problem statement i.e., practise Steps 1 and 2 of the software development process A few glimpses at Step 4 Implementation

5 Data types The reason why we are learning about various data types in Python is that when we design software (i.e., solution to problems), part of the design step is to decide Which data is our program to manipulate in order to solve the problem How to represent (i.e., remember) this data in our program -> variable The name (and purpose) of the variable The data type of the variable

6 Step 1 - Problem Statement
Write a Python program to allow the player to play a simple version of the Mario game The idea is to move Mario around the maze until Mario has reached the “gate” that allows him to exit the maze As Mario is making is way through the maze, there are obstacles. Some of these obstacles are mines and some are extra lives Mario starts with 10 lives Moving Mario onto a cell of the maze containing a mine will decrement Mario’s life count by 1 Moving Mario onto a cell of the maze containing an extra life will increment Mario’s life count by 1 The player can see where the obstacles are, but does not know whether the obstacles are mines or extra lives

7 Step 1 - Problem Statement (cont’d)
The player moves Mario one cell at a time by entering a letter indicating the direction of Mario’s movement The only possible directions are : Move Mario 1 cell to the right Move Mario 1 cell to the left Move Mario 1 cell up Move Mario 1 cell down The player must enter a specific letter to exit the game

8 Step 1 - Problem Statement (cont’d)
Information needed to setup our Mario game is stored in a file, which has the following structure (or format): File Structure Line 1 is the width of the maze. Line 2 is the height of the maze. Line 3 is the number of rewarding obstacles. Line 4 is the number of exploding obstacles. Line 5 is the symbol representing an empty maze cell. Note: there is a blank space before the symbol then a blank space after it. Line 6 is the symbol representing a maze cell containing a rewarding obstacle. Line 7 is the symbol representing a maze cell containing an exploding obstacle. Line 8 is the symbol representing a maze cell containing Mario. Line 9 is the symbol representing the exit gate located on either the top or bottom border.

9 Step 1 - Problem Statement (cont’d)
File Structure (cont’d) Line 10 is the symbol used to create the top and bottom horizontal borders. Line 11 is the symbol used to create the left and right vertical borders. Line 12 is the location (row and column) of Mario. Careful! The row is a number between 0 and mazeHeight-1 and the column is a number between 0 and mazeWidth-1. Lines 13 to last line of the input data file are the locations of the obstacles, one location (row and column) per line. First, there are x rewarding obstacles, where x is the number found on Line 3 -> the number of rewarding obstacles. Then, there are y exploding obstacles, where y is the number found on Line 4 -> the number of exploding obstacles Careful! The row is a number between 0 and mazeHeight-1 and the column is a number between 0 and mazeWidth-1.

10 Step 2 – Design Algorithm – What will our application do when it executes?

11 Mario’s Algorithm Read data from data file Create a maze
Create the boundary around the maze (not part of the maze) Place rewarding obstacles in the maze Place exploding obstacles in the maze Place Mario in the maze Create exit gate and place it in the maze Set Mario's score Set the condition variables for while loop While the player is playing and Mario still has some lives and he has not reached the gate, do Display the maze and Mario's score Ask the player to enter a letter to indicate either the direction in which Mario is to move or to exit the game Move Mario in the maze according to player’s input and checking to see whether Mario has reached the exit gate or he has stepped on a mine or an extra life or the player wants to terminate the game

12 Step 2 – Data Data – Which data will our Python program need to remember in order to execute? maze -> represented by a variable of type list of lists obstacles -> represented by 2 variables of type list of lists Mario -> represented by a variable of type string (containing 1 character) Mario’s score -> represented by a variable of type integer player’s input -> represented by a variable of type string (containing 1 character) etc…

13 Step 4 - Implementation Create a maze
def createMaze(aMaze, aWidth, aHeight, cellContent): ''' Create and return "aMaze" of "aWidth" by "aHeight". Each cell of the maze is a string set to "cellContent". ''' aMaze = [ [ (cellContent) for i in range(aWidth) ] for j in range(aHeight) ] # For debug purposes - Print maze as a list of list # printMaze(maze) return aMaze

14 Step 4 - Implementation # Print Maze - for debug purposes def printMaze(aMaze): ''' Print "aMaze" - for debug purposes. ''' for row in aMaze: print(row)

15 11. Display the maze def displayMaze(aMaze, aWidth, aHeight, hBoundary, bSContent ): ''' Display 'aMaze' with column numbers at the top and row numbers to the left of each row along with the top and the bottom boundaries "hBoundary" that surround the maze. Other parameters: "aWidth" is the width of the maze. "aHeight" is the height of the maze. "bSContent" is the symbol used for the vertical border. No returned value ''' topIndex = 0 bottomIndex = 1 offset = 3 aString = (offset+1) * " " print() # Display a row of numbers from 1 to aWidth for column in range(aWidth): aString = aString + str(column+1) + " " if len(str(column+1)) == 1 : aString += " " print(aString) # Display the top boundary of maze print(offset * " " + "".join(hBoundary[topIndex])) # Display a column of numbers from 1 to aHeight + left and right boundaries of the maze for row in range(aHeight): pre = str(row+1) + " " + bSContent if row >= 9: # i.e., displayed row number is >= 10 pre = str(row+1) + bSContent post = bSContent aRow = pre + ''.join(aMaze[row]) + post print(aRow) # Display the bottom boundary of maze print(offset * " " + "".join(hBoundary[bottomIndex])) return

16 Step 4 - Implementation Place in the maze
def placeInMaze(aMaze, aRow, aColumn, aContent): ''' Place something represented by "aContent" at the location ["aRow", "aColumn"] into "aMaze" Returned value: "aMaze" updated. ''' aMaze[aRow][aColumn] = aContent return aMaze

17 Summary Practise designing (Step 1 and Step 2) a solution to a problem statement A few glimpses at Step 4 Implementation

18 Next Lecture Searching


Download ppt "CMPT 120 Topic:  Case Study."

Similar presentations


Ads by Google