Guest User

Untitled

a guest
Jul 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class Player {
  8. private:
  9. string name;
  10.  
  11. public:
  12. Player(string name0) {
  13. name = name0;
  14. }
  15. string toString() {
  16. return name;
  17. }
  18. };
  19.  
  20. //-------------------------------------------------
  21.  
  22. class Game {
  23. protected:
  24. string name;
  25. vector<Player> players;
  26. public:
  27. virtual void getPlayers() {
  28. cout << "This is a two player game, enter each player's name: " << endl;
  29. string s = "";
  30. for(int i = 0; i < 2; i++) {
  31. cin >> s;
  32. Player p(s);
  33. players.push_back(p);
  34. }
  35. }
  36.  
  37. void putPlayers() {
  38. for(int i = 0; i < players.size(); i++) {
  39. cout << players[i].toString() << endl;
  40. }
  41. }
  42. virtual void play() {
  43. }
  44. };
  45.  
  46. //-------------------------------------------------
  47.  
  48. class ConnectFour : public Game {
  49. public:
  50. void play() {
  51. }
  52. };
  53.  
  54. //-------------------------------------------------
  55.  
  56. class SnakesAndLadders : public Game {
  57. public:
  58. void getPlayers() {
  59. cout << "Enter each player's name, 'x' to finish: " << endl;
  60. string s = "";
  61. cin >> s;
  62. while(s != "x") {
  63. Player p(s);
  64. players.push_back(p);
  65. cin >> s;
  66. }
  67. }
  68.  
  69. void play() {
  70. }
  71. };
  72.  
  73. //--------------------------------------------------
  74.  
  75. class Draughts : public Game {
  76. public:
  77. void play(){
  78. }
  79. };
  80.  
  81. //--------------------------------------------------
  82.  
  83. int main() {
  84.  
  85. int NUM_GAMES = 3;
  86. Game * games[NUM_GAMES];
  87. games[0] = new ConnectFour;
  88. games[1] = new SnakesAndLadders;
  89. games[2] = new Draughts;
  90.  
  91. int choice;
  92.  
  93. cout << "Welcome to the Compendium of Games!" << endl;
  94.  
  95. // ask what game
  96.  
  97. cout << "What number game would you like to play: " << endl;
  98. cout << "Game 1 = Connect Four\nGame 2 = Snakes and Ladders\nGame 3 = Draughts\n" << endl;
  99. cout << "Enter your choice: ";
  100. cin >> choice;
  101. while(choice < 1 || choice > 3) {
  102. cout << "Game does not exist, choose again:" << endl;
  103. cin >> choice;
  104. }
  105.  
  106. choice = choice-1;
  107.  
  108. games[choice]->getPlayers();
  109. games[choice]->play();
  110.  
  111. return 0;
  112. }
Add Comment
Please, Sign In to add comment