Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- class Player {
- private:
- string name;
- public:
- Player(string name0) {
- name = name0;
- }
- string toString() {
- return name;
- }
- };
- //-------------------------------------------------
- class Game {
- protected:
- string name;
- vector<Player> players;
- public:
- virtual void getPlayers() {
- cout << "This is a two player game, enter each player's name: " << endl;
- string s = "";
- for(int i = 0; i < 2; i++) {
- cin >> s;
- Player p(s);
- players.push_back(p);
- }
- }
- void putPlayers() {
- for(int i = 0; i < players.size(); i++) {
- cout << players[i].toString() << endl;
- }
- }
- virtual void play() {
- }
- };
- //-------------------------------------------------
- class ConnectFour : public Game {
- public:
- void play() {
- }
- };
- //-------------------------------------------------
- class SnakesAndLadders : public Game {
- public:
- void getPlayers() {
- cout << "Enter each player's name, 'x' to finish: " << endl;
- string s = "";
- cin >> s;
- while(s != "x") {
- Player p(s);
- players.push_back(p);
- cin >> s;
- }
- }
- void play() {
- }
- };
- //--------------------------------------------------
- class Draughts : public Game {
- public:
- void play(){
- }
- };
- //--------------------------------------------------
- int main() {
- int NUM_GAMES = 3;
- Game * games[NUM_GAMES];
- games[0] = new ConnectFour;
- games[1] = new SnakesAndLadders;
- games[2] = new Draughts;
- int choice;
- cout << "Welcome to the Compendium of Games!" << endl;
- // ask what game
- cout << "What number game would you like to play: " << endl;
- cout << "Game 1 = Connect Four\nGame 2 = Snakes and Ladders\nGame 3 = Draughts\n" << endl;
- cout << "Enter your choice: ";
- cin >> choice;
- while(choice < 1 || choice > 3) {
- cout << "Game does not exist, choose again:" << endl;
- cin >> choice;
- }
- choice = choice-1;
- games[choice]->getPlayers();
- games[choice]->play();
- return 0;
- }
Add Comment
Please, Sign In to add comment