discussion9_ans

discussion9_ans - CS31 Introduction to Computer Science I...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS31: Introduction to Computer Science I Discussion 9 May 27, 2011 TA: Paul Wais ( [email protected] ) Discussion Section 1L Information TA: Paul Wais ( [email protected] ) Office Hours: Thursdays 11:30am - 1:30pm in Boelter Hall 2432 Discusion Website: http://www.cs.ucla.edu/ ˜ pwais 1 Project 7 Project 7 is posted! It’s due next Thursday, there’s no final report to write, and it consists of filling out a skeleton program. Project 7 is not especially algorithmically hard (like Project 5 was). Project 7 is tricky because it exercises a lot of details about Classes. Today, we’ll go over examples similar to Project 7. 2 Mini Pac-Man! Let’s write a program to play a very basic version of Pac-Man . In the game, there’s a grid of ’ * ’ characters. The player, denoted as ’P’ , can move about the grid and eat stars. There are a few ghosts of different colors (e.g. r ed, g reen, and b lue) who wander about randomly. Here’s a demo: ** g ******* ********** ********** ******** r * ******** b * ********** ********** ********* P ********** ********** Enter a move: u ********** ********** ********** ********* b ******** r * ********** ********* P ********* ********** *** g ****** Enter a move: r 1 ********** ********** ********** ******** b * ********** ******** r * ******** P ********* ********** *** g ****** Enter a move: For this basic version of the game, we won’t worry about ghosts capturing Pac-Man, and there will be no walls in the grid. Furthermore, the game will run forever (even after Pac-Man has eaten everything in the grid). We’ll assume that Pac-Man’s movements wrap around the grid, so moving right when Pac-Man is on the rightmost edge of the grid results in Pac-Man moving to the leftmost edge of the grid. Furthermore, at random moments in the game, one ghost can teleport to the same location as another ghost. 3 Class Basics So far, we’ve used built-in types (such as int , double , and char ) to store data. Last time we learned that we could define our own types using structs . Today, we’re going to talk about classes , which are very similar to structs. A class is a language construct that allows us to group variables and functions into a single entity. Considering the Pac-Man game above, it will be nice to associate Pac-Man’s location and functions for moving him in a single place. Let’s define a PacMan class: 1 const int NUM_ROWS = 10; 2 const int NUM_COLS = 10; 3 4 class PacMan 5 { 6 public : 7 PacMan(); // Default constructor 8 PacMan( int r, int c); // Constructor 9 10 void move( char dir); // Moves PacMan according to user input (e.g. ’u’ to go up). 11 // This is a "modifier" or "mutator" function 12 13 int getRow() const ; // Get PacMan’s current location. This is an "accessor" function 14 int getCol() const ; // Get PacMan’s current location. This is an "accessor" function 15 16 bool isAt( int r, int c) const ; 17 18 int r; // Store PacMan’s location here 19 int c; 20 }; // End class declarations with a semicolon...
View Full Document

{[ snackBarMessage ]}

Page1 / 10

discussion9_ans - CS31 Introduction to Computer Science I...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online