Hey guys, I'm busy writing a program to practice implementing the Iterator & Observer design patterns.
I'm stuck at a pure virtual function a want to implement inside of Lifeguard.cpp named update(). Here is a description of what the function should do:
This function is the reaction in the Observer pattern. The lifeguard should check if the surfer is
on the observer border. Each lifeguard only observes the borders directly connected to him/her. Hence
the top-left lifeguard will only check the left and top borders, therefore checking if the surfer's coordinates
are on the border (x or y coordinates equal to zero). The other lifeguards will obviously have to check
different borders, depending on the swimming areas width and height. Since each lifeguard is positioned
at a corner, you can get the x and y coordinates of the lifeguard and compare it to the coordinates of the
surfer. If the surfer gets onto the border, the lifeguard should blow the whistle (print this to screen) and
force the surfer back into the area. Hence the surfer will never be able to get onto the border (`*'). You
can force the surfer back by directly setting the coordinates of the surfer one position back.
Any help in how to implement this will be greatly appreciated, thanks in advance =]
Here is my program so far:
Lifeguard.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifndef LIFEGUARD_H
#define LIFEGUARD_H
#include "Observer.h"
#include "Surfer.h"
class Lifeguard
{
private:
Lifeguard(int x, int y, Surfer* surf);
public:
Surfer* mSurfer;
};
#endif
Lifeguard.cpp
1 2 3 4 5 6 7 8 9 10 11
#include "Lifeguard.h"
Lifeguard::Lifeguard(int x, int y, Surfer* surf) : Human('L', x, y)
{
mSurfer = surf;
}
void Lifeguard::update()
{
//how to implement
}
#ifndef HUMAN_H
#define HUMAN_H
class Human
{
public:
Human(char value, int x, int y);
void setValue(char value);
void setX(int x);
void setY(int y);
char getValue();
int getX();
int getY();
private:
char mValue;
int mX;
int mY;
};
#endif