Skip to content

Instantly share code, notes, and snippets.

@suragch
Created April 22, 2022 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suragch/00a287af78c8b9e92913dd10a27926f3 to your computer and use it in GitHub Desktop.
Save suragch/00a287af78c8b9e92913dd10a27926f3 to your computer and use it in GitHub Desktop.
High and low level navigation
#include <iostream>
using namespace std;
class Point {
int x;
int y;
public:
Point(int X, int Y) {
x = X;
y = Y;
}
int getX() {
return x;
}
int getY() {
return y;
}
// (2, 4)
void print() {
cout << "(" << x << ", " << y << ")";
}
};
class AbstractNavigation {
virtual void goHome() = 0;
virtual void goSchool() = 0;
virtual void setSchoolLocation(Point point) = 0;
virtual void setHomeLocation(Point point) = 0;
virtual Point getCurrentLocation() = 0;
};
class LowLevelNavigation {
int x = 0;
int y = 0;
public:
void goOrigin() {
x = 0;
y = 0;
}
void goRight3() {
x += 3;
}
void goLeft2() {
x -= 2;
}
void goUp3() {
y -= 3;
}
void goDown2() {
y += 2;
}
};
class HighLevelNavigation : AbstractNavigation {
LowLevelNavigation navigator = LowLevelNavigation();
Point schoolLocation = Point(0, 0);
Point homeLocation = Point(0, 0);
int currentX = 0;
int currentY = 0;
public:
void goHome() {
navigator.goOrigin();
int x = homeLocation.getX();
for (int i = 0; i < x; i++) {
navigator.goRight3();
navigator.goLeft2();
currentX++;
}
int y = homeLocation.getY();
for (int i = 0; i < y; i++) {
navigator.goDown2();
navigator.goDown2();
navigator.goUp3();
currentY++;
}
}
void goSchool() {
// TODO
}
void setSchoolLocation(Point point) {
schoolLocation = point;
}
void setHomeLocation(Point point) {
homeLocation = point;
}
Point getCurrentLocation() {
return Point(currentX, currentY);
}
};
int main() {
auto busDriver = HighLevelNavigation();
// busDriver.setSchoolLocation(Point(2, 4));
busDriver.setHomeLocation(Point(1, 1));
busDriver.goHome();
busDriver.getCurrentLocation().print();
// busDriver.goSchool();
// busDriver.getCurrentLocation().print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment