diff --git a/bin/main b/bin/main deleted file mode 100755 index 54f2636..0000000 Binary files a/bin/main and /dev/null differ diff --git a/include/Controller.h b/include/Controller.h index 723254c..fbc3463 100644 --- a/include/Controller.h +++ b/include/Controller.h @@ -3,6 +3,8 @@ #include "Graphics.h" #include "Snake.h" +#define DEFEAT -1 + class Controller{ private: Snake *snake; //the figure the controler controls @@ -14,7 +16,7 @@ public: graphics_input readInput(void); //basic input methods: sets inpt to what it read if i tread something - void act(void); //basic act method: acts like a joystick + int act(void); //basic act method: acts like a joystick bool wantsToQuit(void); //returns true of the user wants to quit }; \ No newline at end of file diff --git a/include/Graphics.h b/include/Graphics.h index 7379cfa..1f36bc9 100644 --- a/include/Graphics.h +++ b/include/Graphics.h @@ -10,6 +10,10 @@ typedef int graphics_input; //the type of the graphics input +#define GAME_TOP_WALL_Y 1 +#define GAME_BOTTOM_WALL_Y LINES+1 +#define GAME_LEFT_WALL_X -1 +#define GAME_RIGHT_WALL_X COLS+1 void initializeGraphics(void); void endGraphics(void); diff --git a/include/Snack.h b/include/Snack.h new file mode 100644 index 0000000..c97382b --- /dev/null +++ b/include/Snack.h @@ -0,0 +1,6 @@ +#pragma once + +#include "Point.h" + +#define SNACK '*' + diff --git a/include/Snake.h b/include/Snake.h index ccd2dd2..e867da1 100755 --- a/include/Snake.h +++ b/include/Snake.h @@ -3,7 +3,8 @@ #include #include "Point.h" //the library we need to implement the snake -#define SNACK '*' +#include "Snack.h" + #define BODY 'o' class Snake{ private: @@ -24,6 +25,8 @@ public: bool isBitten(void); //function to check if the snake bit its self bool hasBitSnack(int snackY, int snackX);//checks if the snake has bitten a snack + bool hasCrashedWall(void); // method to check if the snake crashed the walls + int getSize(void); //get the current length void incSize(void); //function to increase the length diff --git a/src/Controller.cpp b/src/Controller.cpp index 7bfa8eb..b66ce02 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -1,6 +1,6 @@ #include "Controller.h" #include -#include + using namespace std; Controller::Controller() @@ -17,7 +17,7 @@ Controller::Controller(Snake *_snake) Controller::~Controller(){} -void Controller::act(void){ +int Controller::act(void){ switch (this->inpt) { case UP: @@ -37,6 +37,12 @@ void Controller::act(void){ break; } refreshScreen(); + + if(snake->isBitten()) + return DEFEAT; + if (snake->hasCrashedWall()) + return DEFEAT; + return 0; } graphics_input Controller::readInput(void){ diff --git a/src/Graphics.cpp b/src/Graphics.cpp index 4e271af..c04327f 100644 --- a/src/Graphics.cpp +++ b/src/Graphics.cpp @@ -61,9 +61,9 @@ void printMsg(int y, int x, char* str){ } char readChar(int y, int x){ - mvwgetch(_box, y, x); refresh(); wrefresh(_box); + return mvwgetch(_box, y, x); } int readInpt(){ diff --git a/src/SnakeImpl.cpp b/src/SnakeImpl.cpp index 2e2fc68..5385729 100755 --- a/src/SnakeImpl.cpp +++ b/src/SnakeImpl.cpp @@ -6,7 +6,7 @@ Snake::Snake(int headY, int headX) :direction{LEFT} { snake.push_back(Point{headY, headX, '>'}); //add the head of the snake - for(int i=1; i<=3; i++) + for(int i=1; i<=7; i++) snake.push_back(Point{headY, headX+i, BODY}); this->printSnake(); } @@ -21,6 +21,7 @@ bool Snake::isBitten(void){ while(body_part != snake.end()){ if(body_part->getX() == head.getX() && body_part->getY() == head.getY()) return true; + body_part++; } return false; @@ -30,6 +31,15 @@ bool Snake::hasBitSnack(int snackY, int snackX){ return readChar(snackY, snackX) == SNACK; } +bool Snake::hasCrashedWall(void){ + Point &head = *snake.begin(); + + return (head.getY() < GAME_TOP_WALL_Y) || + (head.getY() > GAME_BOTTOM_WALL_Y) || + (head.getX() < GAME_LEFT_WALL_X) || + (head.getX() > GAME_RIGHT_WALL_X); +} + int Snake::getSize(void){ return snake.size(); } diff --git a/src/a.out b/src/a.out deleted file mode 100755 index f23747f..0000000 Binary files a/src/a.out and /dev/null differ diff --git a/src/main.cpp b/src/main.cpp index 15a3386..86845ea 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,11 +8,10 @@ int main() { initializeGraphics(); Controller c; /* CODE TO BE WRITTEN... */ - graphics_input in; while(c.wantsToQuit() == false){ c.readInput(); - c.act(); - + if(c.act() == DEFEAT) + break; } endGraphics(); return 0;