Interface


Interface - Strategy pattern


Bee
Ninja
Fighter

FlyingThing

Bird
Albatross
Canary
Penguin
Pterodactyl

FlyStrategy
DontFly
FlyByFlapping
FlyByGliding

Harness

Read chapter 8, 9, 10


Classwork


Part A

Add to your project this class Position, which has x and y coordinates. 

Create an abstract class GameElt, which has a String name, an int health (keep it in the range 0 to 100) and a Position pos.

For GameElt, include a default constructor that starts pos at (0, 0), and a parameterized constructor that takes x and y coordinates and a name.  health should always start at 100.

Create a class BigDarnHero which inherits from GameElt. Override toString so that when we print a hero, we see something like "Thondar the Hero (3, 17)" based on the name and the position. Other than toString and two constructors taking the same parameters as in GameElt, you do not need to add anything else for this class at this time.


Part B

Create an interface MoveStrategy which requires methods

  The idea is that moving in a given direction will change the given position, and chanceToFall will return true if a fall happened or false otherwise.  How this happens will depend on the implementation in the actual classes.

  Moving North or South will change the Y coordinate (vertical movement) either +1 (North) or -1 (South) moving East or West will change the X coordinate (horizontal movement) (+1 East or -1 West).  Direction will be passed as "N", "S", "E", or "W".

 

Write a class WalkMoveStrategy which implements MoveStrategy. When walking, the position changes by 1 in the direction chosen, and a message like "Walking from (7,1) to (7, 2)." should also be printed out.

People who are walking have a 1 in 20 chance of falling.  In chanceToFall choose a random number and use it to determine if chanceToFall returns true.

You do not need to add anything other than the methods to implement the strategy, not even a constructor

 

Change GameElt so that it also has an instance variable moveStrat of type MoveStrategy.

Add to GameElt a method move(direction) which calls moveStrat's move method, passing it the direction given and GameElt's pos instance variable.  Then call chanceToFall and if the GameElt fell while moving, print a statement about this and reduce health by 5.

In all constructors for BigDarnHero, set the MoveStrategy to a new instance of WalkMoveStrategy.

In a main program, create a BigDarnHero named "Mighty Thog" at (5, 3) and have them walk three moves north and one west. Print the hero before they move, and again after they have moved.


Part C

Add another class RunMoveStrategy which implements MoveStrategy, and whose move method changes the position by 5 in the direction given, as well as printing something like "Running from (9, 3) to (4, 3). Boy my mighty thews are tired." 

People who run have a 1 in 10 chance of falling. 

To BigDarnHero, add a method speedUp() which changes the hero's MoveStrategy to a RunMoveStrategy, and a method slowDown which changes the MoveStrategy to a WalkMoveStrategy.

In the main program, have Mighty Thog move around, speed up, move around, slow down, and move around again.


Part D

Add another strategy for movement, RandomCursedMoveStrategy, which changes the position by a random amount in a random direction, no matter what direction is passed in, and prints out something like "I moved from (2, 3) to (2, 961), truly, I am accursed and shall never get to class on time".

People moving by this strategy have a 50/50 chance of falling and hurting themselves.

In the main program, create a hero, set this as their movement strategy, and add some movement for them.


Part E [EC]

Create a class Horse which inherits from GameElt and also implements MoveStrategy. So a Horse is a thing in the game and also a way for things in the game to move. So it will have both move(p, dir) and move(dir). (hint: does it need to write both of these?)

Horses move 10 steps in the given direction every time they move, and a message like "Mr. Ed gallops like the wind from (5,0) to (-5, 0)." should also print out.

There is a 1 in 30 chance of the horse falling.

Make sure that all constructors set the Horse's MoveStrategy to the horse itself.

In the main program, create a Horse at (4, 1) and have them go two moves east and four moves south.

Create a BigDarnHero at (2,6) and set their MoveStrategy to a new Horse at the same position. Have the hero ride three moves west.

Make sure that when a horse is used to move something else, the horse's position changes accordingly as well.