-2

Hi, plz find my small solution.

#include <iostream>
#include <ctime>
#include <string>
using namespace std;
class cPlayer
{
      public:
             string Pl_name;
             int pos;
             int pts;
};

cPlayer p1, p2;

int random(int nMin, int nMax)
{ return rand() % (nMax - nMin + 1) + nMin; }

int main()
{
    srand(static_cast<unsigned int>(time(NULL)));

    p1.pos = 0 ; p1.pts = 0 ;
    p2.pos = 0 ; p2.pts = 0 ;
    bool flag=false;
    int i = 0 ;
    char x;
    while(true)
    {
                 i = random(1,6);
                 if(!flag)
                 {
                     cout<<"Enter P1 Name ";
                     cin>>p1.Pl_name;

                 }
                 cout << p1.Pl_name<<" is on the square " << p1.pos <<"\n Press Die";
                 cin>>x ;
                 cout << " rolled: " << i << std::endl;
                 while(i > 0)
                 {
                         p1.pos++;
                         if(p1.pos == 36)
                         {
                                   p1.pos = 0;
                                   p1.pts++;
                                   if(p1.pts == 5) break;
                         }
                         i--;
                 }
                 cout << p1.Pl_name<< " came to square " << p1.pos << " (" << p1.pts << " token)" << std::endl;
                 if(p1.pts == 5) break;

                 i = random(1,6);
                 if(!flag)
                 {
                     cout<<"Enter P2 Name ";
                     cin>>p2.Pl_name;
                     flag=true;
                 }
                 std::cout << p2.Pl_name<<" is on the square " << p2.pos ;
                 cout<<"Press Die ";
                 cin>>x;
                 cout << " rolled: " << i << std::endl;
                 while(i > 0)
                 {
                         p2.pos++;
                         if(p2.pos == 36)
                         {
                                   p2.pos = 0;
                                   p2.pts++;
                                   if(p2.pts == 5) break;
                         }
                         i--;
                 }
                 std::cout << p2.Pl_name<<" came to square " << p2.pos << " (" << p2.pts << " token)" << std::endl;
                 if(p2.pts == 5) break;
    }

    if(p1.pts == 5) std::cout << "The winner is " << p1.Pl_name<< std::endl;
    if(p2.pts == 5) std::cout << "The winner is "<< p2.Pl_name << std::endl;

    system("PAUSE");
    return 0;
}

I feel the output format is junk, plz change it as u like :idea:

Edited by Nick Evan: added code tags

Votes + Comments
Hi "plz" find your own thread
1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by mn.balakumar
0

Please find the code with the basic OO concept.

#include <iostream>
#include <ctime>
#include <string>
using namespace std;
class cPlayer
{
    private:
      int pos;
      int pts;
      string Pl_name;
    public:
      cPlayer()
      {
          pos=0;
          pts=0;
          srand(static_cast<unsigned int>(time(NULL)));
      }
      int random(int nMin,int nMax);
      void throwDie();
      void setName()
      {
          cin>>Pl_name;
      }
      string getName() { return Pl_name;}
      int getPos() { return pos;}
      int getPts() { return pts;}
      bool isWin()
      {
        if(pts == 5)
        {
            cout<<Pl_name <<" win the Game";
            return true;
        }
        return false;

      }

};



int cPlayer::random(int nMin, int nMax)
{ return rand() % (nMax - nMin + 1) + nMin; }

void cPlayer::throwDie()
{
    int i;
    char x;

    cout<< Pl_name<<" is on the square " << pos;
    cout<<"\nThrow Die(press Any key)";
    i = random(1,6);
    cin>>x ;
    cout << "\nRolled: " << i << std::endl;

    pos+=i;
    if(pos >= 36)
    {
        pos = 0;
        pts++;
    }
    cout << Pl_name<< " came to square " << pos << " (" << pts << " token)" << std::endl;


}

int main()
{
    cPlayer p1, p2;
    bool flag=false;
    int i = 0 ;
    char x;
    while(true)
    {
      if(!flag)
      {
         cout<<"Enter P1 Name ";
         p1.setName();

      }
      p1.throwDie();
      if(p1.isWin())
       break ;

      if(!flag)
      {
         cout<<"Enter P2 Name ";
         p2.setName();
         flag=true;
      }
      p2.throwDie();
      if(p2.isWin())
         break;
    }

    system("PAUSE");
    return 0;
}

Edited by Nick Evan: added code tags

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.