Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PLAYERS.CPP
- #include "Players.h"
- #include <string>
- #include <iostream>
- using namespace std;
- Players::Players() {
- int pos = 0;
- char symbol;
- int numPlayers;
- }
- void Players::setSymbol(char s) { symbol = s; }
- char Players::getSymbol() { return symbol; }
- void Players::setPos(int p) { pos = p; }
- int Players::getPos() { return pos; }
- GAMES.CPP
- #include "Game.h"
- #include "Players.h"
- #include <string>
- #include <iostream>
- #include <ctime>
- #include <iomanip>
- #include <conio.h>
- using namespace std;
- Game::Game() {
- int turn = 1;
- static const int NUMROWS = 15;
- static const int NUMCOLS = 10;
- static const int WINPOS = 142;
- }
- void Game::printGameHeader() {
- cout << "+" << setfill('-') << setw(36) << "+" << endl;
- cout << "|" << setfill(' ') << setw(36) << "|" << endl;
- cout << "|" << setw(23) << "Welcome to" << setw(13) << "|" << endl;
- cout << "|" << setw(36) << "|" << endl;
- cout << "|" << setw(30) << "Trap Doors & Trampolines" << setw(6) << "|" << endl;
- cout << "+" << setfill('-') << setw(36) << "+" << endl;
- cout << "|" << setfill(' ') << setw(36) << "|" << endl;
- cout << "|" << setw(13) << "Trap Doors" << setw(20) << "Trampolines" << setw(3) << "|" << endl;
- cout << "|" << setw(13) << "----------" << setw(21) << "-------------" << setw(2) << "|" << endl;
- cout << "|" << setw(8) << "9" << setw(4) << "-> " << setw(3) << "2" << setw(9) << "3" << setw(7) << "-> " << " 59" << setw(2) << "|" << endl;
- cout << "|" << setw(8) << "22" << setw(4) << "-> " << setw(3) << "8" << setw(9) << "16" << setw(7) << "-> " << " 32" << setw(2) << "|" << endl;
- cout << "|" << setw(8) << "30" << setw(6) << " -> " " 14" << setw(9) << "33" << setw(7) << "-> " << " 54" << setw(2) << "|" << endl;
- cout << "|" << setw(8) << "51" << setw(6) << " -> " " 12" << setw(9) << "35" << setw(7) << "-> " << " 41" << setw(2) << "|" << endl;
- cout << "|" << setw(8) << "60" << setw(6) << " -> " " 42" << setw(9) << "56" << setw(7) << "-> " << " 78" << setw(2) << "|" << endl;
- cout << "|" << setw(8) << "88" << setw(6) << " -> " " 61" << setw(9) << "66" << setw(7) << "-> " << " 92" << setw(2) << "|" << endl;
- cout << "|" << setw(8) << "116" << setw(6) << " -> " " 86" << setw(9) << "96" << setw(7) << "-> " << "105" << setw(2) << "|" << endl;
- cout << "|" << setw(8) << "128" << setw(7) << "-> " "108" << setw(9) << "109" << setw(7) << "-> " << "140" << setw(2) << "|" << endl;
- cout << "|" << setw(8) << "135" << setw(7) << "-> " "100" << setw(9) << "124" << setw(7) << "-> " << "134" << setw(2) << "|" << endl;
- cout << "+" << setfill('-') << setw(36) << "+" << endl;
- cout << setfill(' ') << endl;
- }
- bool Game::checkTraps(int& pos) {
- bool trapHit = false;
- if (pos == 9)
- {
- pos = 2;
- return true;
- }
- else if (pos == 22)
- {
- pos = 8;
- trapHit = true;
- }
- else if (pos == 30)
- {
- pos = 14;
- trapHit = true;
- }
- else if (pos == 51)
- {
- pos = 12;
- trapHit = true;
- }
- else if (pos == 60)
- {
- pos = 42;
- trapHit = true;
- }
- else if (pos == 88)
- {
- pos = 61;
- trapHit = true;
- }
- else if (pos == 116)
- {
- pos = 86;
- trapHit = true;
- }
- else if (pos == 135)
- {
- pos = 100;
- trapHit = true;
- }
- return trapHit;
- }
- bool Game::checkTrampoline(int& pos) {
- bool trampolineHit = false;
- if (pos == 3)
- {
- pos = 59;
- trampolineHit = true;
- }
- else if (pos == 16)
- {
- pos = 32;
- trampolineHit = true;
- }
- else if (pos == 33)
- {
- pos = 54;
- trampolineHit = true;
- }
- else if (pos == 35)
- {
- pos = 41;
- trampolineHit = true;
- }
- else if (pos == 56)
- {
- pos = 78;
- trampolineHit = true;
- }
- else if (pos == 66)
- {
- pos = 92;
- trampolineHit = true;
- }
- else if (pos == 96)
- {
- pos = 105;
- trampolineHit = true;
- }
- else if (pos == 109)
- {
- pos = 140;
- trampolineHit = true;
- }
- else if (pos == 124)
- {
- pos = 134;
- trampolineHit = true;
- }
- return trampolineHit;
- }
- void Game::printGameBoard(Players players[], int numPlayers)
- {
- for (int i = 0; i < NUMROWS; i++)
- {
- cout << " " << endl;
- cout << right << i << ": ";
- for (int j = 0; j < NUMCOLS + 1; j++)
- {
- cout << "|";
- for (int p = 0; p < numPlayers; p++)
- {
- int tmpPos = j + (i * NUMCOLS);
- if (checkTrampoline(tmpPos) == true)
- {
- cout << "+";
- }
- else if (checkTraps(tmpPos) == true)
- {
- cout << "-";
- }
- else if (tmpPos == WINPOS)
- {
- cout << "!";
- }
- else if (players[p].getPos() == tmpPos)
- {
- cout << players[p].getSymbol();
- }
- else
- {
- cout << " ";
- }
- }
- }
- cout << endl;
- cout << right << " ";
- for (int j = 0; j < NUMCOLS; j++)
- {
- for (int p = 0; p < numPlayers + 1; p++)
- {
- cout << "-";
- }
- }
- }
- cout << " ";
- cout << "\n";
- cout << " ";
- for (int j = 0; j < NUMCOLS; j++)
- {
- cout << right << " " << j;
- }
- cout << "\n";
- }
- int Game::rollDice()
- {
- srand((unsigned)time(0));
- int HIGH = 6;
- int LOW = 1;
- int dieOne = rand() % (HIGH - LOW + 1) + LOW;
- int dieTwo = rand() % (HIGH - LOW + 1) + LOW;
- int roll = dieOne + dieTwo;
- return roll;
- }
- void Game::movePlayer(int& pos, int roll, string skip)
- {
- pos = pos + rollDice();
- if (checkTrampoline(pos) == true)
- {
- string skip = "Trampoline";
- }
- else if (checkTraps(pos) == true)
- {
- string skip = "Trap Door";
- }
- else
- {
- string skip = " ";
- }
- }
- bool Game::noWinner(int pos)
- {
- if (pos >= 142)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- void Game::printWinner(Players &player, int pos)
- {
- if (Game::noWinner(pos) == false)
- {
- cout << "The game is over and the winner is: " << player.getSymbol() << endl;
- }
- }
- void Game::nextTurn()
- {
- turn++;
- }
- int Game::getTurn() { return turn; }
- PLAYERS.H
- #pragma once
- #include <string>
- using namespace std;
- class Players {
- private:
- int pos = 0; // position of the player on the board
- char symbol; // represents each player on the game board
- int numPlayers = 0;
- public:
- Players();
- void setSymbol(char s); // sets the symbol of the players
- char getSymbol(); // retrieves the players symbol
- void setPos(int p); // sets the position of the player
- int getPos(); // gets the position of the player
- };
- MAIN.CPP
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <ctime>
- #include <string>
- #include <conio.h>
- #include "Game.h"
- #include "Players.h"
- using namespace std;
- void getPlayers(Players players[], int& numPlayers, const int MAXPLAYERS)
- {
- int numplayers = 0;
- char symbol;
- while (numPlayers > MAXPLAYERS || numPlayers < 2) {
- cout << "\n\nHow many players will play (2 - " << MAXPLAYERS << ")? ";
- cin >> numPlayers;
- cout << endl;
- }
- for (int i = 0; i < numPlayers; i++) {
- cout << "Enter Player " << i + 1 << "'s Symbol: ";
- cin >> symbol;
- players[i].setSymbol(symbol);
- }
- }
- int main() {
- const int MAX_PLAYERS = 6; //Max # players allowed
- Game G;
- Players players[MAX_PLAYERS]; //Array of Player objects
- int dice; //Value of dice roll
- int pos; //Position variable for a player
- int t = 0; //Index to keep track of whose turn it is
- char symbol;
- int numPlayers = 0; //Number of players playing the game
- string skip; //Denotes name of special space if landed on
- //Game simulation object
- srand(time(NULL));
- G.printGameHeader();
- getPlayers(players, numPlayers, MAX_PLAYERS);
- G.printGameBoard(players, numPlayers);
- //note: one loop iteration = 1 turn. Turn is decided upon by which
- //overall turn number it is
- while (G.noWinner(players[t].getPos())) {
- //set up for the next player's turn (player will be index t in array)
- G.nextTurn();
- t = (G.getTurn() - 1) % numPlayers;
- //handle turn for the current player (again, current player is at index t)
- pos = players[t].getPos();
- cout << "\n\n" << players[t].getSymbol() << "'s turn (space " << pos <<
- ")."
- << " Press any Key to Continue..." << endl;
- //wait for user input to proceed
- _getch();
- skip = "";
- dice = G.rollDice();
- G.movePlayer(pos, dice, skip);
- players[t].setPos(pos);
- cout << dice << " was rolled.";
- if (skip != "") {
- cout << " You landed on a " << skip;
- }
- cout << "\nMoving player " << players[t].getSymbol()
- << " to " << players[t].getPos() << endl;
- G.printGameBoard(players, numPlayers);
- }
- //print who won and end the program
- G.printWinner(players[t], players[t].getPos());
- cout << endl << endl;
- return 0;
- }
- ////////////////////////////////////////////////////////////////////////////////
- //Asks the user how many players will be playing. Then, retrieve player symbols
- //for each player from the user and store in the player array.
- ////////////////////////////////////////////////////////////////////////////////
- GAME.H
- #include <string>
- #include "Players.h"
- using namespace std;
- class Game {
- private:
- int turn;
- static const int NUMROWS = 15;
- static const int NUMCOLS = 10;
- static const int WINPOS = 142;
- public:
- Game();
- void printGameHeader();
- bool checkTraps(int &pos);
- bool checkTrampoline(int &pos);
- void printGameBoard(Players players[], int numPlayers);
- void printWinner(Players &players, int pos);
- int rollDice();
- void movePlayer(int& pos, int roll, string skip);
- bool noWinner(int pos);
- void nextTurn();
- int getTurn();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement