diff --git a/Uppgift-3-Spel/.vscode/settings.json b/Uppgift-3-Spel/.vscode/settings.json index c5dc643e1d07f50e01daeda529bdb508f6b96a82..9b03cb39f39809ea707cbf3c509832935aa63661 100644 --- a/Uppgift-3-Spel/.vscode/settings.json +++ b/Uppgift-3-Spel/.vscode/settings.json @@ -62,6 +62,7 @@ "ratio": "cpp", "fstream": "cpp", "iomanip": "cpp", - "stop_token": "cpp" + "stop_token": "cpp", + "bitset": "cpp" } } \ No newline at end of file diff --git a/Uppgift-3-Spel/a.out b/Uppgift-3-Spel/a.out index ff7a0711e04473518865d68cdc4ea0dd11b301bf..a398520ed6a2db900ffaef0877675112a23b5ad4 100755 Binary files a/Uppgift-3-Spel/a.out and b/Uppgift-3-Spel/a.out differ diff --git a/Uppgift-3-Spel/backup/a.out b/Uppgift-3-Spel/backup/a.out new file mode 100755 index 0000000000000000000000000000000000000000..e14343b95af6139cc53d893800edd60418090f72 Binary files /dev/null and b/Uppgift-3-Spel/backup/a.out differ diff --git a/Uppgift-3-Spel/backup/ghost.cc b/Uppgift-3-Spel/backup/ghost.cc new file mode 100644 index 0000000000000000000000000000000000000000..ffcc154676c7bfe2d44a30aa57e2c5446c35d992 --- /dev/null +++ b/Uppgift-3-Spel/backup/ghost.cc @@ -0,0 +1,171 @@ +#include "ghost.h" + +//ghost + +Ghost::Ghost(std::string const &color, Point const &start_pos, Pacman& pac) + : color_index(color), pos(start_pos), pacman{pac} +{} + +void Ghost::set_pos( Point new_pos) +{ + new_pos.x = pos.x; + new_pos.y = pos.y; +} + + + + +Point Ghost::get_pos() +{ + return Point{pos.x, pos.y}; +} + + + +//clyde +Clyde::Clyde(std::string const &color, Point const &start_pos, Pacman& pac) + : Ghost("orange", start_pos, pac) +{} + + +Point Clyde::get_scatter() +{ +return Point{0,0}; +} + + +Point Clyde::get_pos() +{ + return Point{pos.x, pos.y}; +} + +void Clyde::set_pos( Point new_pos) +{ +Ghost::set_pos(new_pos); +} + + + +string Clyde::get_color() const +{ +return "orange"; +} + +Point Clyde::get_chase() +{ + Point pac_dir = pacman.get_direction(); + Point pac_pos = pacman.get_position(); + int proximity = abs(pos.x - pac_pos.x) + abs(pos.y - pac_pos.y); + + if (proximity >= 2) + { + return pac_pos; + } + else + { + return Point{0,0}; + } + + +} + +//pinky + +Pinky::Pinky(std::string const &color, Point const &start_pos, Pacman& pac): +Ghost("pink", start_pos, pac) +{} + + +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 {pacman.get_direction()}; + Point pac_pos {pacman.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, pac) + {} + +void Blinky::set_pos(Point new_pos) { + set_pos(new_pos); +} + +Point Blinky::get_scatter() { + if (angry == true) + { + return Point{pacman.get_position()}; // stämmer? + } else + { + return Point{6, 6}; + } +} + +Point Blinky::get_chase() { + return Point{pacman.get_position()}; +} + +Point Blinky::get_pos() { + return pos; +} + +std::string Blinky::get_color()const +{ + return "pink"; +} + +bool Blinky::get_angry(){ + return angry; +} + +void Blinky::set_angry(bool state) { + angry = state; +} + + + diff --git a/Uppgift-3-Spel/backup/ghost.h b/Uppgift-3-Spel/backup/ghost.h new file mode 100644 index 0000000000000000000000000000000000000000..2dd23626afd7ba68398b0b85966c979deaf1d546 --- /dev/null +++ b/Uppgift-3-Spel/backup/ghost.h @@ -0,0 +1,61 @@ +#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, Pacman& pac); + virtual void set_pos( Point new_pos); + virtual Point get_scatter() = 0; + virtual Point get_chase() = 0; + virtual Point get_pos(); + virtual std::string get_color()const = 0; + virtual ~Ghost() = default; +protected: + std::string color_index; + Point pos; + Pacman& pacman; +}; + +class Clyde: public Ghost +{ +public: + Clyde(std::string const &color, Point const &start_pos, Pacman& pac); + void set_pos(Point new_pos) override; + Point get_scatter() override ; + Point get_chase() override ; + Point get_pos() override; + std::string get_color()const; +}; + +class Pinky: public Ghost +{ +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() override ; + 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, Pacman& pac); + void set_pos(Point new_pos)override; + Point get_scatter() override; + Point get_chase() override; // Blinky::get_chase(const Point&)’ marked ‘override’, but does not override + Point get_pos()override; + std::string get_color()const; +private: + bool angry = false ; +}; + +#endif \ No newline at end of file diff --git a/Uppgift-3-Spel/backup/given.cc b/Uppgift-3-Spel/backup/given.cc new file mode 100644 index 0000000000000000000000000000000000000000..154bfe8f81b4c120c571f60e7e97b3480c0d1f87 --- /dev/null +++ b/Uppgift-3-Spel/backup/given.cc @@ -0,0 +1,46 @@ +#include "given.h" + +int WIDTH = 19; +int HEIGHT = 22; + +bool operator==(Point const& lhs, Point const& rhs) +{ + return lhs.x == rhs.x && lhs.y == rhs.y; +} + +std::istream& operator>>(std::istream& is, Point& rhs) +{ + return is >> rhs.x >> rhs.y; +} + +Pacman::Pacman() + : pos {}, dir {1,0} +{} + +Point Pacman::get_position() const +{ + return pos; +} + +void Pacman::set_position(Point const& p) +{ + if (p.x > WIDTH or p.x < 0 or p.y > HEIGHT or p.y < 0) + { + throw std::runtime_error("position outside valid range"); + } + pos = p; +} + +Point Pacman::get_direction() const +{ + return dir; +} + +void Pacman::set_direction(Point const& p) +{ + if (p.x > 1 or p.x < -1 or p.y > 1 or p.y < -1 or abs(p.x + p.y) != 1) + { + throw std::runtime_error("direction outside valid range"); + } + dir = p; +} diff --git a/Uppgift-3-Spel/backup/given.h b/Uppgift-3-Spel/backup/given.h new file mode 100644 index 0000000000000000000000000000000000000000..92b57b82966db209d7c3b0d63a7344dfe33a4fd1 --- /dev/null +++ b/Uppgift-3-Spel/backup/given.h @@ -0,0 +1,60 @@ +/* + I denna fil finns ett utval av kod fr�n det dina kollegor skrivit till ert pacman-projekt. Det �r + precis det som du beh�ver f�r att skriva din del av koden. + + Tips: Du f�r ut�ka denna fil med saker som du beh�ver f�r att ditt program ska kompilera. Det �r + �ven okej att ut�ka Point med fler operatorer om det skulle beh�vas. + */ + + +#include <stdexcept> +#include <iostream> + + +/* + Globala variabler f�r storlek p� spelplanen. + + Din kollega som skrev detta �r medveten om att globala variabler �r d�ligt och borde undvikas, men + har inte haft tid att �tg�rda det. I den slutgiltiga versionen av koden borde dessa s�klart l�sas + fr�n en fil med konfigurationsdata. + */ +extern int WIDTH; +extern int HEIGHT; + + +/* + Ett aggregat som anv�nd f�r att representera punkter och riktninar p� spelplanen. + Aggregatet har ocks� hj�lpsamma operatorer som kan underl�tta din implementation. + + */ +struct Point +{ + int x; + int y; +}; +bool operator==(Point const& lhs, Point const& rhs); +std::istream& operator>>(std::istream& is, Point& rhs); + + +/* + En klass f�r att representera spelarfiguren. Detta �r en nedskalad version j�mf�rt med det som + kollegan skrev till spelet. Notera att din kod inte beh�ver �ndras om man skulle g� fr�n denna + implementaion till den faktiska implementationen. + */ +class Pacman +{ +public: + + Pacman(); + + Point get_position() const; + void set_position(Point const& p); + + Point get_direction() const; + void set_direction(Point const& p); + +private: + + Point pos {}; + Point dir {}; +}; diff --git a/Uppgift-3-Spel/backup/main.cc b/Uppgift-3-Spel/backup/main.cc new file mode 100644 index 0000000000000000000000000000000000000000..bbfa9277c5ef66a1ae0e1c4ac44046d63bc52015 --- /dev/null +++ b/Uppgift-3-Spel/backup/main.cc @@ -0,0 +1,158 @@ +#include "ghost.h" +//#include "given.h" +#include <string> +#include <iostream> +#include <iomanip> +#include <sstream> +#include <vector> +#include <cctype> + +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. + - 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. + */ + +class Ghost_Tester +{ + +public: + + Pacman pacman; + vector<Ghost*> all_ghosts; + + Ghost_Tester(): + pacman {} ,all_ghosts{} + { + + all_ghosts.push_back(new Clyde("orange", Point{5,5}, pacman)); + all_ghosts.push_back(new Blinky("red", Point{6,6}, pacman)); + all_ghosts.push_back(new Pinky("pink", Point{7,7}, pacman)) ; + } + + void run() + { + while(true) + { + draw_map(); + cout << "> "; + + string line {}; + getline(cin, line); + istringstream iss {line}; + + 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 {}; + iss >> new_pos.x >> new_pos.y; + pacman.set_position(new_pos); + } + else if (command == "dir") + { + } + 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� + spelplanen. + */ + string to_draw(Point const& curr_pos) + { + string to_draw{" "}; + + if (pacman.get_position() == curr_pos) + { + to_draw[1] = '@'; + } + + return to_draw; + } + + /* + 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. + + 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() + { + cout << "+" << setfill('-') << setw(WIDTH * 2) << "-" << "+\n"; + + for (int y {HEIGHT - 1}; y >= 0; --y) + { + cout << "|"; + for (int x {}; x < WIDTH; ++x) + { + cout << to_draw( Point{x,y} ); + } + cout << "|\n"; + } + + cout << "+" << setfill('-') << setw(WIDTH * 2) << "-" << "+" << endl; + } + + + +}; + +int main() +{ + Ghost_Tester gt {}; + gt.run(); + return 0; +} diff --git a/Uppgift-3-Spel/full_game/Code/constants.h b/Uppgift-3-Spel/full_game/Code/constants.h deleted file mode 100644 index b977ac644d38217f83b93c880450282c7813ca51..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/Code/constants.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef CONSTANTS_H -#define CONSTANTS_H - -/* - Konstanter för olika aspekter av spelet. Dessa borde läsas från en fil. - - (inline använd för att slippa lägga definitionen av konstanterna i en cc-fil. Ej en del av - kursen!) - */ -inline int const WIDTH { 19 }; -inline int const HEIGHT { 22 }; -inline int TILE_SIZE { 20 }; - -inline int const SCREEN_WIDTH { WIDTH * (TILE_SIZE + 1) }; -inline int const SCREEN_HEIGHT { HEIGHT * (TILE_SIZE + 1) }; -inline int const FPS { 60 }; - -#endif diff --git a/Uppgift-3-Spel/full_game/Code/entity.cc b/Uppgift-3-Spel/full_game/Code/entity.cc deleted file mode 100644 index 65e143c075a191c180c4f167c139bad0d7b1744b..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/Code/entity.cc +++ /dev/null @@ -1,38 +0,0 @@ -#include "entity.h" - -#include "constants.h" - -Entity::Entity(sf::Vector2f const& start_position, Grid& grid, int speed, sf::Color const& color) - : current_target { start_position }, - previous_target {}, - current_direction {}, - grid { grid }, - speed { speed } -{ - setPosition(start_position); - setFillColor(color); -} - -void Entity::update() -{ - if (at_target_position()) - { - sf::Vector2f tmp { current_target }; - select_new_target(current_target, current_direction); - previous_target = tmp; - } - move_forward(); - - time_passed += 1.0 / FPS; -} - -bool Entity::at_target_position() const -{ - return abs(getPosition().x - current_target.x) < 0.1 and - abs(getPosition().y - current_target.y) < 0.1; -} - -void Entity::move_forward() -{ - move(static_cast<sf::Vector2f>(current_direction * speed)); -} diff --git a/Uppgift-3-Spel/full_game/Code/entity.h b/Uppgift-3-Spel/full_game/Code/entity.h deleted file mode 100644 index 0824a6e9376a86728660a6be6f8b68d3ba65c6b1..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/Code/entity.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef ENTITY_H -#define ENTITY_H - -#include <SFML/Graphics.hpp> -#include <cmath> - -#include "grid.h" - -/* - Typdeklaration för att representera en position i kartan. Går i diskreta steg där varje steg representerar - en ruta. - */ -using Point = sf::Vector2i; - -/* - Basklass som representerar alla ritbara rörliga objekt i spelet. Har härledda klasser för pacman och spöken. - */ -class Entity : public sf::ConvexShape -{ -public: - Entity(sf::Vector2f const& start_position, Grid& grid, int speed, sf::Color const& color); - virtual ~Entity() = default; - - /* - Uppdaterar objektets position. Varje Entity väljer en position en ruta framåt eller bakåt i x- - eller y-led och flyttar sig sedan med en jämn hastighet dit. När den når den rutan väljer den - en ny ruta och börjar om. Sättet den väljer en ruta sker med select_new_target() och - implementeras av den härledda klassen. - */ - virtual void update(); - -protected: - bool at_target_position() const; - virtual void move_forward(); - - /* - Uppdaterar current_target till en ny position. Ska vara en ruta horisontellt eller vertikalt - bort från nuvarande position. - */ - virtual void select_new_target(sf::Vector2f& current_target, - sf::Vector2i& current_direction) const = 0; - - sf::Vector2f current_target; - sf::Vector2f previous_target; - sf::Vector2i current_direction; - Grid& grid; - int speed; - - float time_passed {}; -}; - -#endif diff --git a/Uppgift-3-Spel/full_game/Code/game.cc b/Uppgift-3-Spel/full_game/Code/game.cc deleted file mode 100644 index f42e556b4b3752e10ea1b0d9ca5d4685647bdd63..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/Code/game.cc +++ /dev/null @@ -1,90 +0,0 @@ -#include "game.h" - -#include "ghost.h" - -Game::Game() - : grid{"map.txt"}, - pacman {{200, 340}, grid}, - ghosts {}, - window {sf::VideoMode {static_cast<unsigned>(SCREEN_WIDTH), static_cast<unsigned>(SCREEN_HEIGHT)}, "Pacman" } -{ - // Lägger till tre dynamiskt allokerade spöken i en vektor. - // Ett spökes konstruktor tar argumenten (pacman, startposition, scatterposition, + speciella argument). - ghosts.emplace_back(sf::Vector2f{360, 40}, grid, new Blinky{pacman, Point{}, Point{WIDTH, 0}}); - ghosts.emplace_back(sf::Vector2f{40 , 40}, grid, new Pinky {pacman, Point{}, Point{0, 0}}); - ghosts.emplace_back(sf::Vector2f{360, 420}, grid, new Clyde{pacman, Point{}, Point{0, HEIGHT}, 8}); - window.setFramerateLimit(FPS); -} - -void Game::run() -{ - bool running {true}; - while (running) - { - window.clear(); - - sf::Event event; - while (window.pollEvent(event)) - { - if (event.type == sf::Event::Closed) - { - window.close(); - running = false; - } - } - - update(); - draw(); - - if (check_collision()) - { - std::cout << "Du kolliderade med ett spöke..." << std::endl; - running = false; - } - - if (grid.food_count() == 0) - { - std::cout << "Du åt upp all mat! Bra jobbat!!" << std::endl; - running = false; - } - } -} - -void Game::update() -{ - pacman.update(); - - for (Ghost_Wrapper & g : ghosts) - { - g.update(); - } -} - -bool Game::check_collision() const -{ - for (Ghost_Wrapper const& g : ghosts) - { - if (pacman.getGlobalBounds().intersects(g.getGlobalBounds())) - { - return true; - } - } - return false; -} - -void Game::draw() -{ - grid.draw(window); - window.draw(pacman); - for (Ghost_Wrapper const& g : ghosts) - { - window.draw(g); - } - - window.display(); -} - -int Game::get_score() -{ - return pacman.get_score(); -} diff --git a/Uppgift-3-Spel/full_game/Code/game.h b/Uppgift-3-Spel/full_game/Code/game.h deleted file mode 100644 index 9f1cf214e788c87e2a705704c2282323dc0927b3..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/Code/game.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef GAME_H -#define GAME_H - -#include "grid.h" -#include "entity.h" -#include "pacman.h" -#include "ghost_wrapper.h" -#include "constants.h" - -/* - Spelklassen har det yttersta ansvaret för spelet. Klassen sköter huvudloopen och deligerar ansvar till andra klasser. - */ -class Game -{ -public: - /* - Skapa en spelinstans och alla objekt i spelvärden. De är just nu hårdkodade till specifika - positioner. Detta borde läsas från kart-filen på ett snyggt sätt. - */ - Game(); - - /* - Huvudloopen i spelet. Det är här allt utgår ifrån. Hanterar användarindata, deligerar till andra - funktioner för att uppdatera spellogik och rendera till fönster. Gör också checkar för om spelet - är slut. - */ - void run(); - - /* - Uppdatera spellogik. tex flytta pacman och alla spöken. - */ - void update(); - - /* - Kolla om pacman har kolliderat med någon av spökena. - */ - bool check_collision() const; - - /* - Rendera alla objekt i spelfönstret. - */ - void draw(); - - /* - Beräkna antal poäng spelaren har samlat på sig just nu. - */ - int get_score(); - -private: - Grid grid; - Pacman pacman; - std::vector<Ghost_Wrapper> ghosts; - sf::RenderWindow window; -}; - -#endif diff --git a/Uppgift-3-Spel/full_game/Code/ghost_wrapper.cc b/Uppgift-3-Spel/full_game/Code/ghost_wrapper.cc deleted file mode 100644 index 9ceb1ecfddfc082b1bcfa122ee58d7ca476e331a..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/Code/ghost_wrapper.cc +++ /dev/null @@ -1,108 +0,0 @@ -#include "ghost_wrapper.h" - -#include <cmath> -#include <algorithm> - -#include "constants.h" - -Ghost_Wrapper::Ghost_Wrapper(sf::Vector2f const& start_position, Grid& grid, Ghost* ghost) - : Entity {start_position, grid, 1, get_sfml_color(ghost)}, ghost {ghost} -{ - setPointCount(7); - - // Plockar fram ett antal punkter runt en cirkel för att skapa den ikoniska pacman-formen. - float radius { static_cast<float>(TILE_SIZE) / 2 }; - int no_points { 5 }; - for (int i {}; i < no_points; ++i) - { - setPoint(i, sf::Vector2f{ std::cos(3.14f / (no_points - 1) * i) * radius, - -std::sin(3.14f / (no_points - 1) * i) * radius }); - } - setPoint(5, {-radius, radius}); - setPoint(6, { radius, radius}); - -} - -void Ghost_Wrapper::update() -{ - if ((chase_mode && time_passed > 10) or - (!chase_mode && time_passed > 5)) - { - chase_mode = !chase_mode; - time_passed = 0; - - current_direction = -current_direction; - std::swap(current_target, previous_target); - } - - Entity::update(); -} - -// Ghost AI genomgång: https://gameinternals.com/understanding-pac-man-ghost-behavior -void Ghost_Wrapper::select_new_target(sf::Vector2f& current_target, - sf::Vector2i& current_direction) const -{ - sf::Vector2i curr { to_grid_pos(current_target) }; - sf::Vector2i goal {}; - - // Plocka fram den nya 'goal'-punkten. Detta är olika för alla spöken. - ghost->set_position(to_grid_pos(getPosition())); - if (chase_mode) - { - goal = ghost->get_chase_point(); - } - else - { - goal = ghost ->get_scatter_point(); - } - - // Hitta alla punkter precis brevid spöket. Detta är de möjliga nästa destinationerna. - std::vector<sf::Vector2i> potential_points { {curr.x + 1, curr.y}, {curr.x - 1, curr.y}, - {curr.x, curr.y + 1}, {curr.x, curr.y - 1} }; - - // Sortera punkterna så att punkten närmast 'goal'-punkten är först. - std::sort(potential_points.begin(), potential_points.end(), - [&goal](sf::Vector2i const& p1, sf::Vector2i const& p2) - { - double p1_dist { pow(p1.x - goal.x, 2) + pow(p1.y - goal.y, 2) }; - double p2_dist { pow(p2.x - goal.x, 2) + pow(p2.y - goal.y, 2) }; - return p1_dist < p2_dist; - }); - - // Hitta den första punkten som inte är en vägg, eller positionen som spöket precis var på. - sf::Vector2i best_point = *std::find_if(potential_points.begin(), potential_points.end(), - [this](sf::Vector2i const& p) - { - return not grid.wall_at(p) and p != to_grid_pos(previous_target); - }); - - // Uppdatera spökets riktningsvariabel. Kollar om spöket står stilla för att undvika division med noll. - sf::Vector2f new_direction = (to_world_pos(best_point) - current_target); - float length = sqrt( (new_direction.x * new_direction.x) + - (new_direction.y * new_direction.y)); - if (length != 0) - { - current_target = to_world_pos(best_point); - current_direction = static_cast<sf::Vector2i>(new_direction / length); - } - else - { - current_direction = {}; - } -} - -sf::Color Ghost_Wrapper::get_sfml_color(Ghost* ghost) const -{ - std::string color {ghost->get_color()}; - if (color == "red" ) return sf::Color::Red; - if (color == "green" ) return sf::Color::Green; - if (color == "blue" ) return sf::Color::Blue; - if (color == "orange") return sf::Color{255,165,0}; - if (color == "yellow") return sf::Color::Yellow; - if (color == "pink" ) return sf::Color{255,105,180}; - if (color == "purple") return sf::Color{128,0,128}; - if (color == "brown" ) return sf::Color{139,69,19}; - if (color == "gray" ) return sf::Color{128,128,128}; - if (color == "black" ) return sf::Color::Black; - return sf::Color::White; -} diff --git a/Uppgift-3-Spel/full_game/Code/ghost_wrapper.h b/Uppgift-3-Spel/full_game/Code/ghost_wrapper.h deleted file mode 100644 index 5fc976d53710a884fb69abe47f3de27ad43b1315..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/Code/ghost_wrapper.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef GHOST_WRAPPER_H -#define GHOST_WRAPPER_H - -#include <SFML/Graphics.hpp> -#include <memory> - -#include "entity.h" -#include "grid.h" -#include "YOUR_CODE_HERE/ghost.h" - -/* - En klass som äger ett objekt av den studentskapade datatypen "Ghost". Se detta som en klass som - lägger till grafikelement och sökalgoritm till klasserna som studenten skapat. - - */ -class Ghost_Wrapper : public Entity -{ -public: - Ghost_Wrapper(sf::Vector2f const& start_position, Grid& grid, Ghost* ghost); - - /* - Utökar Entitys update med funktionallitet för att byta mellan chase och scatter. - - Anropar sedan Entitys update. Kolla där för detaljer. - */ - void update() override; - -protected: - - /* - Väljer en ny punkt för spöket att gå till. Detta görs när spöket har nått sitt förra mål. - - Notera att detta inte är samma sak som det studenten ska implementera. Detta är ruta för ruta - och använder sig av studentkod, men de är inte samma. - */ - void select_new_target(sf::Vector2f& current_target, - sf::Vector2i& current_direction) const override; - -private: - - /* - Konverterar den färg-strängen som spöket sparar till en faktiskt sfml-färg. - - Följ mönstret för att lägga till fler färger. - */ - sf::Color get_sfml_color(Ghost* ghost) const; - - std::unique_ptr<Ghost> ghost; - bool chase_mode {true}; -}; - -#endif diff --git a/Uppgift-3-Spel/full_game/Code/given.h b/Uppgift-3-Spel/full_game/Code/given.h deleted file mode 100644 index b97738d6fc1db0de57f417fc4eb7520e3ece64ca..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/Code/given.h +++ /dev/null @@ -1,6 +0,0 @@ -/* - Detta är en liten wrapper för att studentkoden inte ska behöva ändras för den läggs in i spelet. - Utan detta skulle man behöva ändra vilka filer som inkluderas i studentkoden. - */ -#include "constants.h" -#include "pacman.h" diff --git a/Uppgift-3-Spel/full_game/Code/grid.cc b/Uppgift-3-Spel/full_game/Code/grid.cc deleted file mode 100644 index d067166b06ce8b8f0f822b9d82ae5b3e307a1e40..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/Code/grid.cc +++ /dev/null @@ -1,135 +0,0 @@ -#include "grid.h" - -#include <fstream> -#include <deque> -#include <algorithm> - -#include "constants.h" - -std::istream& operator>>(std::istream& is, Row& row) -{ - std::string line {}; - getline(is, line); - std::istringstream iss {line}; - - if (line.size() != WIDTH) - { - is.setstate(std::ios::failbit); - } - - row.clear(); - - char c {}; - while(iss >> c) - { - row.push_back(c); - } - - if ( row.size() != 0 and row.size() != WIDTH ) - { - throw std::runtime_error{"wrong width [" + std::to_string(row.size()) + "] of row. Expected " + std::to_string(WIDTH)}; - } - return is; -} - -sf::Vector2i to_grid_pos(sf::Vector2f const& pos) -{ - return { static_cast<int>((pos.x - TILE_SIZE) / TILE_SIZE), - static_cast<int>((pos.y - TILE_SIZE) / TILE_SIZE) }; -} - -sf::Vector2f to_world_pos(sf::Vector2i const& pos) -{ - return { static_cast<float>(pos.x * TILE_SIZE + TILE_SIZE), - static_cast<float>(pos.y * TILE_SIZE + TILE_SIZE) }; -} - -Grid::Grid(std::string const& map_file) -{ - std::ifstream ifs {map_file}; - - Row row {}; - while (ifs >> row) - { - rows.push_back(row); - } - - if (rows.size() != HEIGHT) - { - throw std::runtime_error{"wrong height [" + std::to_string(rows.size()) + "] of map. Expected " + std::to_string(HEIGHT)}; - } -} - -void Grid::draw(sf::RenderWindow& window) const -{ - // Skapa en rektangel med rätt storlek och färg. Flyttas till olika positioner och ritas ut - // en gång för varje vägg. - sf::RectangleShape box {{TILE_SIZE / 1.5f, TILE_SIZE / 1.5f}}; - box.setFillColor(sf::Color::Blue); - box.setOrigin(box.getSize().x / 2,box.getSize().y / 2); - - // Skapa en cirkel med rätt storlek och färg. Flyttas till olika positioner och ritas ut en gång - // för varje mat. - sf::CircleShape food {2}; - food.setFillColor(sf::Color::White); - food.setOrigin(food.getRadius(), food.getRadius()); - - for (unsigned i {}; i < rows.size(); ++i) - { - for (unsigned j {}; j < rows[i].size(); ++j) - { - switch (rows[i][j]) - { - case 'w': - box.setPosition((j + 1) * TILE_SIZE, (i + 1) * TILE_SIZE); - window.draw(box); - break; - case 'f': - food.setPosition((j + 1) * TILE_SIZE, (i + 1) * TILE_SIZE); - window.draw(food); - break; - default: - break; - } - } - } -} - -bool Grid::wall_at(sf::Vector2i const& grid_pos) const -{ - if (grid_pos.x < 0 or grid_pos.x >= static_cast<int>(rows[0].size()) or - grid_pos.y < 0 or grid_pos.y >= static_cast<int>(rows.size()) ) - { - return true; - } - - return rows[grid_pos.y][grid_pos.x] == 'w'; -} - -bool Grid::eat_food(sf::Vector2i const& grid_pos) -{ - if (rows[grid_pos.y][grid_pos.x] == 'f') - { - rows[grid_pos.y][grid_pos.x] = 'e'; - return true; - } - return false; -} - - -int Grid::food_count() const -{ - int count {}; - - for (unsigned i {}; i < rows.size(); ++i) - { - for (unsigned j {}; j < rows[i].size(); ++j) - { - if (rows[i][j] == 'f') - { - ++count; - } - } - } - return count; -} diff --git a/Uppgift-3-Spel/full_game/Code/grid.h b/Uppgift-3-Spel/full_game/Code/grid.h deleted file mode 100644 index 6005b99da73f5b08406ff1442382bf1117022689..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/Code/grid.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef GRID_H -#define GRID_H - -#include <SFML/Graphics.hpp> -#include <iostream> -#include <sstream> -#include <string> - -/* - Datatyp för att representera en rad i rutnätet som är kartan. - */ -using Row = std::vector<char>; -std::istream& operator>>(std::istream& is, Row& row); - -/* - Hjälpfunktioner för att konvertera mellan två olika coordinatsystem. - - 'grid' är ett coordinatsystem där varje ruta på spelplanen är en coordinat. - 'world' är ett coordinatsystem där varje pixel på skärmen är en coordinat. - */ -sf::Vector2i to_grid_pos(sf::Vector2f const& pos); -sf::Vector2f to_world_pos(sf::Vector2i const& pos); - -/* - Klass som representerar hela kartan. Dvs håller koll på alla positioner för väggar och mat på - kartan. - */ -class Grid -{ -public: - - Grid(std::string const& map_file); - - /* - Rendera alla väggar och all mat. - */ - void draw(sf::RenderWindow& window) const; - - /* - Ger sant om det är en vägg på given position. Notera att positionen måste anges i - 'grid'-systemet. - */ - bool wall_at(sf::Vector2i const& grid_pos) const; - - /* - Ger sant om det var en mat på given position. Plockar då också bort maten. Notera att - positionen måste anges i 'grid'-systemet. - - Om det inte var en mat på positionen returneras 'false' och inget mer händer. - */ - bool eat_food(sf::Vector2i const& grid_pos); - - int food_count() const; - -private: - std::vector<Row> rows {}; -}; - -#endif diff --git a/Uppgift-3-Spel/full_game/Code/pacman.cc b/Uppgift-3-Spel/full_game/Code/pacman.cc deleted file mode 100644 index 4134a8e3895e4975062619b1ec7724eb203bb5d6..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/Code/pacman.cc +++ /dev/null @@ -1,92 +0,0 @@ -#include "pacman.h" - -#include <cmath> - -#include "constants.h" - -Pacman::Pacman(sf::Vector2f const& start_position, Grid& grid) - : Entity{start_position, grid, 2, sf::Color::Yellow}, - next_direction {}, score {} -{ - int no_points { 8 }; - float radius { 10 }; - - setPointCount(no_points); - - for (int i {}; i < no_points; ++i) - { - setPoint(i, sf::Vector2f{ std::cos(6.28f / (no_points) * i) * radius, - std::sin(6.28f / (no_points) * i) * radius }); - } -} - -void Pacman::update() -{ - handle_input(); - - if (next_direction == -current_direction) - { - current_direction = next_direction; - std::swap(current_target, previous_target); - } - - if (grid.eat_food(to_grid_pos(getPosition()))) - { - score += 10; - } - - Entity::update(); -} - -Point Pacman::get_position() const -{ - return { static_cast<int>((getPosition().x - TILE_SIZE) / TILE_SIZE), - static_cast<int>((getPosition().y - TILE_SIZE) / TILE_SIZE) }; -} - -Point Pacman::get_direction() const -{ - return { current_direction.x, current_direction.y }; -} - -void Pacman::select_new_target(sf::Vector2f& current_target, - sf::Vector2i& current_direction) const -{ - // Plockar fram potentiella nästa punkter. Det är antingen en position framåt i spelarens - // riktning, eller stå stilla. - std::vector<sf::Vector2i> candidates { next_direction, current_direction }; - - // Stega igenom listan med potentiella punkter och plocka den första som inte är en vägg. - for (sf::Vector2i const& candidate : candidates) - { - sf::Vector2f new_target { current_target + static_cast<sf::Vector2f>(candidate * static_cast<int>(TILE_SIZE)) }; - if ( not grid.wall_at(to_grid_pos(new_target)) ) - { - current_direction = candidate; - current_target = new_target; - return; - } - } - current_direction = {}; -} - -void Pacman::handle_input() -{ - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) - { - next_direction = {0, -1}; - } - else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) - { - next_direction = {0, 1}; - } - else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) - { - next_direction = {-1, 0}; - } - else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) - { - next_direction = {1, 0}; - } -} - diff --git a/Uppgift-3-Spel/full_game/Code/pacman.h b/Uppgift-3-Spel/full_game/Code/pacman.h deleted file mode 100644 index 325b1b4731d5a6c3b7fd07813e2555a1e224f5b2..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/Code/pacman.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef PACMAN_H -#define PACMAN_H - -#include <SFML/Graphics.hpp> - -#include "entity.h" -#include "grid.h" - -/* - Pacmanklassen representerar spelarefiguren i spelet. Den innehåller funktionallitet för att flytta och rendera spelarefiguren. - - Är en specialicering av basklassen Entity. - */ -class Pacman : public Entity -{ -public: - Pacman(sf::Vector2f const& start_position, Grid& grid); - - /* - Uppdatera pacmans position och riktning. - */ - void update() override; - - /* - Hämta ut olika egenskaper från pacman. - */ - unsigned get_score() const { return score; }; - Point get_position() const; - Point get_direction() const; - -protected: - - /* - Använder användainmatning för att beräkna vilken nästa ruta spelare ska röra sig till är. - */ - void select_new_target(sf::Vector2f& current_target, - sf::Vector2i& current_direction) const override; - -private: - - /* - Kollar vilka knappar som är nedtryckta och uppdaterar variabeln next_direction baserat på det. - - Ändra i denna om du vill ha andra knappar som styr spelaren. - */ - void handle_input(); - - sf::Vector2i next_direction; - unsigned score; -}; - -#endif diff --git a/Uppgift-3-Spel/full_game/Makefile b/Uppgift-3-Spel/full_game/Makefile deleted file mode 100644 index dad0d525f4e6aacb07dc27f2abbb30a9ce6c663c..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -FLAGS = -std=c++17 -Wall -Wextra -Wpedantic -Weffc++ -g -I. -ICode -IYOUR_CODE_HERE -LIB = -lsfml-graphics -lsfml-system -lsfml-window - -pacman: main.cc game.o entity.o pacman.o ghost_wrapper.o grid.o student_code.o - g++ $(FLAGS) -o pacman main.cc game.o entity.o pacman.o ghost_wrapper.o grid.o student_code.o $(LIB) - -student_code.o: $(shell find YOUR_CODE_HERE -type f) - g++ -o student_code.o $(FLAGS) -c YOUR_CODE_HERE/*.cc - -%.o: Code/%.cc Code/%.h - g++ $(FLAGS) -o $@ -c $< - -.PHONY: clean -clean: - rm -f *.o ./pacman diff --git a/Uppgift-3-Spel/full_game/README.md b/Uppgift-3-Spel/full_game/README.md deleted file mode 100644 index 480b7544cfe063a5dbd9b1a4fcc681b917f569f7..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/README.md +++ /dev/null @@ -1,14 +0,0 @@ -Steg för att spela pacman med dina egna spöken. - -1. Lägg en (1) headerfil med kod för alla spöken i mappen YOUR_CODE_HERE. Den ska heta ghost.h. -2. Lägg en (1) implemenationsfil med kod för alla spöken i mappen YOUR_CODE_HERE. -3. Flytta *inte* med huvudprogrammet eller de givna filerna till mappen. -4. Stå i mappen med filen som heter Makefile och skriv 'make' i terminalen. -5. Kör programmet med './pacman'. -6. Fly från dina spöken. - -Om du har problem med att kompilera koden, se till att alla klasser och funktioner heter som angivet -i labbinstruktionen. Det går också att gå in i spelkoden och kolla vad som väntas. Det är primärt -filerna Code/ghost_wrapper.h och Code/game.h som är relevanta. - -Prata med en assistent om ni inte får det att kompilera. diff --git a/Uppgift-3-Spel/full_game/main.cc b/Uppgift-3-Spel/full_game/main.cc deleted file mode 100644 index 06a9467c7507c62e6224063f56e014e3239bb093..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/main.cc +++ /dev/null @@ -1,20 +0,0 @@ - -#include <SFML/Graphics.hpp> -#include <iostream> - -#include "game.h" - -using namespace std; - -int main() -{ - cout << "Välkommen till Pacman\nTryck 'Enter' för att starta"; - cin.get(); - - Game game {}; - game.run(); - - cout << "Du fick " << game.get_score() << " poäng" << std::endl; - cout << "Tryck 'Enter' för att avsluta" << std::endl; - cin.get(); -} diff --git a/Uppgift-3-Spel/full_game/map.txt b/Uppgift-3-Spel/full_game/map.txt deleted file mode 100644 index f757795aa8f5022b016aef42ce607b6196b87919..0000000000000000000000000000000000000000 --- a/Uppgift-3-Spel/full_game/map.txt +++ /dev/null @@ -1,22 +0,0 @@ -wwwwwwwwwwwwwwwwwww -wffffffffwffffffffw -wfwwfwwwfwfwwwfwwfw -wfwwfwwwfwfwwwfwwfw -wfffffffffffffffffw -wfwwfwfwwwwwfwfwwfw -wffffwfffwfffwffffw -wwwwfwwwewewwwfwwww -wwwwfweeeeeeewfwwww -wwwwfwewwwwwewfwwww -eeeefeewwwwweefeeee -wwwwfwewwwwwewfwwww -wwwwfweeeeeeewfwwww -wwwwfwewwwwwewfwwww -wffffffffwffffffffw -wfwwfwwwfwfwwwfwwfw -wffwfffffffffwfwffw -wwfwfwfwwwwwfwfwfww -wffffwfffwffffffffw -wfwwwwwwfwfwwwwwwfw -wfffffffffffffffffw -wwwwwwwwwwwwwwwwwww diff --git a/Uppgift-3-Spel/ghost.cc b/Uppgift-3-Spel/ghost.cc index 02ba484bcbe197d5075a405eebbd2f4adba275ca..ffcc154676c7bfe2d44a30aa57e2c5446c35d992 100644 --- a/Uppgift-3-Spel/ghost.cc +++ b/Uppgift-3-Spel/ghost.cc @@ -2,14 +2,14 @@ //ghost -Ghost::Ghost(std::string const &color, Point const &start_pos): -color_index(color), pos(start_pos) +Ghost::Ghost(std::string const &color, Point const &start_pos, Pacman& pac) + : color_index(color), pos(start_pos), pacman{pac} {} -void Ghost::set_pos( Point &new_pos) +void Ghost::set_pos( Point new_pos) { -new_pos.x = pos.x; -new_pos.y = pos.y; + new_pos.x = pos.x; + new_pos.y = pos.y; } @@ -17,18 +17,18 @@ new_pos.y = pos.y; Point Ghost::get_pos() { -return Point{pos.x, pos.y}; + return Point{pos.x, pos.y}; } //clyde -Clyde::Clyde(std::string const &color, Point const &start_pos): -Ghost("orange", start_pos) +Clyde::Clyde(std::string const &color, Point const &start_pos, Pacman& pac) + : Ghost("orange", start_pos, pac) {} -Point Clyde::get_scatter() const +Point Clyde::get_scatter() { return Point{0,0}; } @@ -36,10 +36,10 @@ return Point{0,0}; Point Clyde::get_pos() { -return Point{pos.x, pos.y}; + return Point{pos.x, pos.y}; } -void Clyde::set_pos( Point &new_pos) +void Clyde::set_pos( Point new_pos) { Ghost::set_pos(new_pos); } @@ -50,16 +50,16 @@ string Clyde::get_color() const { return "orange"; } -Point Clyde::get_chase() const + +Point Clyde::get_chase() { - - 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); + Point pac_dir = pacman.get_direction(); + Point pac_pos = pacman.get_position(); + int proximity = abs(pos.x - pac_pos.x) + abs(pos.y - pac_pos.y); if (proximity >= 2) { - return pos_pacman; + return pac_pos; } else { @@ -71,8 +71,8 @@ Point Clyde::get_chase() const //pinky -Pinky::Pinky(std::string const &color, Point const &start_pos): -Ghost("pink", start_pos) +Pinky::Pinky(std::string const &color, Point const &start_pos, Pacman& pac): +Ghost("pink", start_pos, pac) {} @@ -87,7 +87,7 @@ Point Pinky::get_pos() return Point{pos.x, pos.y}; } -void Pinky::set_pos( Point &new_pos) +void Pinky::set_pos( Point new_pos) { Ghost::set_pos(new_pos); } @@ -101,8 +101,8 @@ return "pink"; Point Pinky::get_chase() { - Point pac_dir = pac.get_direction(); - Point pac_pos = pac.get_position(); + Point pac_dir {pacman.get_direction()}; + Point pac_pos {pacman.get_position()}; if(pac_dir.x == 0 && pac_dir.y == -1) { @@ -128,18 +128,18 @@ Point Pinky::get_chase() } //blinky -Blinky::Blinky(std::string const &color, Point const &start_pos, Pacman pac) - : Ghost("red", start_pos) +Blinky::Blinky(std::string const &color, Point const &start_pos, Pacman& pac) + : Ghost("red", start_pos, pac) {} -void Blinky::set_pos(Point &new_pos) { +void Blinky::set_pos(Point new_pos) { set_pos(new_pos); } Point Blinky::get_scatter() { if (angry == true) { - return Point{pos_pacman.x, pos_pacman.y}; // stämmer? + return Point{pacman.get_position()}; // stämmer? } else { return Point{6, 6}; @@ -147,7 +147,7 @@ Point Blinky::get_scatter() { } Point Blinky::get_chase() { - return Point{pos_pacman.x, pos_pacman.y}; + return Point{pacman.get_position()}; } Point Blinky::get_pos() { diff --git a/Uppgift-3-Spel/ghost.h b/Uppgift-3-Spel/ghost.h index 8c6074517f1e7ad72107a67ba38e69aef7735aba..2dd23626afd7ba68398b0b85966c979deaf1d546 100644 --- a/Uppgift-3-Spel/ghost.h +++ b/Uppgift-3-Spel/ghost.h @@ -4,75 +4,58 @@ #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() = 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; - + Ghost(std::string const &color, Point const &start_pos, Pacman& pac); + virtual void set_pos( Point new_pos); + virtual Point get_scatter() = 0; + virtual Point get_chase() = 0; + virtual Point get_pos(); + virtual std::string get_color()const = 0; + virtual ~Ghost() = default; +protected: + std::string color_index; + Point pos; + Pacman& pacman; }; - class Clyde: public Ghost - { - 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; - - - +public: + Clyde(std::string const &color, Point const &start_pos, Pacman& pac); + void set_pos(Point new_pos) override; + Point get_scatter() override ; + Point get_chase() override ; + Point get_pos() override; + std::string get_color()const; }; - - class Pinky: public Ghost - { - 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; - - - +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() override ; + 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, 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; - +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() override; + Point get_chase() override; // Blinky::get_chase(const Point&)’ marked ‘override’, but does not override + Point get_pos()override; + std::string get_color()const; private: -bool angry = false ; - - + bool angry = false ; }; + #endif \ No newline at end of file diff --git a/Uppgift-3-Spel/given.h b/Uppgift-3-Spel/given.h index 2999a6dd7d55e74636feba01df9d41880eefad38..92b57b82966db209d7c3b0d63a7344dfe33a4fd1 100644 --- a/Uppgift-3-Spel/given.h +++ b/Uppgift-3-Spel/given.h @@ -1,11 +1,9 @@ -// TODO: skydd f�r dubbel inkludering - /* - I denna fil finns ett utval av kod fr�n det dina kollegor skrivit till ert pacman-projekt. Det �r - precis det som du beh�ver f�r att skriva din del av koden. + I denna fil finns ett utval av kod fr�n det dina kollegor skrivit till ert pacman-projekt. Det �r + precis det som du beh�ver f�r att skriva din del av koden. - Tips: Du f�r ut�ka denna fil med saker som du beh�ver f�r att ditt program ska kompilera. Det �r - �ven okej att ut�ka Point med fler operatorer om det skulle beh�vas. + Tips: Du f�r ut�ka denna fil med saker som du beh�ver f�r att ditt program ska kompilera. Det �r + �ven okej att ut�ka Point med fler operatorer om det skulle beh�vas. */ @@ -14,19 +12,19 @@ /* - Globala variabler f�r storlek p� spelplanen. + Globala variabler f�r storlek p� spelplanen. - Din kollega som skrev detta �r medveten om att globala variabler �r d�ligt och borde undvikas, men - har inte haft tid att �tg�rda det. I den slutgiltiga versionen av koden borde dessa s�klart l�sas - fr�n en fil med konfigurationsdata. + Din kollega som skrev detta �r medveten om att globala variabler �r d�ligt och borde undvikas, men + har inte haft tid att �tg�rda det. I den slutgiltiga versionen av koden borde dessa s�klart l�sas + fr�n en fil med konfigurationsdata. */ extern int WIDTH; extern int HEIGHT; /* - Ett aggregat som anv�nd f�r att representera punkter och riktninar p� spelplanen. - Aggregatet har ocks� hj�lpsamma operatorer som kan underl�tta din implementation. + Ett aggregat som anv�nd f�r att representera punkter och riktninar p� spelplanen. + Aggregatet har ocks� hj�lpsamma operatorer som kan underl�tta din implementation. */ struct Point @@ -39,8 +37,8 @@ std::istream& operator>>(std::istream& is, Point& rhs); /* - En klass f�r att representera spelarfiguren. Detta �r en nedskalad version j�mf�rt med det som - kollegan skrev till spelet. Notera att din kod inte beh�ver �ndras om man skulle g� fr�n denna + En klass f�r att representera spelarfiguren. Detta �r en nedskalad version j�mf�rt med det som + kollegan skrev till spelet. Notera att din kod inte beh�ver �ndras om man skulle g� fr�n denna implementaion till den faktiska implementationen. */ class Pacman diff --git a/Uppgift-3-Spel/main.cc b/Uppgift-3-Spel/main.cc index 86fbd0604f598493c9324fd094621d5c722aa441..b113c93550246b253659d4fbf5a0bf871533a0e7 100644 --- a/Uppgift-3-Spel/main.cc +++ b/Uppgift-3-Spel/main.cc @@ -32,9 +32,9 @@ public: 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})) ; + all_ghosts.push_back(new Clyde("orange", Point{5,5}, pacman)); + all_ghosts.push_back(new Blinky("red", Point{6,6}, pacman)); + all_ghosts.push_back(new Pinky("pink", Point{7,7}, pacman)) ; } void run() @@ -108,17 +108,34 @@ private: 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) - { - string to_draw{" "}; - if (pacman.get_position() == curr_pos) + Point pacmanPosition = pacman.get_position(); + Point pacmanDirection = pacman.get_direction(); + + string to_draw(Point const& curr_pos) { - to_draw[1] = '@'; - } + string to_draw{" "}; - return to_draw; - } + if (pacman.get_position() == curr_pos) + { + to_draw[1] = '@'; + } + for (Ghost* temp : all_ghosts) + { + char r {temp -> get_color().at(0)}; + + if(temp -> get_chase() == curr_pos) + { + to_draw[1] = r; + } + if(temp -> get_pos() == curr_pos) + { + to_draw[1] = toupper(r); + } + } + + return to_draw; + } /* En hj�lpfunktion f�r att rita ut spelplanen f�r testprogrammet.