Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.51 KB | None | 0 0
  1. PLAYERS.CPP
  2.  
  3. #include "Players.h"
  4. #include <string>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. Players::Players() {
  11.     int pos = 0;
  12.     char symbol;
  13.     int numPlayers;
  14.  
  15. }
  16.  
  17. void    Players::setSymbol(char s) { symbol = s; }
  18.  
  19. char    Players::getSymbol() { return symbol; }
  20.  
  21. void    Players::setPos(int p) { pos = p; }
  22.  
  23. int     Players::getPos() { return pos; }
  24.  
  25. GAMES.CPP
  26.  
  27. #include "Game.h"
  28. #include "Players.h"
  29. #include <string>
  30. #include <iostream>
  31. #include <ctime>
  32. #include <iomanip>
  33. #include <conio.h>
  34.  
  35. using namespace std;
  36.  
  37. Game::Game() {
  38.     int turn = 1;
  39.     static const int NUMROWS = 15;
  40.     static const int NUMCOLS = 10;
  41.     static const int WINPOS = 142;
  42. }
  43.  
  44. void    Game::printGameHeader() {
  45.  
  46.     cout << "+" << setfill('-') << setw(36) << "+" << endl;
  47.     cout << "|" << setfill(' ') << setw(36) << "|" << endl;
  48.     cout << "|" << setw(23) << "Welcome to" << setw(13) << "|" << endl;
  49.     cout << "|" << setw(36) << "|" << endl;
  50.     cout << "|" << setw(30) << "Trap Doors & Trampolines" << setw(6) << "|" << endl;
  51.     cout << "+" << setfill('-') << setw(36) << "+" << endl;
  52.     cout << "|" << setfill(' ') << setw(36) << "|" << endl;
  53.     cout << "|" << setw(13) << "Trap Doors" << setw(20) << "Trampolines" << setw(3) << "|" << endl;
  54.     cout << "|" << setw(13) << "----------" << setw(21) << "-------------" << setw(2) << "|" << endl;
  55.     cout << "|" << setw(8) << "9" << setw(4) << "-> " << setw(3) << "2" << setw(9) << "3" << setw(7) << "-> " << " 59" << setw(2) << "|" << endl;
  56.     cout << "|" << setw(8) << "22" << setw(4) << "-> " << setw(3) << "8" << setw(9) << "16" << setw(7) << "-> " << " 32" << setw(2) << "|" << endl;
  57.     cout << "|" << setw(8) << "30" << setw(6) << " -> " " 14" << setw(9) << "33" << setw(7) << "-> " << " 54" << setw(2) << "|" << endl;
  58.     cout << "|" << setw(8) << "51" << setw(6) << " -> " " 12" << setw(9) << "35" << setw(7) << "-> " << " 41" << setw(2) << "|" << endl;
  59.     cout << "|" << setw(8) << "60" << setw(6) << " -> " " 42" << setw(9) << "56" << setw(7) << "-> " << " 78" << setw(2) << "|" << endl;
  60.     cout << "|" << setw(8) << "88" << setw(6) << " -> " " 61" << setw(9) << "66" << setw(7) << "-> " << " 92" << setw(2) << "|" << endl;
  61.     cout << "|" << setw(8) << "116" << setw(6) << " -> " " 86" << setw(9) << "96" << setw(7) << "-> " << "105" << setw(2) << "|" << endl;
  62.     cout << "|" << setw(8) << "128" << setw(7) << "-> " "108" << setw(9) << "109" << setw(7) << "-> " << "140" << setw(2) << "|" << endl;
  63.     cout << "|" << setw(8) << "135" << setw(7) << "-> " "100" << setw(9) << "124" << setw(7) << "-> " << "134" << setw(2) << "|" << endl;
  64.     cout << "+" << setfill('-') << setw(36) << "+" << endl;
  65.     cout << setfill(' ') << endl;
  66.  
  67. }
  68.  
  69. bool    Game::checkTraps(int& pos) {
  70.  
  71.     bool trapHit = false;
  72.  
  73.     if (pos == 9)
  74.     {
  75.         pos = 2;
  76.         return true;
  77.     }
  78.     else if (pos == 22)
  79.     {
  80.         pos = 8;
  81.         trapHit = true;
  82.     }
  83.     else if (pos == 30)
  84.     {
  85.         pos = 14;
  86.         trapHit = true;
  87.     }
  88.     else if (pos == 51)
  89.     {
  90.         pos = 12;
  91.         trapHit = true;
  92.     }
  93.     else if (pos == 60)
  94.     {
  95.         pos = 42;
  96.         trapHit = true;
  97.     }
  98.     else if (pos == 88)
  99.     {
  100.         pos = 61;
  101.         trapHit = true;
  102.     }
  103.     else if (pos == 116)
  104.     {
  105.         pos = 86;
  106.         trapHit = true;
  107.     }
  108.     else if (pos == 135)
  109.     {
  110.         pos = 100;
  111.         trapHit = true;
  112.     }
  113.     return trapHit;
  114. }
  115.  
  116. bool    Game::checkTrampoline(int& pos) {
  117.    
  118.     bool trampolineHit = false;
  119.     if (pos == 3)
  120.     {
  121.         pos = 59;
  122.         trampolineHit = true;
  123.     }
  124.     else if (pos == 16)
  125.     {
  126.         pos = 32;
  127.         trampolineHit = true;
  128.     }
  129.     else if (pos == 33)
  130.     {
  131.         pos = 54;
  132.         trampolineHit = true;
  133.     }
  134.     else if (pos == 35)
  135.     {
  136.         pos = 41;
  137.         trampolineHit = true;
  138.     }
  139.     else if (pos == 56)
  140.     {
  141.         pos = 78;
  142.         trampolineHit = true;
  143.     }
  144.     else if (pos == 66)
  145.     {
  146.         pos = 92;
  147.         trampolineHit = true;
  148.     }
  149.     else if (pos == 96)
  150.     {
  151.         pos = 105;
  152.         trampolineHit = true;
  153.     }
  154.     else if (pos == 109)
  155.     {
  156.         pos = 140;
  157.         trampolineHit = true;
  158.     }
  159.     else if (pos == 124)
  160.     {
  161.         pos = 134;
  162.         trampolineHit = true;
  163.     }
  164.     return trampolineHit;
  165. }
  166.  
  167. void    Game::printGameBoard(Players players[], int numPlayers)
  168. {
  169.     for (int i = 0; i < NUMROWS; i++)
  170.     {
  171.         cout << "    " << endl;
  172.  
  173.         cout << right << i << ":  ";
  174.  
  175.  
  176.             for (int j = 0; j < NUMCOLS + 1; j++)
  177.             {
  178.                 cout << "|";
  179.  
  180.                 for (int p = 0; p < numPlayers; p++)
  181.                 {
  182.                     int tmpPos = j + (i * NUMCOLS);
  183.  
  184.                     if (checkTrampoline(tmpPos) == true)
  185.                     {
  186.                         cout << "+";
  187.                     }
  188.                     else if (checkTraps(tmpPos) == true)
  189.                     {
  190.                         cout << "-";
  191.                     }
  192.                     else if (tmpPos == WINPOS)
  193.                     {
  194.                         cout << "!";
  195.                     }
  196.                     else if (players[p].getPos() == tmpPos)
  197.                     {
  198.                         cout << players[p].getSymbol();
  199.                     }
  200.                     else
  201.                     {
  202.                         cout << " ";
  203.                     }
  204.                 }
  205.             }
  206.             cout << endl;
  207.            
  208.             cout << right << "    ";
  209.  
  210.         for (int j = 0; j < NUMCOLS; j++)
  211.         {
  212.             for (int p = 0; p < numPlayers + 1; p++)
  213.             {
  214.                 cout << "-";
  215.             }
  216.         }
  217.  
  218.     }
  219.    
  220.  
  221.  
  222.     cout << "    ";
  223.  
  224.  
  225.  
  226.     cout << "\n";
  227.     cout << "    ";
  228.     for (int j = 0; j < NUMCOLS; j++)
  229.     {
  230.         cout << right << "  " << j;
  231.     }
  232.     cout << "\n";
  233. }
  234.  
  235. int     Game::rollDice()
  236. {
  237.     srand((unsigned)time(0));
  238.     int HIGH = 6;
  239.     int LOW = 1;
  240.     int dieOne = rand() % (HIGH - LOW + 1) + LOW;
  241.     int dieTwo = rand() % (HIGH - LOW + 1) + LOW;
  242.  
  243.     int roll = dieOne + dieTwo;
  244.  
  245.  
  246.     return roll;
  247. }
  248.  
  249. void    Game::movePlayer(int& pos, int roll, string skip)
  250. {
  251.      pos = pos + rollDice();
  252.  
  253.      if (checkTrampoline(pos) == true)
  254.      {
  255.          string skip = "Trampoline";
  256.      }
  257.      else if (checkTraps(pos) == true)
  258.      {
  259.          string skip = "Trap Door";
  260.      }
  261.      else
  262.      {
  263.          string skip = " ";
  264.      }
  265.    
  266.    
  267.  
  268. }
  269.  
  270. bool    Game::noWinner(int pos)
  271. {
  272.  
  273.     if (pos >= 142)
  274.     {
  275.         return false;
  276.     }
  277.     else
  278.     {
  279.         return true;
  280.     }
  281. }
  282.  
  283. void    Game::printWinner(Players &player, int pos)
  284. {
  285.  
  286.     if (Game::noWinner(pos) == false)
  287.     {
  288.         cout << "The game is over and the winner is: " << player.getSymbol() << endl;
  289.     }
  290. }
  291.  
  292. void    Game::nextTurn()
  293. {
  294.         turn++;
  295.    
  296. }
  297.  
  298. int     Game::getTurn() { return turn; }
  299.  
  300. PLAYERS.H
  301.  
  302. #pragma once
  303. #include <string>
  304.  
  305. using namespace std;
  306.  
  307. class Players {
  308. private:
  309.     int pos = 0; // position of the player on the board
  310.     char symbol; // represents each player on the game board
  311.     int numPlayers = 0;
  312.  
  313.  
  314. public:
  315.     Players();
  316.  
  317.     void setSymbol(char s); // sets the symbol of the players
  318.  
  319.     char getSymbol(); // retrieves the players symbol
  320.  
  321.     void setPos(int p); // sets the position of the player
  322.  
  323.     int getPos(); // gets the position of the player
  324.  
  325.  
  326. };
  327.  
  328. MAIN.CPP
  329.  
  330. #include <iostream>
  331. #include <iomanip>
  332. #include <cstdlib>
  333. #include <ctime>
  334. #include <string>
  335. #include <conio.h>
  336. #include "Game.h"
  337. #include "Players.h"
  338. using namespace std;
  339.  
  340. void getPlayers(Players players[], int& numPlayers, const int MAXPLAYERS)
  341. {
  342.     int numplayers = 0;
  343.     char symbol;
  344.     while (numPlayers > MAXPLAYERS || numPlayers < 2) {
  345.  
  346.  
  347.         cout << "\n\nHow many players will play (2 - " << MAXPLAYERS << ")? ";
  348.         cin >> numPlayers;
  349.         cout << endl;
  350.     }
  351.     for (int i = 0; i < numPlayers; i++) {
  352.         cout << "Enter Player " << i + 1 << "'s Symbol: ";
  353.         cin >> symbol;
  354.         players[i].setSymbol(symbol);
  355.     }
  356.  
  357. }
  358.  
  359. int main() {
  360.     const int MAX_PLAYERS = 6;          //Max # players allowed
  361.     Game  G;
  362.     Players      players[MAX_PLAYERS];   //Array of Player objects
  363.     int         dice;                    //Value of dice roll
  364.     int         pos;                     //Position variable for a player
  365.     int         t = 0;                   //Index to keep track of whose turn it is
  366.     char        symbol;
  367.  
  368.     int         numPlayers = 0;              //Number of players playing the game
  369.     string      skip;                    //Denotes name of special space if landed on
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.         //Game simulation object
  378.     srand(time(NULL));
  379.     G.printGameHeader();
  380.     getPlayers(players, numPlayers, MAX_PLAYERS);
  381.     G.printGameBoard(players, numPlayers);
  382.    
  383.  
  384.  
  385.     //note: one loop iteration = 1 turn. Turn is decided upon by which
  386.     //overall turn number it is
  387.     while (G.noWinner(players[t].getPos())) {
  388.         //set up for the next player's turn (player will be index t in array)
  389.         G.nextTurn();
  390.         t = (G.getTurn() - 1) % numPlayers;
  391.         //handle turn for the current player (again, current player is at index t)
  392.         pos = players[t].getPos();
  393.         cout << "\n\n" << players[t].getSymbol() << "'s turn (space " << pos <<
  394.             ")."
  395.             << " Press any Key to Continue..." << endl;
  396.         //wait for user input to proceed
  397.         _getch();
  398.         skip = "";
  399.         dice = G.rollDice();
  400.         G.movePlayer(pos, dice, skip);
  401.         players[t].setPos(pos);
  402.         cout << dice << " was rolled.";
  403.         if (skip != "") {
  404.             cout << " You landed on a " << skip;
  405.         }
  406.         cout << "\nMoving player " << players[t].getSymbol()
  407.             << " to " << players[t].getPos() << endl;
  408.         G.printGameBoard(players, numPlayers);
  409.  
  410.     }
  411.     //print who won and end the program
  412.    
  413.     G.printWinner(players[t], players[t].getPos());
  414.     cout << endl << endl;
  415.     return 0;
  416. }
  417. ////////////////////////////////////////////////////////////////////////////////
  418. //Asks the user how many players will be playing. Then, retrieve player symbols
  419. //for each player from the user and store in the player array.
  420. ////////////////////////////////////////////////////////////////////////////////
  421.  
  422. GAME.H
  423.  
  424. #include <string>
  425. #include "Players.h"
  426.  
  427. using namespace std;
  428.  
  429. class Game {
  430. private:
  431.     int turn;
  432.     static const int NUMROWS = 15;
  433.     static const int NUMCOLS = 10;
  434.     static const int WINPOS = 142;
  435.  
  436.  
  437. public:
  438.     Game();
  439.  
  440.     void printGameHeader();
  441.  
  442.     bool checkTraps(int &pos);
  443.  
  444.     bool checkTrampoline(int &pos);
  445.  
  446.     void printGameBoard(Players players[], int numPlayers);
  447.  
  448.     void printWinner(Players &players, int pos);
  449.  
  450.     int rollDice();
  451.    
  452.     void movePlayer(int& pos, int roll, string skip);
  453.  
  454.     bool noWinner(int pos);
  455.  
  456.     void nextTurn();
  457.  
  458.     int getTurn();
  459.  
  460.  
  461. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement