Making Pig in C++

Started by
12 comments, last by Downer 18 years, 3 months ago
Thank all of you who replied very much, you pointed out some of my obvious mistakes and made this entire process much much more bearable. Hopefully my game goes over well with my school board. It's the equivalent of a thesis project.

For those who are interested, this is the current, finished product:

//Pig 1.0 - A simple, text based version of the dice game Pig//Programmed by: Devin Gray//Rules of Pig courtesy of www.wikipedia.com//Design Notes// 01/02/2006 - Pig 1.0 Completed#include <iostream>#include <stdlib.h>#include <ctime>#include <vector>#include <string>#include <algorithm>using namespace std;string Aname;   //Global string Aname, or Player A's namestring Bname;   //Global string Bname, or Player B's namestring Rname;   //Global string Rname, or Returned name.void Instructions();void About();class Player{public:  //Begin the public access level section    Player(int Ptotal=0);  //Declares the constructor    string GetName();    int Roll();    //Creates a random number from 1-6 and determines whether or not Rtotal is added to PScore    int GetScore();   //Declares the function GetScore(), which calculates the constant int PScore, or the player's score    void DisplayScore();    //Declares the function DisplayScore(), which displays PScore    void Victory();    //Declares the function Victory(), which displays a congratulatory message when the player wins    int Ptotal;  //Declares the integer Ptotal, which holds the total score of the player    int m_Score; //Declares the integer m_Score    string name; //Stores the result of GetName()private: //Begin the private access level section    short int turns; //Declares the integer rolls, which holds the number of rolls it took for a player to win    int Rtotal;  //Declares the integer Rtotal, which holds the total score accumulated in one round    vector<int> PScore;  //Creates a vector of integers with the purpose of storing the Player's Score, Ptotal    vector<int> RScore;  //Creates a vector of integers with the purpose of storing the sum of the rolled values, RScore};//Defines the constructor    Player::Player(int Ptotal){    vector<int> PScore;    vector<int> RScore;    RScore.clear();    PScore.clear();    turns = 0;    m_Score = Ptotal;    //Sets int score to int m_Score}//End of constructor definition//Defines the GetName() functionstring Player::GetName(){cout << "\nWhat is your name?\n";string name;cin >> name;cout << "\nGood luck, " << name << ".";Rname = name;return name;}//End of the GetName() function definition//Defines the Roll() functionint Player::Roll()//This function rolls the die and returns a value{RScore.clear();      //Empties the vector before usevector<int>::const_iterator iter;  Rtotal=0;char again; //Creates a char value to determine whether or not the player wishes to roll again      do       {      int randomNumber = rand(); //Generate random number      int die = (randomNumber % 6) + 1; //Get a number between 1 and 6      cout << "You rolled a " << die << endl;            if (die == 1)  //If the die was a 1...      {            cout << "\nPig! Your score for this round is lost.\n";  //Then the round's value is lost            Rtotal = 0;            RScore.clear();            again == 'n';            Sleep(1500);            break;      }            //If the die did not equal 1 the current round's score is calculated      else      {      RScore.push_back(die);      //Adds the die's value to the vector      for (iter = RScore.begin(); iter != RScore.end(); ++iter)      //Sets the iterator to the beginning of the vector and cycles through it            Rtotal += (*iter);      //Adds the sum of the iterator to total            RScore.pop_back();      //This line clears the last element. If this does not happen, the previous round's score doubles.            //Example:  2. Score is 2. Next round, 4. Score is 10.            cout << "\n" << Rname << ", your score for this round is:" << Rtotal;  //Displays the round's total score            cout << "\nWould you like to roll again? (Y/N)\n";  //Prompts the user to roll again            cin >> again;            if (again !='y' && again != 'Y')                        cout << "\nThen you hold."; //If the player's response was not y, then the player holds            else               continue;      }            } while (again == 'y' || again == 'Y');      ++ turns;      return Rtotal;}//End of the Roll() function//Defining the GetScore() functionint Player::GetScore(){    vector<int>::const_iterator iter;      PScore.push_back(Rtotal);      //Adds the round's total value (Rtotal) to the vector      Rtotal = 0;      for (iter = PScore.begin(); iter != PScore.end(); ++iter)      //Sets the iterator to the beginning of the vector and cycles through it            m_Score += (*iter);      //Adds the sum of the iterator to total      PScore.pop_back();      //This line clears the last element. If this does not happen, the previous round's score doubles.    return m_Score;}//End of GetScore() function definition//Defining the DisplayScore() functionvoid Player::DisplayScore()//This function displays the score and then shows a message either advising or congratulating the player{cout << "\n" << Rname << ", your score is " << m_Score << ".";    if (m_Score <=99)    {        if (m_Score == 0)                cout << "\nOink oink!";        if (m_Score > 0  && m_Score <= 20)                cout << "\nStill a beginner.";        if (m_Score > 20 && m_Score <= 40)                cout << "\nNow you're showing some progress.";        if (m_Score > 40 && m_Score <= 60)                cout << "\nAlmost...";        if (m_Score > 60 && m_Score <= 80)                cout << "\nGo big or go home!";        if (m_Score > 80 && m_Score <= 99)                cout << "\nYou're so close you can almost taste the bacon!.";    }    else    {        cout << "\nYou broke 100!";    }    Sleep(500);  //Pauses the program for 1/2 a second to make the program more legible}//End of DisplayScore() function definition//Defining the Victory() functionvoid Player::Victory(){            cout << "\nCongratulations, " << name << "! You won!\n\n";            cout << "\nIt took you " << turns << " turns to win!";            //Depending on how many turns it took the player to win, a message is displayed            if (turns >=1 && turns <=5)                        cout << "\nIncredible!";            if (turns >=6 && turns <=10)                        cout << "\nDefinitely impressive.";            if (turns >=11 && turns <=15)                        cout << "\nNot bad.";            if (turns >=16 && turns <=20)                        cout << "\nYou could do better.";            if (turns >=21)                        cout << "\nWow, you had some really bad luck, huh?";}//End of the Player classint main(int argc, char *argv[]){  srand(time(0)); //Seed random number generator based on current time  Instructions();  Player A; //Creates an object in the player class named A  Player B; //Creates an object in the player class named B    char PlayAgain;  PlayAgain == 'y';  do  {  cout << "\n\nPlayer A,";  Aname = A.GetName(); //Sets global string Aname to the result of A's GetName() function  cout << "\n\nPlayer B,";  Bname = B.GetName(); //Sets global string Bname to the result of B's GetName() function  do //While both player's scores are <= 99, the following gameplay loop executes  {  cout << "\n\n" << Aname << ", you're up.\n";  Sleep(250); // Delays the loop for 1/4 of a second, or 250 milliseconds  Rname = Aname; //Sets global string Rname to Aname for the duration of A's turn so that A's name is displayed correctly during each function  A.Roll();  A.GetScore();  A.DisplayScore();  Rname = "";  //Sets Rname to blank space    cout << "\n\n" << Bname << ", do you think you can beat " << Aname << "'s roll?\n";  Sleep(250); // Delays the loop for 1/4 of a second, or 250 milliseconds  Rname = Bname;  //Sets global string Rname to Bname for the duration of B's turn so that B's name is displayed correctly during each function  B.Roll();    B.GetScore();  B.DisplayScore();  Rname = "";  //Sets Rname to blank space  } while (A.m_Score <=99 && B.m_Score <=99);  //The game loop continues as long as neither player has exceeded 100 points    if (A.m_Score >=100 && B.m_Score <=99)  //If A exceeds 100 points but B doesn't then A wins    Rname = Aname; //Sets Rname to Aname    A.Victory();  if (B.m_Score >=100 && A.m_Score <=99)  //If B exceeds 100 points but A doesn't then B wins    Rname = Bname; //Sets Rname to Bname    B.Victory();  if (A.m_Score >=100 && B.m_Score >=100) //If they both exceed 100 points, they tie.    cout << "You tie!";      cout << "\n\nGame Over\n\n!";  cout << "Would you like to play again? (Y/N)";  cin >> PlayAgain;  } while (PlayAgain == 'y' || PlayAgain == 'Y' );    About();    system("PAUSE");	  return 0;}void Instructions(){    cout << "\t***PIG****\n\n";    cout << "The goal of Pig is to reach 100 points before the other player does.";    cout << "\nEach time you roll the die points are added to your turn total.";    cout << "\nIf you choose to stop, the points are added to your score.";    cout << "\nBut if you roll a 1, then all your points for that turn are lost!";    cout << "\n\nJust how well can you throw the dice?";}void About(){    cout << "\n\nPig is a MS-DOS game programmed by Devin Gray.";}


There are probably better ways of programming it, more efficient ways, etc.
But I'm pretty proud of it considering this is my first real program :)
Advertisement
some suggestions:
1) dont include stdlib.h, include cstdlib
2) you shouldnt need the three global names, try to get rid of them
besides that, great game!
You also need to include <windows.h> for Sleep() and System().
I might have scanned thru the posts a little to fast so Im not sure this was mentioned, what you might want to do is have a general style guide to go by for your classes, for instances you have a member variable m_score, so why not have all member variable inside a class start with the prefix m_, that way when you have a member variable Ptotal it would be m_Ptotal, and having m_Ptotal=Ptotal; no longer becomes confusing.

This topic is closed to new replies.

Advertisement