Chapter 13B

advertisement
Visual C++ Programming:
Concepts and Projects
Chapter 13B:
Object-Oriented Programming
(Tutorial)
Tutorial: Maze Program
• Problem analysis
– Create a program in which a mouse navigates a
maze looking for the hidden cheese
– The maze consists of a two-dimensional array of
cells
– The Mouse is an object created from a Mouse
class definition
– Each cell in the maze is an object created from a
Cell class definition
Programming with Visual C++
2
Problem Analysis
Programming with Visual C++
3
The Mouse Class Definition
• Class variables
– Various icons of mouse facing different directions
• private data members
– Row and column locations of the mouse in the
maze (row and col)
– Icon
• private methods
– Default constructor – Mouse()
Programming with Visual C++
4
The Mouse Class Definition
(continued)
• public methods
– Initializing constructor – Mouse(int, int)
– Accessor methods
• getRow()
• getCol()
• getIcon()
– Mutator methods
• setRow()
• setCol()
Programming with Visual C++
5
The Mouse Class Definition
(continued)
• public methods
– Utility methods
•
•
•
•
goRight()
goLeft()
goUp()
goDown()
Programming with Visual C++
6
The Mouse Class Definition
(continued)
Programming with Visual C++
7
The Mouse Class Definition
(continued)
Programming with Visual C++
8
The Mouse Class Definition
(continued)
Programming with Visual C++
9
Programming with Visual C++
10
The Cell Class Definition
• private data members
– row: int – row location of Cell in maze
– col: int – column location of Cell in maze
– access: bool – indicates whether Mouse can
enter this Cell
– hasCheese: bool – indicates whether cheese is
in this Cell
• private methods
– Default constructor – Cell()
Programming with Visual C++
11
The Cell Class Definition (continued)
• public methods
– Initializing constructor – Cell(int, int)
– Accessor methods
•
•
•
•
getRow()
getCol()
getAccess()
getCheese()
– Mutator methods
• setAccess()
• setCheese()
Programming with Visual C++
12
The Cell Class Definition (continued)
Programming with Visual C++
13
Programming with Visual C++
14
The Cell Class Definition (continued)
• An initializing constructor is used to assign
each Cell a row and column location as well as
a Boolean access value
Programming with Visual C++
15
Design
• A maze consists of rows and columns
– Rows run horizontally
– Columns run vertically
• Each cell has a row and column location
Programming with Visual C++
16
Design (continued)
Programming with Visual C++
17
Design (continued)
Programming with Visual C++
18
Design (continued)
Programming with Visual C++
19
Design (continued)
• The interface contains two buttons (used to
start and stop the animation)
• A Timer control is used to perform Mouse
movement
• The Mouse moves from one Cell to another
• Previous cells are colored brown
• When the mouse reaches the cell with the
cheese, a MessageBox pops up
Programming with Visual C++
20
Design (continued)
Programming with Visual C++
21
Design (continued)
• The size of Cells and the number of rows and
columns of Cells in the maze are constants
Programming with Visual C++
22
Design (continued)
Programming with Visual C++
23
Design (continued)
• The maze is a two-dimensional array of Cells
• Use the Array template as a foundation for
this
Programming with Visual C++
24
Design (continued)
• Instance variables include the mouse, maze,
and a variable to indicate which direction the
mouse is moving
Programming with Visual C++
25
Design (continued)
• Creating the maze requires the instantiation of
320 Cell objects (20 in each row)
Programming with Visual C++
26
Design (continued)
• The Form1_Load() event will construct the
interface, complete with maze, mouse, and cheese
Programming with Visual C++
27
Design (continued)
• Drawing the maze (nested loops for rows and
columns)
Programming with Visual C++
28
Design (continued)
• Drawing and positioning the Mouse
Programming with Visual C++
29
Design (continued)
• Keep the mouse in the maze by checking for
edges
Programming with Visual C++
30
Development
•
•
•
•
Interface construction (only two buttons)
Timer control used to control movement
Form1_Load() will draw the maze initially
The maze and mouse are redrawn in each
Timer_Tick() event
Programming with Visual C++
31
Development (continued)
Programming with Visual C++
32
Development (continued)
• The Mouse and Cell class definition header
files must both be included at the top of the
client (Form1.h)
Programming with Visual C++
33
Development (continued)
• Instance variables, constants, and object handles
Programming with Visual C++
34
Development (continued)
• Form1_Load() constructs the maze
Programming with Visual C++
35
Development (continued)
• The start button creates the mouse and
cheese
Programming with Visual C++
36
Development (continued)
• The mouse and cheese icons are displayed in
Rectangles
Programming with Visual C++
37
Development (continued)
• The maze cells are drawn in drawMaze()
Programming with Visual C++
38
Development (continued)
• At each interval of the Timer, its Tick()
event:
– Redraws the maze
– Draws the mouse at its new location
• The animation can be halted with
btnStop_Click()
Programming with Visual C++
39
Development (continued)
• The Timer1_Tick() event:
– Creates a Rectangle based on the cell in which the
mouse currently resides (oldRect)
Programming with Visual C++
40
Development (continued)
• If mouse is not at edge, then move in current
direction
Programming with Visual C++
41
Development (continued)
• If mouse moves into cell with cheese, congratulations!
• If mouse was at an edge, then choose new direction
Programming with Visual C++
42
Development (continued)
• Check for edges by checking row and column
Programming with Visual C++
43
Testing
• When btnStart is clicked:
– Maze appears (16 rows x 20 columns of cells)
– Mouse and cheese appear
– Mouse moves across the maze from left to right
– When mouse reaches the edge, it turns and
follows the edges around
• Reposition the cheese so that the mouse finds
it in its path
Programming with Visual C++
44
On Your Own
• Stronger mutators
– Do not allow negative values
• Private instance methods
– verifyRow() and verifyCol() for Mouse
class
• New UML class diagram
– Add verifyRow() and verifyCol() to
Mouse UML diagram
Programming with Visual C++
45
Download