Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on OO Programming Our programs will no longer just be a main method or a main which calls a collection of other methods –instead, a program will be.

Similar presentations


Presentation on theme: "More on OO Programming Our programs will no longer just be a main method or a main which calls a collection of other methods –instead, a program will be."— Presentation transcript:

1 More on OO Programming Our programs will no longer just be a main method or a main which calls a collection of other methods –instead, a program will be a group of objects that work together to solve a problem –the main method will create objects and then call upon those objects to perform tasks This is a different kind of programming strategy than we previously had where we just listed instructions to execute –we have to now think about what types of objects are needed to create a game that comprises multiple objects of different types of classes

2 Example: Adventure Game Consider a 1-player adventure game, just you against the various monsters you will fight –lets create a class called Adventurer the Adventurer will be an object with –hit points –weapons –armor –other items like treasure, food, potions, etc and the Adventurer will need to have methods to –move to a new room, search a room, pick up items found –fight another Adventurer –change weapons/armor, eat food, take potions, cast spells, rest We can create other objects –Adventurer objects (with their own hit points and armor) –Weapons and Armor that can be picked up, put down, and used/worn –Food that, when eaten, might heal the player –Rooms that contain Adventurers and other items (Weapons, Food)

3 The Adventurer Class We will assume an Adventurer can be either the human player (the user) or a Creature to fight or even cooperate with –What will describe an Adventurer? a name and/or type (e.g., Ogre, Wizard, etc) its hit points (how much damage it can take before dying) how many attacks it gets per turn the maximum amount of damage it can do per attack its defense (how hard it is for another Adventurer to attack it or to hit it) what it currently is holding (Weapon, Armor, Food, etc)

4 Adventurer Methods The previous list will make up the class’ instance data, but what about methods? –we need a constructor to initialize the Adventurer’s values so that we can create a new Adventurer with code like this: Adventurer a1 = new Adventurer(“Fred”, “Fighter”, 31, 2, 8, 14, null, null); –Fred is a Fighter with 31 hit points who can attack twice per turn for a maximum damage of 8, and a defense of 14, Fred currently has no weapons or armor –We will need to write methods for attack – attack another Adventurer (monster, creature) isAttacked – when this Adventurer is attacked, how much damage has he/she/it taken on? isAlive – is this Adventurer still alive? pickUpWeapon – add a Weapon to what it is carrying similarly for Armor and Treasure and Food and accessors for each of its instance variables

5 The attacks Method How do we get one Adventurer to attack another? –the code may look like this: –while(a1.isAlive( ) && a2.isAlive( )) { a1.attacks(a2); if(a2.isAlive( )) a2.attacks(a1); } So how does attacks work? –a1 can attack numAttacks time (numAttacks is an instance data), so we use a for loop to iterate that many times –each time, we roll a 20 sided die, if the roll > a2.getDefense( ) then this attack hits a2 –we generate the damage done as a random number from 1 to maxDamageAmount (the amount of damage we can cause in one attack): damage = g.nextInt(maxDamageAmount) + 1; –now we have to tell a2 that we have hit him for that amount: a2.isAttacked(damage);

6 Objects Interacting with Objects Notice how one Object (a1) can interact with another (a2) This is the basis for OOP, having objects interacting with each other in different ways: –Object containing Object (Adventurer has a Weapon, Room has a Weapon or Treasure) –Object operating positively with an Object (Adventurer picks up a Weapon) –Object operating negatively with an Object (Adventurer attacks another Adventurer and causes the other Adventurer to lose hit points) We need to properly implement our methods and classes for these types of interactions

7 Classes Containing Classes Lets expand our Adventure game as follows: –the Adventurer (user) will be in a Room which contains objects –a Room is a class that has instance data for: a creature to fight (of type Adventurer), items lying around (some type of Treasure, a discarded Weapon, some Armor) We might define a Room like this: –Room r1 = new Room(new Adventurer(“Ogre”, “Fighter”, 16, 1, 12, 11), new Treasure(“Gold”, 25), null, new Armor(“Chain Mail”, 15); –thus, r1 is a Room that contains an Ogre to fight and 25 pieces of gold, but no Weapon, and a piece of Chain Mail offering 15 defense points –the Ogre has 16 hit points, 1 attack per turn of 12 maximum damage points, and defense of 11 The Room class might have methods –to access each instance data and to remove or add items to the Room for instance, if we kill the Ogre, we can take the Treasure and thus remove it from the room or we might pick up the Chain Mail and drop our own Leather Armor, thus we drop one piece of armor and add another

8 Making a Dungeon/Adventure Space So we can define a Room, how about multiple Rooms? –Room[ ] rooms = new Room[5]; our dungeon has 5 rooms –rooms[0] = new Room(…); –rooms[1] = new Room(…); we populate each Room individually by specifying in the … what Adventurer (monster), Treasure, Weapon, Armor is in the room, if any We can connect Rooms together too, lets assume we add these instance data to the Room class: –Room north, south, east, west If we do rooms[0].north = rooms[1]; –Then room 0 connects to room 1 by going north out of rooms[0] –of course we should also do rooms[1].south = rooms[0]; or else it is a 1-way trip through that doorway!

9 A Full Adventure Game In order to create a full game: –you need to define each class Room, Weapon, Armor, Treasure, Adventurer –each class needs proper constructors and accessor methods (an accessor method for each instance data) and probably a toString to describe the object –additional methods as needed: take an object from a Room, drop an object in a Room (the Adventurer and Room classes will need methods to perform this) go from Room to Room (the Adventurer and the Room classes will need methods to perform this) rest or eat food or drink potions to heal (Adventurer will need this) Before starting, design the entire thing!


Download ppt "More on OO Programming Our programs will no longer just be a main method or a main which calls a collection of other methods –instead, a program will be."

Similar presentations


Ads by Google