Browse Source

added a 'Player' class and a 'Game' class to achieve higher encapsulation and data abstraction

master
VissaMoutafis 5 years ago
parent
commit
baf56b9564
  1. 3
      include/Controller.h
  2. 30
      include/Game.h
  3. 2
      include/Graphics.h
  4. 24
      include/Player.h
  5. 26
      src/Controller.cpp
  6. 75
      src/Game.cpp
  7. 6
      src/Graphics.cpp
  8. 43
      src/Player.cpp
  9. 8
      src/PointClassImpl.cpp
  10. 1
      src/SnakeImpl.cpp
  11. 13
      src/main.cpp

3
include/Controller.h

@ -10,11 +10,14 @@ private:
Snake *snake; //the figure the controler controls Snake *snake; //the figure the controler controls
Point snack; Point snack;
graphics_input inpt; graphics_input inpt;
unsigned int currentScore;
public: public:
Controller(Snake* _snake); Controller(Snake* _snake);
Controller(); Controller();
~Controller(); //default destructor ~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 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 int act(void); //basic act method: acts like a joystick

30
include/Game.h

@ -0,0 +1,30 @@
#pragma once
//This class will be the abstract Game Interface
#include <string>
#include "Controller.h"
#include "Snake.h"
#include "Player.h"
class SnakeGame{
private:
std::string gameName;
std::list<Player> 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);
};

2
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_LEFT_WALL_X 1
#define GAME_RIGHT_WALL_X (COLS - 2) #define GAME_RIGHT_WALL_X (COLS - 2)
void initializeGraphics(void);
void initializeGraphics(char* gameName);
void endGraphics(void); void endGraphics(void);
void refreshScreen(void); void refreshScreen(void);
void printChar(int y, int x, graphics_input img); void printChar(int y, int x, graphics_input img);

24
include/Player.h

@ -0,0 +1,24 @@
#pragma once
#include <iostream>
#include <string>
#include "Controller.h"
class Player{
private:
std::string name;
unsigned int points;
unsigned int timesPlayed;
public:
Player();
Player(std::string _name);
~Player();
std::pair<std::string, unsigned int> play(void);
//Point managment methods
unsigned int getPoints(void);
void addPoints(unsigned int _points);
std::string getName(void);
};

26
src/Controller.cpp

@ -1,31 +1,49 @@
#include "Controller.h" #include "Controller.h"
#include <assert.h> #include <assert.h>
#include <string>
using namespace std; using namespace std;
Controller::Controller() Controller::Controller()
: snack{SNACK} ,inpt{0}
: snack{SNACK} ,inpt{0}, currentScore{0}
{ {
snake = new Snake(); snake = new Snake();
generateSnack(snack); generateSnack(snack);
} }
Controller::Controller(Snake *_snake) Controller::Controller(Snake *_snake)
: snake{_snake}, snack{SNACK}, inpt{0}
: snake{_snake}, snack{SNACK}, inpt{0}, currentScore{0}
{ {
assert(this->snake != NULL); 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){ int Controller::act(void){
if (snake->hasBitSnack(this->snack.getY(), this->snack.getX()) == true) if (snake->hasBitSnack(this->snack.getY(), this->snack.getX()) == true)
{ {
this->currentScore += 10;
advanceDifficulty(); advanceDifficulty();
generateSnack(this->snack); generateSnack(this->snack);
this->snake->incSize(); this->snake->incSize();
printScore(currentScore);
} }
switch (this->inpt) switch (this->inpt)

75
src/Game.cpp

@ -0,0 +1,75 @@
#include "Game.h"
#include <algorithm>
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<string, unsigned int> 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" <<endl;
}
void SnakeGame::play(void){
while(1){
string playerName;
cout << "Who's playing: ";
cin >> playerName;
cout << endl;
list<Player>::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;
}
}

6
src/Graphics.cpp

@ -19,7 +19,7 @@ static void destroyBox(void){
delwin(_box); delwin(_box);
} }
//Initialization function //Initialization function
void initializeGraphics(void){
void initializeGraphics(char* gameName){
srand((unsigned int) time(NULL)); srand((unsigned int) time(NULL));
initscr(); //initialize curses initscr(); //initialize curses
@ -27,11 +27,11 @@ void initializeGraphics(void){
noecho(); //set input echo to false noecho(); //set input echo to false
keypad(stdscr, TRUE); //this step enables the use of arrow keys and other function keys keypad(stdscr, TRUE); //this step enables the use of arrow keys and other function keys
nodelay(stdscr, true); 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 //We must clear the screen from unecessary garbage
clear(); clear();
//Print the title //Print the title
mvprintw(0, (COLS/2) - 12, "~Snake Game by VissaM~");
mvprintw(0, (COLS/2) - 12, gameName);
refresh(); refresh();
//create the game box //create the game box

43
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<std::string, unsigned int> 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};
}

8
src/PointClassImpl.cpp

@ -11,13 +11,9 @@ Point::Point(int y, int x, graphics_input img)
//delegating the previous constructor //delegating the previous constructor
Point::Point() Point::Point()
:Point(10, 10, '*') :Point(10, 10, '*')
{
}
{}
Point::~Point(){
/*No need to do something */
}
Point::~Point(){/*No need to do something */}
void Point::setPoint(int y, int x){ void Point::setPoint(int y, int x){
this->x = x; this->x = x;

1
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 snake.push_back(Point{headY, headX, '>'}); //add the head of the snake
for(int i=1; i<=3; i++) for(int i=1; i<=3; i++)
snake.push_back(Point{headY, headX+i, BODY}); snake.push_back(Point{headY, headX+i, BODY});
this->printSnake();
} }
Snake::~Snake(){} Snake::~Snake(){}

13
src/main.cpp

@ -1,19 +1,12 @@
#include <ncurses.h> #include <ncurses.h>
#include <iostream> #include <iostream>
#include "Controller.h"
#include "Game.h"
using namespace std; using namespace std;
int main() { 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; return 0;
} }
Loading…
Cancel
Save