Need Help

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
}


Observer.h
1
2
3
4
5
6
7
8
9
10
#ifndef OBSERVER_H
#define OBSERVER_H

class Observer
{
	private:
	      virtual void update() = 0;
};

#endif 


Surfer.h
1
2
3
4
5
6
7
8
9
10
#ifndef SURFER_H
#define SURFER_H
#include "Human.h"

class Surfer : public Human
{
	public:
	      Surfer(int x, int y);
};
#endif 


Surfer.cpp
1
2
3
4
5
6
#include "Surfer.h"

Surfer::Surfer(int x, int y) : Human('X', x, y)
{
    
}


Human.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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 


Human.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "Human.h"

Human::Human(char value, int x, int y)
{
	mValue = value;
	mX = x;
	mY = y;
}

char Human::getValue()
{
	return this->mValue;
}

void Human::setValue(char value)
{
	mValue = value;
}

void Human::setX(int x)
{
	mX = x;
}

void Human::setY(int y)
{
	mY = y;
}

int Human::getX()
{
	return mX;
}

int Human::getY()
{
	return mY;
}


SwimmingArea.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef SWIMMINGAREA_H
#define SWIMMINGAREA_H
#include "Surfer.h"
#include "PositionMover.h"

#include <iostream>

using namespace std;

//class Surfer;
class PositionMover;

class SwimmingArea
{
	public:
	      SwimmingArea(int inWidth, int inHeight);
	      ~SwimmingArea();
	      
	      int getWidth() const;
	      int getHeight() const;
	      Surfer* getSurfer() const;
	      PositionMover* createPositionMover();
	      void print();


	private:
	      int mWidth;
	      int mHeight;
	      Surfer* mSurfer;
};

#endif 


SwimmingArea.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "SwimmingArea.h"

SwimmingArea::SwimmingArea(int inWidth, int inHeight)
{
    mWidth = inWidth;
    mHeight = inHeight;
    
    mSurfer = new Surfer(mWidth/2, mHeight/2);
}

SwimmingArea::~SwimmingArea()
{
    mWidth = 0;
    mHeight = 0;
    
    delete mSurfer;
}

int SwimmingArea::getWidth() const
{
    return mWidth;
}

int SwimmingArea::getHeight() const
{
    return mHeight;
}

Surfer* SwimmingArea::getSurfer() const
{
    return mSurfer;
}

PositionMover* SwimmingArea::createPositionMover()
{
    return new PositionMover(this);

}

void SwimmingArea::print()
{
    for (int y = 0; y < mHeight; ++y)
    {
	for ( int x = 0; x < mWidth; ++x)
	{
	    char value = ' ';
	    if (x == mSurfer->getX() && y == mSurfer->getY())
	    {
		value = mSurfer->getValue();
	    }
	    else if (x == 0 || x == mWidth-1 || y == 0 || y == mHeight - 1)
	    {
		value = '*';
	    }
	    cout << value;
	}
	cout << endl;
    }
}


PositionMover.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef POSITIONMOVER_H
#define POSITIONMOVER_H
#include "SwimmingArea.h"

class SwimmingArea;

class PositionMover
{
	private:
	      SwimmingArea* mSwimmingArea;
  
	public:
	      PositionMover(SwimmingArea* m);
	      void left();
	      void right();
	      void up();
	      void down();
};

#endif 


PositionMover.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "SwimmingArea.h"
#include "PositionMover.h"

PositionMover::PositionMover(SwimmingArea* m)
{
    mSwimmingArea = m;
}

void PositionMover::left()
{
    Surfer* surf = mSwimmingArea->getSurfer();
    surf->setX(surf->getX()-1);
}

void PositionMover::right()
{
    Surfer* surf = mSwimmingArea->getSurfer();
    surf->setX(surf->getX()+1);
}

void PositionMover::up()
{
    Surfer* surf = mSwimmingArea->getSurfer();
    surf->setY(surf->getY()-1);
}

void PositionMover::down()
{
    Surfer* surf = mSwimmingArea->getSurfer();
    surf->setY(surf->getY()+1);
}
Topic archived. No new replies allowed.