CALTECH CS 11 - Lecture 7 (36 pages)

Previewing pages 1, 2, 17, 18, 19, 35, 36 of 36 page document View the full content.
View Full Document

Lecture 7



Previewing pages 1, 2, 17, 18, 19, 35, 36 of actual document.

View the full content.
View Full Document
View Full Document

Lecture 7

67 views

Lecture Notes


Pages:
36
School:
California Institute of Technology
Course:
Cs 11 - Computer Language Shop
Computer Language Shop Documents

Unformatted text preview:

CS11 Introduction to C Spring 2013 2014 Lecture 7 Computer Strategy Game Want to write a turn based strategy game for the computer Need different kinds of units for the game Different capabilities strengths weaknesses etc Examples Infantry unit Transport ship Trebuchet Unit Classes Infantry unit data members Member functions Move to another location Attack another unit receive attack from another unit What about transport ships Current location strength experience level Who owns the unit Probably not experience level or attack a unit Need to add a list of things the ship is carrying Trebuchets are very similar to infantry units Just a different way of attacking other units Common Themes Very clear we have common themes here All units have a location strength owner All units can move around All units can be attacked Also have specialized capabilities Infantry units and trebuchets can t carry other stuff and they can t make it very far on water Of course boats can t make it very far on land Single Class Implementation Doesn t make sense to put all features in one class class GameUnit UnitType type Point location int playerID int strength For transport ships only int transportCapacity vector GameUnit contents public bool move const Map map int direction For infantry and trebuchets only bool attack const Map map int direction For transport ships only bool transportItem GameUnit u Single Class Implementation 2 Not a clean abstraction Very likely to lead to bugs e g Both data and operations for infantry units are mixed together with transport ships etc Hard to understand what is going on Infantry units can accidentally carry other units Transport ships can attack other units Also wastes space Infantry unit doesn t need list of units it s carrying C Class Inheritance C provides class inheritance Base class provides generalized capabilities A game unit type with location and owner Basic ability to move unit around retrieve owner info etc Derived classes inherit the state



View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Lecture 7 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 7 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?