#include // A separate representation of a player for use in this assignment // You should not modify the player struct // (In a real project the player would be a subclass of GameObject) struct Player { // Position int x; int y; // Velocity int v_x; int v_y; }; //////////////////////////////////////////////////////////////////////////// // Write your header file content here //////////////////////////////////////////////////////////////////////////// // Write your implementation file content here //////////////////////////////////////////////////////////////////////////// class GameObject { public: GameObject(int x, int y) : x{x}, y{y} { } virtual ~GameObject() = default; void move_player_here(Player& p) { p.x = x; p.y = y; } bool collides(Player const& p) const { return x == p.x && y == p.y; } virtual void affect(Player& p) const = 0; private: int x; int y; }; class Portal : public GameObject { public: Portal(int x, int y, GameObject* destination) : GameObject{x,y}, destination{destination} { } ~Portal() { delete destination; } Portal& operator=(Portal const&) = delete; Portal(Portal const&) = delete; void affect(Player& p) const override { destination->move_player_here(p); destination->affect(p); } private: GameObject* destination; }; class Bouncer : public GameObject { public: Bouncer(int x, int y, int bounce) : GameObject{x,y}, bounce{bounce} { } void affect(Player& p) const override { p.x += - p.v_x * bounce; p.y += - p.v_y * bounce; } private: int bounce; }; /////////////////////////////////////////////////////////////////////////////// // // // Reset any modification below this line before submission! // // // /////////////////////////////////////////////////////////////////////////////// #include #include #include using namespace std; void CHECK_TRUE( bool result, string if_true, string if_false); void CHECK_POSITION( Player p, int x, int y, string if_true, string if_false); int main() { Player player{12, 8, 3, 2}; CHECK_POSITION( player, 12, 8, "ok1", "fail: player should start at {12,8}"); Bouncer bouncer{12, 8, 2}; CHECK_TRUE( bouncer.collides(player), "ok2", "fail: player and bouncer should have collided"); bouncer.affect(player); CHECK_POSITION( player, 6, 4, "ok3", "fail: player should have been bounced to {6,4}"); Portal one_step_portal(6, 4, new Bouncer{16, 10, 3}); CHECK_TRUE( one_step_portal.collides(player), "ok4", "fail: player and one step portal should have collided"); one_step_portal.affect(player); CHECK_POSITION( player, 7, 4, "ok5", "fail: player should have been teleported to {16,10} and then immediately bounced to {7,4}"); Portal two_step_portal(7, 4, new Portal{0,0, new Bouncer{20, 12, 4}}); CHECK_TRUE( two_step_portal.collides(player), "ok6", "fail: player and two step portal should have collided"); two_step_portal.affect(player); CHECK_POSITION( player, 8, 4, "ok7", "fail: player should have been teleported to {20,12} via {0,0} and then immediately bounced to {8,4}"); two_step_portal.affect(player); CHECK_POSITION( player, 8, 4, "ok7", "fail: player should have been teleported to {20,12} via {0,0} and then immediately bounced to {8,4}"); return 0; } void CHECK_TRUE( bool result, string if_true, string if_false) { if (result) cout << if_true << endl; else cout << if_false << endl; } void CHECK_POSITION( Player p, int x, int y, string if_true, string if_false) { CHECK_TRUE( p.x == x && p.y == y, if_true, if_false); }