diff --git a/Uppgift-3-Spel/a.out b/Uppgift-3-Spel/a.out index c1c47349945b8778a077b12755772931be7875c5..ff7a0711e04473518865d68cdc4ea0dd11b301bf 100755 Binary files a/Uppgift-3-Spel/a.out and b/Uppgift-3-Spel/a.out differ diff --git a/Uppgift-3-Spel/ghost.cc b/Uppgift-3-Spel/ghost.cc index 703cecd6e17a4e811ea0bc361bc15bac52bc6f16..02ba484bcbe197d5075a405eebbd2f4adba275ca 100644 --- a/Uppgift-3-Spel/ghost.cc +++ b/Uppgift-3-Spel/ghost.cc @@ -1,34 +1,142 @@ #include "ghost.h" -Ghost::Ghost(const string &colorName, Point const &start_position) - : color_index(colorName), pos(start_position) - {} +//ghost -void Ghost::set_pos( Point &new_pos) { - pos = new_pos; +Ghost::Ghost(std::string const &color, Point const &start_pos): +color_index(color), pos(start_pos) +{} + +void Ghost::set_pos( Point &new_pos) +{ +new_pos.x = pos.x; +new_pos.y = pos.y; } -Point Ghost::get_pos() { - return pos; + + + +Point Ghost::get_pos() +{ +return Point{pos.x, pos.y}; } -bool Blinky::get_angry(){ - return angry; + + +//clyde +Clyde::Clyde(std::string const &color, Point const &start_pos): +Ghost("orange", start_pos) +{} + + +Point Clyde::get_scatter() const +{ +return Point{0,0}; } -void Blinky::set_angry(bool state) { - angry = state; + +Point Clyde::get_pos() +{ +return Point{pos.x, pos.y}; } + +void Clyde::set_pos( Point &new_pos) +{ +Ghost::set_pos(new_pos); +} + -Blinky::Blinky(std::string const &color, Point const &start_pos) + +string Clyde::get_color() const +{ +return "orange"; +} +Point Clyde::get_chase() const +{ + + Point pac_dir = pac.get_direction(); + Point pac_pos = pac.get_position(); + int proximity = abs(pos.x - pos_pacman.x) + abs(pos.y - pos_pacman.y); + + if (proximity >= 2) + { + return pos_pacman; + } + else + { + return Point{0,0}; + } + + +} + +//pinky + +Pinky::Pinky(std::string const &color, Point const &start_pos): +Ghost("pink", start_pos) +{} + + +Point Pinky::get_scatter() +{ +return Point{0,21}; +} + + +Point Pinky::get_pos() +{ +return Point{pos.x, pos.y}; +} + +void Pinky::set_pos( Point &new_pos) +{ +Ghost::set_pos(new_pos); +} + + + +string Pinky::get_color() const +{ +return "pink"; +} +Point Pinky::get_chase() +{ + + Point pac_dir = pac.get_direction(); + Point pac_pos = pac.get_position(); + + if(pac_dir.x == 0 && pac_dir.y == -1) + { + pac_pos.y = (pac_pos.y - 2); + } + else if(pac_dir.x == 0 && pac_dir.y == 1) + { + pac_pos.y = (pac_pos.y + 2); + +} + else if(pac_dir.x == -1 && pac_dir.y == 0) + { + pac_pos.x = (pac_pos.x -2 ); + + } + else if(pac_dir.x == 1 && pac_dir.y == 0) + { + pac_pos.x = (pac_pos.x -2); + + + } + return pac_pos; +} + +//blinky +Blinky::Blinky(std::string const &color, Point const &start_pos, Pacman pac) : Ghost("red", start_pos) {} -void Blinky::set_pos(Point &new_pos) { +void Blinky::set_pos(Point &new_pos) { set_pos(new_pos); } -Point Blinky::get_scatter(const Point &pos_pacman) { +Point Blinky::get_scatter() { if (angry == true) { return Point{pos_pacman.x, pos_pacman.y}; // stämmer? @@ -38,14 +146,26 @@ Point Blinky::get_scatter(const Point &pos_pacman) { } } -Point Blinky::get_chase(const Point &pos_pacman, const Point &dir_pacman) { +Point Blinky::get_chase() { return Point{pos_pacman.x, pos_pacman.y}; } -Point Blinky::get_pos() { +Point Blinky::get_pos() { return pos; } -std::string Blinky::get_color() { +std::string Blinky::get_color()const +{ return "pink"; -} \ No newline at end of file +} + +bool Blinky::get_angry(){ + return angry; +} + +void Blinky::set_angry(bool state) { + angry = state; +} + + + diff --git a/Uppgift-3-Spel/ghost.h b/Uppgift-3-Spel/ghost.h index 8f5078368bc4fd68f5ba0ef7577ebfb413ba4bc2..8c6074517f1e7ad72107a67ba38e69aef7735aba 100644 --- a/Uppgift-3-Spel/ghost.h +++ b/Uppgift-3-Spel/ghost.h @@ -1,59 +1,78 @@ +#ifndef GHOST_H +#define GHOST_H + #include "given.h" #include "string" using namespace std; - class Ghost { public: - Ghost(std::string const &color, Point const &start_pos); - virtual void set_pos( Point &new_pos); - virtual Point get_scatter(const Point &pos_pacman) = 0; - virtual Point get_chase(const Point &pos_pacman, const Point &dir_pacman) = 0; - virtual Point get_pos(); - virtual std::string get_color()const = 0; - virtual ~Ghost() = default; +Ghost(std::string const &color, Point const &start_pos); +virtual void set_pos( Point &new_pos); +virtual Point get_scatter() = 0; +virtual Point get_chase(const Point &pos_pacman, const Point &dir_pacman) = 0; +virtual Point get_pos(); +virtual std::string get_color()const = 0; +virtual ~Ghost() = default; + + + public: - std::string color_index; - Point pos; +std::string color_index; +Point pos; + }; + class Clyde: public Ghost + { -public: - Clyde(std::string const &color, Point const &start_pos); - void set_pos(Point &new_pos); - Point get_scatter(const Point &pos_pacman); - Point get_chase(const Point &pos_pacman, const Point &dir_pacman); - Point get_pos(); - std::string get_color()const; + public: +Clyde(std::string const &color, Point const &start_pos); +void set_pos(Point &new_pos) override; +Point get_scatter() const; +Point get_chase() const; +Point get_pos() override; +std::string get_color()const; + + + }; class Pinky: public Ghost + { -public: - Pinky(std::string const &color, Point const &start_pos); - void set_pos(Point &new_pos); - Point get_scatter(const Point &pos_pacman); - Point get_chase(const Point &pos_pacman, const Point &dir_pacman); - Point get_pos(); - std::string get_color()const; + public: +Pinky(std::string const &color, Point const &start_pos, Pacman pac); +void set_pos(Point &new_pos)override; +Point get_scatter() override; +Point get_chase() const; +Point get_pos()override; +std::string get_color()const; + + + }; class Blinky : public Ghost { -public: - bool get_angry(); - void set_angry(bool state); - Blinky(std::string const &color, Point const &start_pos); - void set_pos(Point &new_pos); - Point get_scatter(const Point &pos_pacman); - Point get_chase(const Point &pos_pacman, const Point &dir_pacman); - Point get_pos(); - std::string get_color(); + public: +bool get_angry(); +void set_angry(bool state); +Blinky(std::string const &color, Point const &start_pos, Pacman pac); +void set_pos(Point &new_pos)override; +Point get_scatter(); +Point get_chase(); // Blinky::get_chase(const Point&)’ marked ‘override’, but does not override +Point get_pos()override; +std::string get_color()const; + private: - bool angry; +bool angry = false ; + + }; +#endif \ No newline at end of file diff --git a/Uppgift-3-Spel/main.cc b/Uppgift-3-Spel/main.cc index 38bbfc157c07c9346b267bc101938fde0cee97ee..86fbd0604f598493c9324fd094621d5c722aa441 100644 --- a/Uppgift-3-Spel/main.cc +++ b/Uppgift-3-Spel/main.cc @@ -1,8 +1,11 @@ #include "ghost.h" +//#include "given.h" #include <string> #include <iostream> #include <iomanip> #include <sstream> +#include <vector> +#include <cctype> using namespace std; @@ -10,11 +13,11 @@ using namespace std; Ledning och Tips: - Modifiera stukturen till en header-fil och en implementationsfil - - Utöka 'run()' och 'draw_map()' med övrig funktionalitet. - - Lägg alla spöken i en lämplig behållare som en datamedlem. + - Ut�ka 'run()' och 'draw_map()' med �vrig funktionalitet. + - L�gg alla sp�ken i en l�mplig beh�llare som en datamedlem. - Bryt ut stora kodblock till egna funktioner. - - Använd hjälpfunktioner för att undvika duplicering av kod. - - Tänk på att varje funktion inte borde vara längre än 25 rader. + - Anv�nd hj�lpfunktioner f�r att undvika duplicering av kod. + - T�nk p� att varje funktion inte borde vara l�ngre �n 25 rader. */ class Ghost_Tester @@ -22,9 +25,16 @@ class Ghost_Tester public: - Ghost_Tester() - : pacman {} - { + Pacman pacman; + vector<Ghost*> all_ghosts; + + Ghost_Tester(): + pacman {} ,all_ghosts{} + { + + all_ghosts.push_back(new Clyde("orange", Point{5,5})); + all_ghosts.push_back(new Blinky("red", Point{6,6})); + all_ghosts.push_back(new Pinky("pink", Point{7,7})) ; } void run() @@ -41,6 +51,31 @@ public: string command {}; iss >> command; + for (Ghost* e : all_ghosts) + { + if (command == e -> get_color()) + { + Point new_pos {}; + iss >> new_pos.x >> new_pos.y; + e -> set_pos(new_pos); + } + else if (command == "chase") + { + e -> set_pos(e -> get_chase()); + } + else if (command == "scatter") + { + e -> set_pos(e -> get_scatter()); + } + else if (command == "anger") + { + // behöver vara blinky + if(e -> get_color() == "red") { + dynamic_cast<Blinky*>(e) -> set_angry(true); + } + } + } + if (command == "pos") { Point new_pos {}; @@ -52,15 +87,25 @@ public: } else if (command == "quit") { + for (Ghost* e : all_ghosts) + { + delete e; + } + break; } } } + + + + + //fixa följande funltion private: /* - En hjälpfunktion som avgör vilka två tecken som ska ritas ut för en given position på + En hj�lpfunktion som avg�r vilka tv� tecken som ska ritas ut f�r en given position p� spelplanen. */ string to_draw(Point const& curr_pos) @@ -76,13 +121,13 @@ private: } /* - En hjälpfunktion för att rita ut spelplanen för testprogrammet. + En hj�lpfunktion f�r att rita ut spelplanen f�r testprogrammet. - Itererar över varje rad och column i kartan. Index för raderna är flippade för att placera - y = 0 längst ned. + Itererar �ver varje rad och column i kartan. Index f�r raderna �r flippade f�r att placera + y = 0 l�ngst ned. - Varje punkt i kartan ritas som två tecken eftersom ett tecken i terminalen är ca dubbelt så - högt som det är brett. + Varje punkt i kartan ritas som tv� tecken eftersom ett tecken i terminalen �r ca dubbelt s� + h�gt som det �r brett. */ void draw_map() { @@ -101,7 +146,8 @@ private: cout << "+" << setfill('-') << setw(WIDTH * 2) << "-" << "+" << endl; } - Pacman pacman; + + }; int main() @@ -110,4 +156,3 @@ int main() gt.run(); return 0; } - diff --git a/Uppgift-3-Spel/main.o b/Uppgift-3-Spel/main.o deleted file mode 100644 index e9dfced89aa5f4db9b4709672a8b8850d1b9d22f..0000000000000000000000000000000000000000 Binary files a/Uppgift-3-Spel/main.o and /dev/null differ