diff --git a/include/Controller.h b/include/Controller.h index b17a367..c102abc 100755 --- a/include/Controller.h +++ b/include/Controller.h @@ -10,11 +10,14 @@ private: Snake *snake; //the figure the controler controls Point snack; graphics_input inpt; + unsigned int currentScore; public: Controller(Snake* _snake); Controller(); ~Controller(); //default destructor + unsigned int getCurrScore(void); + void resetScore(void); graphics_input readInput(void); //basic input methods: sets inpt to what it read if i tread something int act(void); //basic act method: acts like a joystick diff --git a/include/Game.h b/include/Game.h index e69de29..08dc6e4 100755 --- a/include/Game.h +++ b/include/Game.h @@ -0,0 +1,30 @@ +#pragma once + +//This class will be the abstract Game Interface +#include +#include "Controller.h" +#include "Snake.h" +#include "Player.h" +class SnakeGame{ +private: + std::string gameName; + std::list players; + unsigned int highScore; + std::string bestPlayer; + + void play(std::string playerName); + void addPlayer(std::string playerName); + +public: + SnakeGame(); //intialize graphics and set the game screen + ~SnakeGame(); + + + unsigned int getHighScore(void); + std::string getBestPlayer(void); + + //print the statistics (highest score & games played) of each player + //void printGameStatistics(void); + + void play(void); +}; \ No newline at end of file diff --git a/include/Graphics.h b/include/Graphics.h index 6cb27f9..4833d46 100755 --- a/include/Graphics.h +++ b/include/Graphics.h @@ -15,7 +15,7 @@ typedef int graphics_input; //the type of the graphics input #define GAME_LEFT_WALL_X 1 #define GAME_RIGHT_WALL_X (COLS - 2) -void initializeGraphics(void); +void initializeGraphics(char* gameName); void endGraphics(void); void refreshScreen(void); void printChar(int y, int x, graphics_input img); diff --git a/include/Player.h b/include/Player.h index e69de29..e03e3b2 100755 --- a/include/Player.h +++ b/include/Player.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include "Controller.h" + +class Player{ +private: + std::string name; + unsigned int points; + unsigned int timesPlayed; +public: + Player(); + Player(std::string _name); + ~Player(); + + std::pair play(void); + + //Point managment methods + unsigned int getPoints(void); + void addPoints(unsigned int _points); + + std::string getName(void); +}; \ No newline at end of file diff --git a/src/Controller.cpp b/src/Controller.cpp index 746d120..733c1c2 100755 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -1,31 +1,49 @@ #include "Controller.h" #include - +#include using namespace std; Controller::Controller() -: snack{SNACK} ,inpt{0} +: snack{SNACK} ,inpt{0}, currentScore{0} { snake = new Snake(); generateSnack(snack); } Controller::Controller(Snake *_snake) -: snake{_snake}, snack{SNACK}, inpt{0} +: snake{_snake}, snack{SNACK}, inpt{0}, currentScore{0} { assert(this->snake != NULL); } -Controller::~Controller(){} +unsigned int Controller::getCurrScore(void){ + return this->currentScore; +} + +void Controller::resetScore(void){ + this->currentScore = 0; +} + +Controller::~Controller(){delete snake;} + +static void printScore(unsigned int _score){ + string str = "Score: " + to_string(_score); + printMsg(0, 0, (char*)str.c_str()); //-1, -1 because otherwise it'll be printed inside the game box +} int Controller::act(void){ if (snake->hasBitSnack(this->snack.getY(), this->snack.getX()) == true) { + this->currentScore += 10; + advanceDifficulty(); generateSnack(this->snack); + this->snake->incSize(); + + printScore(currentScore); } switch (this->inpt) diff --git a/src/Game.cpp b/src/Game.cpp new file mode 100644 index 0000000..5c01093 --- /dev/null +++ b/src/Game.cpp @@ -0,0 +1,75 @@ +#include "Game.h" +#include +using namespace std; + +SnakeGame::SnakeGame() +:gameName{"~Snake Game by VissaM~"},highScore{0}, bestPlayer{"None"} +{} + +SnakeGame::~SnakeGame(){} + +void SnakeGame::addPlayer(string playerName){ + players.emplace_back( Player{playerName} ); +} + +unsigned int SnakeGame::getHighScore(void){ + return highScore; +} + +string SnakeGame::getBestPlayer(void){ + return bestPlayer; +} + +void SnakeGame::play(string playerName){ + pair curBest{playerName, 0}; + for(auto player : players){ + if(player.getName() == playerName){ + initializeGraphics((char *)gameName.c_str()); + curBest = player.play(); + endGraphics(); + break; + } + } + + if(curBest.second > highScore){ + highScore = curBest.second; + bestPlayer = curBest.first; + } + + cout << "Highscore: " << highScore << " by " << bestPlayer << "\n" <> playerName; + cout << endl; + + list::iterator p; + for(p=players.begin(); p!=players.end(); p++){ + if(p->getName() == playerName) + break; + } + + if(p == players.end()){ + //if the player isn't in the list, add him/her + addPlayer(playerName); + } + + + play(playerName); //get the player to play the game + + + cout << "Do you want to play again? (yes or no): "; + string ans; + + cin >> ans; + + if(ans != "yes"){ + cout << "Exiting ..." << endl; + break; + } + cout << "Perfect...\n" << endl; + } +} \ No newline at end of file diff --git a/src/Graphics.cpp b/src/Graphics.cpp index 7df1b98..cab15a0 100755 --- a/src/Graphics.cpp +++ b/src/Graphics.cpp @@ -19,7 +19,7 @@ static void destroyBox(void){ delwin(_box); } //Initialization function -void initializeGraphics(void){ +void initializeGraphics(char* gameName){ srand((unsigned int) time(NULL)); initscr(); //initialize curses @@ -27,11 +27,11 @@ void initializeGraphics(void){ noecho(); //set input echo to false keypad(stdscr, TRUE); //this step enables the use of arrow keys and other function keys nodelay(stdscr, true); - curs_set(0); //to hide teh cursor + curs_set(0); //to hide the cursor //We must clear the screen from unecessary garbage clear(); //Print the title - mvprintw(0, (COLS/2) - 12, "~Snake Game by VissaM~"); + mvprintw(0, (COLS/2) - 12, gameName); refresh(); //create the game box diff --git a/src/Player.cpp b/src/Player.cpp new file mode 100644 index 0000000..5d09297 --- /dev/null +++ b/src/Player.cpp @@ -0,0 +1,43 @@ +#include "Player.h" + +using namespace std; + +Player::Player() +: name{"Unknown"}, points{0}, timesPlayed{0} +{} + +Player::Player(string _name) +: name{_name}, points{0}, timesPlayed{0} +{} + +Player::~Player() +{/* No need to do something */} + +string Player::getName(void){ + return this->name; +} + +void Player::addPoints(unsigned int _points){ + points += _points; +} + +unsigned int Player::getPoints(void){ + return this->points; +} + +std::pair Player::play(void){ + //here we will implement the basic loop of the game since each player has its remote controller + //and can play the game + Controller controller; + while (controller.wantsToQuit() == false) + { + controller.readInput(); + if (controller.act() == DEFEAT) + break; + } + + points = controller.getCurrScore(); + controller.resetScore(); + + return {this->name, points}; +} \ No newline at end of file diff --git a/src/PointClassImpl.cpp b/src/PointClassImpl.cpp index 25d9b92..6993299 100755 --- a/src/PointClassImpl.cpp +++ b/src/PointClassImpl.cpp @@ -11,13 +11,9 @@ Point::Point(int y, int x, graphics_input img) //delegating the previous constructor Point::Point() :Point(10, 10, '*') -{ - -} +{} -Point::~Point(){ - /*No need to do something */ -} +Point::~Point(){/*No need to do something */} void Point::setPoint(int y, int x){ this->x = x; diff --git a/src/SnakeImpl.cpp b/src/SnakeImpl.cpp index d1782ac..a54bdb3 100755 --- a/src/SnakeImpl.cpp +++ b/src/SnakeImpl.cpp @@ -8,7 +8,6 @@ Snake::Snake(int headY, int headX) snake.push_back(Point{headY, headX, '>'}); //add the head of the snake for(int i=1; i<=3; i++) snake.push_back(Point{headY, headX+i, BODY}); - this->printSnake(); } Snake::~Snake(){} diff --git a/src/main.cpp b/src/main.cpp index 86845ea..3f852d2 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,19 +1,12 @@ #include #include -#include "Controller.h" +#include "Game.h" using namespace std; int main() { - initializeGraphics(); - Controller c; - /* CODE TO BE WRITTEN... */ - while(c.wantsToQuit() == false){ - c.readInput(); - if(c.act() == DEFEAT) - break; - } - endGraphics(); + SnakeGame game; + game.play(); return 0; } \ No newline at end of file