CMPT 120 Topic:  Case Study.

Slides:



Advertisements
Similar presentations
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Advertisements

Chapter 8 Arrays and Strings
Chapter 3 Planning Your Solution
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
CS1020E Sitin 1 Discussion -- Counting Palindromes.
General Programming Introduction to Computing Science and Programming I.
Chapter 2 - Algorithms and Design
Chapter 8 Arrays and Strings
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Input, Output, and Processing
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
Introduction to Computing Science and Programming I
CMPT 120 Topic: Python’s building blocks -> More Statements
Topic: Introduction to Computing Science and Programming + Algorithm
Topic: File Input/Output (I/O)
Introduction to Programming
Fundamentals of Python: First Programs
Topic: Python’s building blocks -> Variables, Values, and Types
String Methods Programming Guides.
Topics Designing a Program Input, Processing, and Output
Topic: Recursion – Part 2
Topic: Functions – Part 1
Topic: Iterative Statements – Part 1 -> for loop
INTRODUCTION TO PROBLEM SOLVING
Introduction to Programming
CMPT 120 Topic: Python Modules.
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Possible exam questions with Scenarios
Topic: Python’s building blocks -> Statements
Topic: Conditional Statements – Part 1
Chapter 2: Input, Processing, and Output
Topic: Python’s building blocks -> Variables, Values, and Types
for Repetition Structures
CMPT 120 Topic: Searching – Part 1
CMPT 120 Topic: Functions – Part 4
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to Programming
CMPT 120 Topic: Python strings.
CS1371 Introduction to Computing for Engineers
Topic: Functions – Part 2
Getting Started with C.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Engineering Innovation Center
Using a Stack Chapter 6 introduces the stack data type.
Chapter 12 Recursion (methods calling themselves)
Introduction to Programming
Python I/O.
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Lesson Objectives Aims You should be able to:
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
CS 1111 Introduction to Programming Fall 2018
Introduction to Programming
Introduction to Programming
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Flowcharts and Pseudo Code
Topics Designing a Program Input, Processing, and Output
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Introduction to Programming
Topic: Iterative Statements – Part 2 -> for loop
Primary School Computing
Lecture 31 – Practice Exercises 7
Lecture 17 – Practice Exercises 3
Lecture 20 – Practice Exercises 4
Lecture 20 – Practice Exercises 4
Lecture 17 – Practice Exercise 3 SOLUTIONS
Presentation transcript:

CMPT 120 Topic:  Case Study

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 …

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

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

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

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

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

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.

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.

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

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

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…

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

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

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

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

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

Next Lecture Searching