4.6 FishBot
A fish robot that alternates between two strategies so it is harder for predators to predict its motion.
Purpose
The purpose of this assignment is to motivate the use of Strategies instead of “hard-coding” individual behavior into an object.
Introduction
A fish that is startled moves to get away from a predator. If a fish always does the same behavior to escape, it is easy for a predator to predict what will happen and the fish will get eaten. In order to avoid getting eaten, a fish alternates between darting forward and darting to the left.
-
DartForward: aStrategyof moving forward three times -
DartLeft:aStrategyof turning left and moving forward once -
FishBotis a kind of (our)StrategyLayerthat remembers two different strategies and alternates betweewn them.public void startle()activates the startle reflex, running the current strategy for escapepublic void swapStrategy()switches between the two strategies that the fish knows
Difficulties
- Confusion between
StrategyLayerandStrategy.- How are they actually related?
- Can both be created? (
new Strategy()vsnew StrategyLayer(...))
- What is the relationship between
FishBotandStrategyLayer? - Too-specific names for things:
iFishBotStrategyfor the interface- variables of type
DartStrategyandDodgeLeftStrategyinstead of justStrategy
Some of these difficulties should have come up with the ArmyCaricature, so we need to revisit that!
Last modified August 25, 2021: ap-cs 2017-2018 (3bb9976)