1 CSC 222: Computer Programming II Spring 2004 Structuring data & class design  Hunt the Wumpus game  Cave class  CaveMaze class.

Slides:



Advertisements
Similar presentations
1 CSC 222: Computer Programming II Spring 2004 HW1 review arrays vs. vectors class design object-oriented design data objects + functionality example:
Advertisements

CSC 221: Computer Programming I Fall 2001  conditional repetition  repetition for: simulations, input verification, input sequences, …  while loops.
C++ Classes & Data Abstraction
1 CSC 222: Computer Programming II Spring 2004  vectors  library, templated  advantages over arrays:  const-reference parameters  growable vectors.
1 CSC 222: Computer Programming II Spring 2004 Queues and simulation  queue ADT  enqueue (push), dequeue (pop), front, empty, size  library  application:
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Lecture 10:Control Structures II (Repetition) Introduction to Computer Science Spring 2006.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
Searching Arrays Linear search Binary search small arrays
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Introduction to Object- Oriented Programming Prof. Shie-Jue Lee Dept. of Electrical Engineering National Sun Yat-sen University.
Program Input and the Software Design Process ROBERT REAVES.
1 Techniques of Programming CSCI 131 Lecture 29 Search.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
1 CSC 222: Computer Programming II Spring 2004 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search Class design  templated.
1 CSC 222: Object-Oriented Programming Spring 2012 Searching and sorting  sequential search  algorithm analysis: big-Oh, rate-of-growth  binary search.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Programming Principles Chapter 1. Objectives Discuss the program design process. Introduce the Game of Life. Discuss object oriented design. – Information.
File I/O ifstreams and ofstreams Sections 11.1 &
Pointers OVERVIEW.
Implementation of the Hangman Game in C++
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 Command Processor II. 2 Command Processor Example Let's look at a simple example of a command processor using states and cities. Get initial information.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
1 CSC 222: Computer Programming II Spring 2004  classes and objects  abstract data types, classes and objects  using existing classes, #include, member.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
Functions and Libraries. Reference parameters void setToZero(int var) { var = 0; } int main() { int var = 0; setToZero(var); cout
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
1 C++ Classes and Data Structures Course link…..
Searching Arrays Linear search Binary search small arrays
CSCE 210 Data Structures and Algorithms
CSC 222: Object-Oriented Programming Spring 2017
CSC 222: Object-Oriented Programming Fall 2015
Chapter 1.2 Introduction to C++ Programming
לולאות קרן כליף.
Quiz Next Monday.
Multiple Files Revisited
More About File Reading
אבני היסוד של תוכנית ב- C++
אבני היסוד של תוכנית ב- C++
Today’s Lecture I/O Streams Tools for File I/O
Standard Input/Output Stream
CSC 270 – Survey of Programming Languages
CSC 581: Mobile App Development
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
CS150 Introduction to Computer Science 1
(Dreaded) Quiz 2 Next Monday.
File I/O in C++ II.
Reading from and Writing to Files
Presentation transcript:

1 CSC 222: Computer Programming II Spring 2004 Structuring data & class design  Hunt the Wumpus game  Cave class  CaveMaze class

2 Hunt the Wumpus: OO design first step: identify the object and their properties/behaviors CaveMaze: list of caves, # of wumpi, # of grades, current location, alive? move down a tunnel to an adjacent cave toss a grenade down a tunnel to an adjacent cave show info about current location determine how many grenades remaining determine how many wumpi remaining determine if alive Cave: cave number, name, contents, visited?, adjacent caves get cave numberget cave name get cave contentschange cave contents get adjacent cavedetermine if visited before mark as visited

3 Class prototypes enum CaveContents {EMPTY, WUMPUS, PIT, BATS}; class Cave { public: Cave(int num, string name, int left, int straight, int right); void SetContents(CaveContents c); void MarkAsVisited(); CaveContents GetContents() const; bool HasVisited() const; int GetCaveNum() const; string GetCaveName() const; int GetAdjacent(int tunnel) const; private: // PRIVATE DATA FIELDS AND/OR MEMBER FUNCTIONS }; class CaveMaze { public: CaveMaze(string filename); void Move(int tunnel); void Toss(int tunnel); void ShowLocation() const; bool StillAlive() const; bool StillWumpi() const; private: // PRIVATE DATA FIELDS AND/OR MEMBER FUNCTIONS }; as before: can design the classes based on behavior before having to decide on data structures

4 Hunt the Wumpus program #include #include "CaveMaze.h" using namespace std; const string MAZE_FILE = "caves.dat"; void DisplayIntro(); int main() { CaveMaze maze(MAZE_FILE); DisplayIntro(); while (maze.StillAlive() && maze.StillWumpi()) { maze.ShowLocation(); char action; int target; cin >> action >> target; if (action == 't') { maze.Toss(target); } else { maze.Move(target); } cout << endl << "GAME OVER" << endl; return 0; } note: user input is crude what if user enters action other than 't' or 'm'? what if target 3?

5 Cave class enum CaveContents {EMPTY, WUMPUS, PIT, BATS}; class Cave { public: Cave(int num=-1, string name="", int left=-1, int straight=-1, int right=-1); void SetContents(CaveContents c); void MarkAsVisited(); CaveContents GetContents() const; bool HasVisited() const; int GetCaveNum() const; string GetCaveName() const; int GetAdjacent(int tunnel) const; private: vector adjacentCaves; int caveNum; string caveName; bool visited; CaveContents contents; }; a Cave object caveNum = 0 caveName = "The Fountainhead" visited = false contents = EMPTY adjacentCaves = MEMBER FUNCTIONS e.g., Cave start(0, "The Fountainhead", 1, 4, 9); start.HasVisited()  falsestart.GetContents()  EMPTY start.GetCaveNum()  0 start.GetCaveName()  "unknown" start.MarkAsVisited()start.GetCaveName()  "The Fountainhead" start.GetAdjacent(1)  1start.GetAdjacent(2)  4

6 CaveMaze class class CaveMaze { public: CaveMaze(string filename); void Move(int tunnel); void Toss(int tunnel); void ShowLocation() const; bool StillAlive() const; bool StillWumpi() const; private: vector caves; int numGrenades; int numWumpi; int currentLoc; bool alive; int FindEmpty() const; void MoveAdjacentWumpi(); bool AdjacentTo(CaveContents room) const; }; a CaveMaze object numGrenades = 8 numWumpi = 2 currentLoc = 0 alive = true caves = MEMBER FUNCTIONS to navigate the maze, use currentLoc and vector of caves caves[currentLoc]  Cave object where currently located caves[currentLoc].GetContents()  contents of current location caves[currentLoc].GetAdjacent(2)  cave number connected to middle tunnel

7 MazeClass constructor CaveMaze::CaveMaze(string filename) { ifstream myin(filename.c_str()); assert(myin); int numCaves; myin >> numCaves; caves.resize(numCaves); string name; int num, left, straight, right; while (myin >> num >> left >> straight >> right && getline(myin, name)) { Cave c(num, name, left, straight, right); caves[num] = c; } currentLoc = 0; caves[currentLoc].MarkAsVisited(); Die d(caves.size()/4); numWumpi = d.Roll(); numGrenades = 4*numWumpi; caves[currentLoc].SetContents(WUMPUS); for (int i = 0; i < numWumpi; i++) { caves[FindEmpty()].SetContents(WUMPUS); } caves[FindEmpty()].SetContents(BATS); caves[FindEmpty()].SetContents(PIT); caves[currentLoc].SetContents(EMPTY); } open cave data file read number of caves & resize vector to fit repeatedly read line of cave info, create Cave object, and put in vector set initial location in maze pick number of wumpi (& grenades) place wumpi, bats & pit at random locations in the maze (uses FindEmpty to find location)

8 Cave data file (caves.dat) The Fountainhead The Rumpus Room Buford's Folly The Hall of Kings The Silver Mirror The Gallimaufry The Den of Iniquity The Findledelve The Page of the Deniers The Final Tally Ess four The Trillion The Scrofula Ephemeron Shelob's Lair The Lost Caverns of the Wyrm The Lost Caverns of the Wyrm The Lost Caverns of the Wyrm The Lost Caverns of the Wyrm The Lost Caverns of the Wyrm first, number of caves then 1 line per cave: cave number adjacent cave numbers cave name note: Lost Caverns are simulated using unidirectional links

9 Move member function void CaveMaze::Move(int tunnel) // Results: moves to the cave connected by the specified tunnel // if new cave contains WUMPUS or PIT, then killed // if new cave contains BATS, then moved at random { currentLoc = caves[currentLoc].GetAdjacent(tunnel); caves[currentLoc].MarkAsVisited(); CaveContents room = caves[currentLoc].GetContents(); if (room == WUMPUS) { cout << "You have entered the Wumpus' lair... CHOMP CHOMP CHOMP" << endl; alive = false; } else if (room == PIT) { cout << "Look out for the bottomless pit... AAAAAiiiiiiyyyeeeeeeee" << endl; alive = false; } else if (room == BATS) { cout << "A swarm of giant bats picks you up and flies away." << endl; currentLoc = FindEmpty(); caves[currentLoc].MarkAsVisited(); } update currentLoc and mark new location as visited check the contents of the new location to see if killed or moved – if moved, use FindEmpty to pick destination

10 HW3 for HW3, you are to complete the Hunt the Wumpus code         you must complete the member functions for the Cave class in order to make the program work. also, make user input more robust (ignore unrecognized commands)