After playing for 5 or more times the total scores are going out or range (In the table generated at the end). I tried using long but didnt helped.

#include<conio.h>
#include<iostream>
#include<stdlib.h>
#include<string>
#include<windows.h>
#include<time.h>

using namespace std;

unsigned flag=0, ply_m,ply_t,ply_s,zz,i;

//funct for gotoxy
void gotoxy(int x, int y)
{
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

//--function to shuffle contents of an array
void shuffle(int a[], int size)
 {
 int temp,ranno,last;
 srand(time(NULL));
 for(last=size;last>1;last--)
  {
    ranno = rand()%last;
    temp = a[ranno];
    a[ranno] = a[last-1];
    a[last-1] = temp;
  }
}

//function generating random tickets
 void  fun_ply(int n,int ply_y,int ply_x[10], int k)
   { if(n==0)
     {
      Sleep(1000);
      cout<<"King";
      ply_x[k]=1000;
     }

      else if(n==2)
     {
     Sleep(1000);
    cout<<"xxx";
    ply_s=ply_y;
      ply_x[k]=300;
     }

      else if(n==3)
     {
      Sleep(1000);
      cout<<"xxx";
      ply_t=ply_y;
      ply_x[k]=0;
      zz =k;
     }

      else
     {
      Sleep(1000);
      cout<<"Minister";
      flag = 1;
      ply_m = ply_y;
      ply_x[k]=500;
      zz =k;

     }
    }


 int main(void)
  {
   system("cls");
   unsigned sp1=0,sp2=0,sp3=0,sp4=0,sp44=0,n,p,s;
   int ply_1[2],ply_2[2],ply_3[2],ply_4[2],res[4],shuf[4]={1,3,0,2};
   char d;
   cout<<"\n";
   cout<<"*** Welcome to the game of RAJA MANTRI CHOR SIPAHI ***";
   cout<<"\nFor help and rules press h";
   cout<<"\nTo start playing press any other key   ";d=getch();
   system("cls");

 if((d=='h')||(d=='H'))
  {
   system("cls");
   cout<<"King Minister Thief Soldier or Raja Mantri Chor Sipahi is the classic ticket\n";
   cout<<"game played between 4 players"<<endl<<"Each player will get a ticket randomly.\n";
   cout<<"\nTicket points are as follow: "<<endl<<"King    : 1000"<<endl<<"Minister: 500"<<endl<<"Soldier : 300"<<endl<<"Thief   : 0";
   cout<<"\n\nThe player who got the 'Minister' ticket has to guess the 'Thief' among\n";
   cout<<"'Soldier' and 'Thief'\n\n";
   cout<<"If he guess correct he retains his ticket, but if he guess wrong he has to\n";
   cout<<"switch his ticket with 'Thief'\n";
   cout<<"Hence he will get 0 points for guessing wrong and 'Thief' will gain 500 points\n";
   cout<<"and will become new 'Minister'";
   cout<<"\n\nPress any key to return to game...";
   getch();
   system("cls");
  }

 cout<<"Enter no of players\n";
 cin>>p;
  i =0;
 char c;
 while(c!='n')
   {
    shuffle(shuf,4);
    cout<<"Player 1 :";
    fun_ply(shuf[1],1,ply_1,i);

    cout<<endl;

    cout<<"Player 2 :";
     fun_ply(shuf[2],2,ply_2,i);

     cout<<endl;

     cout<<"Player 3 :";
     fun_ply(shuf[3],3,ply_3,i);

     cout<<endl;
     cout<<"Player 4 :";
     fun_ply(shuf[0],4,ply_4,i);

     //logic for guessing thief
       if(flag==1)
    {
    int l,ply_stemp,ply_ttemp;
    l = rand()%2;
    if(l==0)
    {
     ply_stemp=ply_t;
     ply_ttemp=ply_s;
    }
    else
     {
      ply_stemp=ply_s;
      ply_ttemp=ply_t;
     }

    cout<<"\n\nPlayer "<<ply_m<<" guess thief between player "<<ply_stemp<<" and "<<ply_ttemp<<":   ";
    int c;
    if(ply_m<=p)
    {
    cin>>c;
    }
    else
    {
    cout<<"\nComputer is guessing the thief:   ";
    Sleep(2000);
    srand(time(NULL));
    int g=rand()%2;
    if(g==0)
     {
      c=ply_t;
      cout<<c;
     }
    else
     {
      c=ply_s;
      cout<<c;
     }
    }
    Sleep(2000);
    cout<<"\nThief is player "<<ply_t<<endl;
    Sleep(1000);
    if(c==ply_t)
     {
      cout<<"\nCongrats! player "<<ply_m<<" you guessed correct\n";
     }
    else
    {
     cout<<"\nOops! player "<<ply_m<<" you guessed wrong\n";
     cout<<"Exchange your ticket with player "<<ply_t<<"\n";
     cout<<"\nPlayer "<<ply_m<<" you became Thief and will get 0 points\n";
     cout<<"Player "<<ply_t<<" you became Mininster and will get 500 points\n";

    //swapping scores in case of wrong guess
     if(ply_m==1)
     {
      ply_1[zz]=0;
     switch(ply_t)
      {
       case 2:
       ply_2[zz]=500;
       break;

       case 3:
       ply_3[zz]=500;
       break;

       case 4:
       ply_4[zz]=500;
       break;

      }
     }

     else  if(ply_m==2)
     {
      ply_2[zz]=0;
     switch(ply_t)
      {
       case 1:
       ply_1[zz]=500;
       break;

       case 3:
       ply_3[zz]=500;
       break;

       case 4:
       ply_4[zz]=500;
       break;

      }
     }

       else if(ply_m==3)
     {
      ply_3[zz]=0;
     switch(ply_t)
      {
       case 2:
       ply_2[zz]=500;
       break;

       case 1:
       ply_1[zz]=500;
       break;

       case 4:
       ply_4[zz]=500;
       break;

      }
     }

     else
     {
      ply_4[zz]=0;
     switch(ply_t)
      {
       case 2:
       ply_2[zz]=500;
       break;

       case 3:
       ply_3[zz]=500;
       break;

       case 1:
       ply_1[zz]=500;
       break;

      }
     }
     }
    }

          //showing current scores here
    cout<<"\n** Current Scores of round "<<i+1<<" **\n";


      cout<<"Player 1:   "<<ply_1[i]<<endl<<"Player 2:   "<<ply_2[i]<<endl<<"Player 3:   "<<ply_3[i]<<endl<<"Player 4:   "<<ply_4[i]<<endl;
  cout<<"\nDo u want to play more?(y/n)   ";
     c=getche();
     cout<<endl;
     i++;

}

// use this to display final scores in tab form
system("cls");
cout<<"Game terminated - Total rounds played: "<<i<<endl;
cout<<"Final scores are as follows:\n";

//-------> table design

           char aa=218, ab = 196, ac = 191,ad=179, ae = 192, af =217;
               int j,k;
               cout<<aa;
               for(j=0;j<43;j++)
            {
             cout<<ab;
            }
            cout<<ac;

               for(j=0;j<19;j++)
            cout<<endl<<ad;

            cout<<endl<<ae;

               for(j=0;j<43;j++)
               cout<<ab;
                cout<<af;

            for(k=0;k<23;k++)
            {
             for(j=21;j>=3;j--)
              {
                gotoxy(44,j);
                cout<<ad;
              }
            }
               cout<<endl;
            gotoxy(2,6);
            for(j=43;j>=2;j--)
             {
              cout<<ab;
             }

               for(j=4;j<=21;j++)
              {
                gotoxy(24,j);
                cout<<ad;
              }


             for(j=4;j<=21;j++)
              {
                gotoxy(12,j);
                cout<<ad;
              }
               for(j=4;j<=21;j++)
              {
                gotoxy(34,j);
                cout<<ad;
              }


              gotoxy(2,18);
              for(j=0;j<42;j++)
              cout<<ab;

              gotoxy(2,20);
              for(j=0;j<42;j++)
              cout<<ab;

//------->table design ended

//calculating total score of each player and showing in table

    j=7;
    for(k=0;k<i;k++)
   {
      gotoxy(3,4);
      cout<<"Player1";


      gotoxy(4,j);
      cout<<ply_1[k]<<"\n";


      sp1=sp1+ply_1[k];
      gotoxy(4,19);
      cout<<sp1;
      j++;
   }

    j=7;
    for(k=0;k<i;k++)
    {

     gotoxy(15,4);
      cout<<"Player2";
      gotoxy(16,j);
      cout<<ply_2[k]<<"\n";
     sp2=sp2+ply_2[k];
      gotoxy(16,19);
      cout<<sp2;
      j++;
   }


     j=7;
    for(k=0;k<i;k++)
     {
      gotoxy(26,4);
      cout<<"Player3";


      gotoxy(28,j);
      cout<<ply_3[k]<<"\n";


     sp3=sp3+ply_3[k];
      gotoxy(28,19);
      cout<<sp3;
      j++;
     }

     j=7;
    for(k=0;k<i;k++)
    {
     gotoxy(36,4);
      cout<<"Player4";


      gotoxy(37,j);
      cout<<ply_4[k]<<"\n";

      sp4=sp4+ply_4[k];
      gotoxy(37,19);
      cout<<sp4;
      j++;
   }
     j=0;

//ranking logic :(

if((sp1>sp2) && (sp1>sp3) && (sp1>sp4))
  {
   gotoxy(4,21);
   cout<<"King";
   if((sp2>sp3)&&(sp2>sp4))
    {
     gotoxy(15,21);
     cout<<"Minister";

    if(sp3>sp4)
     {
      gotoxy(26,21);
      cout<<"Soldier";
      gotoxy(37,21);
      cout<<"Theif";
     }
    else
     {
      gotoxy(37,21);
      cout<<"Soldier";
      gotoxy(26,21);
      cout<<"Theif";
     }
   }

   else if((sp3>sp2)&&(sp3>sp4))
    {
     gotoxy(26,21);
     cout<<"Minister";

      if(sp2>sp4)
       {
    gotoxy(15,21);
    cout<<"Soldier";
    gotoxy(37,21);
    cout<<"Thief";
       }
      else
       {
    gotoxy(37,21);
    cout<<"Soldier";
    gotoxy(15,21);
    cout<<"Theif";
       }
     }

    else if((sp4>sp2)&&(sp4>sp3))
     {
      gotoxy(37,21);
      cout<<"Minister";

      if(sp2>sp3)
       {
    gotoxy(15,21);
    cout<<"Soldier";
    gotoxy(26,21);
    cout<<"Theif";
       }
      else
       {
    gotoxy(26,21);
    cout<<"Soldier";
    gotoxy(15,21);
    cout<<"Theif";
       }
     }
   }
// sp1 grts comp
// sp2 grt check
else if((sp2>sp1)&&(sp2>sp3)&&(sp2>sp4))
 {
  gotoxy(15,21);
  cout<<"King";
  if((sp1>sp3)&&(sp1>sp4))
   {
    gotoxy(4,21);
    cout<<"Minister";
    if(sp3>sp4)
     {
      gotoxy(26,21);
      cout<<"Soldier";
      gotoxy(37,21);
      cout<<"Thief";
     }
    else
     {
      gotoxy(37,21);
      cout<<"Soldier";
      gotoxy(26,21);
      cout<<"Thief";
     }
    }
   else if((sp3>sp1)&&(sp3>sp4))
    {
     gotoxy(26,21);
     cout<<"Minister";
     if(sp1>sp4)
      {
       gotoxy(4,21);
       cout<<"Soldier";
       gotoxy(37,21);
       cout<<"Thief";
      }
     else
      {
       gotoxy(37,21);
       cout<<"Soldier";
       gotoxy(4,21);
       cout<<"Thief";
      }
     }
    else if((sp4>sp1)&&(sp4>sp3))
     {
      gotoxy(37,21);
      cout<<"Minister";
      if(sp1>sp3)
       {
    gotoxy(4,21);
    cout<<"Soldier";
    gotoxy(26,21);
    cout<<"Thief";
       }
      else
       {
    gotoxy(26,21);
    cout<<"Soldier";
    gotoxy(4,21);
    cout<<"Thief";
       }
      }
    }
 // sp2 grts comp
 // sp3 check

else if((sp3>sp1)&&(sp3>sp2)&&(sp3>sp4))
 {
  gotoxy(26,21);
  cout<<"King";
   if((sp1>sp2)&&(sp1>sp4))
    {
     gotoxy(4,21);
     cout<<"Minister";
     if(sp2>sp4)
      {
       gotoxy(15,21);
       cout<<"Soldier";
       gotoxy(37,21);
       cout<<"Thief";
      }
     else
      {
       gotoxy(37,21);
       cout<<"Soldier";
       gotoxy(15,21);
       cout<<"Thief";
      }
     }
    else if((sp2>sp1)&&(sp2>sp4))
     {
       gotoxy(15,21);
       cout<<"Minister";
       if(sp1>sp4)
    {
     gotoxy(4,21);
     cout<<"Soldier";
     gotoxy(37,21);
     cout<<"Thief";
    }
       else
    {
     gotoxy(37,21);
     cout<<"Soldier";
     gotoxy(4,21);
     cout<<"Thief";
    }
      }
     else if((sp4>sp1)&&(sp4>sp2))
      {
       gotoxy(37,21);
       cout<<"Minister";
       if(sp1>sp2)
     {
      gotoxy(4,21);
      cout<<"Soldier";
      gotoxy(15,21);
      cout<<"Thief";
     }
       else
    {
     gotoxy(15,21);
     cout<<"Soldier";
     gotoxy(4,21);
     cout<<"Thief";
    }
       }
      }
 //sp3 chk comp
 // sp4 chk
else if((sp4>sp1)&&(sp4>sp2)&&(sp4>sp3))
 {
  gotoxy(37,21);
  cout<<"King";
  if((sp1>sp2)&&(sp1>sp3))
   {
    gotoxy(4,21);
    cout<<"Minister";
    if(sp2>sp3)
     {
      gotoxy(15,21);
      cout<<"Soldier";
      gotoxy(26,21);
      cout<<"Thief";
     }
    else
     {
      gotoxy(26,21);
      cout<<"Soldier";
      gotoxy(15,21);
      cout<<"Thief";
     }
    }
   else if((sp2>sp1)&&(sp2>sp3))
    {
     gotoxy(15,21);
     cout<<"Minister";
     if(sp1>sp3)
      {
       gotoxy(4,21);
     cout<<"Soldier";
     gotoxy(26,21);
     cout<<"Thief";
       }
      else
       {
    gotoxy(26,21);
     cout<<"Soldier";
     gotoxy(4,21);
     cout<<"Thief";
       }
     }
    else if((sp3>sp1)&&(sp3>sp2))
     {
      gotoxy(26,21);
      cout<<"Minister";
      if(sp1>sp2)
       {
    gotoxy(4,21);
     cout<<"Soldier";
     gotoxy(15,21);
     cout<<"Thief";
    }
       else
    {
     gotoxy(15,21);
     cout<<"Soldier";
     gotoxy(4,21);
     cout<<"Thief";
    }
       }
      }
 // sp4 grt comp
 // all check comp

    getch();
    }

Recommended Answers

All 3 Replies

TL;DR. Instead of posting the entire code post only the relevant part(s) or create a small example which reproduces the issue.

Bad variable naming... Very difficult to follow...

Anyway, line 78, you declare array size of 2 to each ply. In your while loop, you use i as index pointing to your array value (in your fun_ply()). You keep increasing the value of i (line 271) when the answer is not equal to 'n' (line 107). So what would happen on the third round coming through (when i is equal to 2)?

Gonbe: I'll keep that in mind for future postings.
Taywin: I'll try to use more proper variable names. And thanks for pointing out the problem, idk why I declared array size of 2 when I was going to run the loop for 10 times. I corrected it. and its working perfectly now.
Thnx agn for quick and to the point reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.