/*
Jackpot #5
http://www.geocities.com/SiliconValley/Park/3230/pas/pasles01.html
Submit this assignment by 23-Aug-2007 23:59 through e-mail to me
*/

#include <cstdio>
#include <cstdlib>
#include <ctime>

int main(){
    int n=4; // Number of players
    int starting_money=50;
    int bet[n]; // Bet of each player
    int digit[n][4];   // Each player has 4 digits
    int pay=20;        // bet of each game
    int h,i,j,k;
    bool no_money[n];
    bool game_end=false;    // Indicate whether the game ends
    bool same4,same3; 
    bool parity;   // Store whether all of them are odd or even.            
    bool prime;    // Store whether all are primes.
    bool z;
    bool sequence; // in sequence or not
    int difference; // To store the difference between two digits
    int bonus;     // Returned bonus at each round
    int pair;      // Count the number of same pair
    int lose=0;    // Count how many of them lost 
    int winner;    // Store who the winner is
    int turn=0;    // Turn Number
    char c;
    
    
    srand(time(0));
    
    printf("How much do each player have at first? ");
    scanf("%d",&starting_money);
    printf("\n");
    
    
    for (i=0;i<n;++i) {
        bet[i]=starting_money;
        no_money[i]=false;
        }
        
    while (!game_end){
          turn = turn +1; // Add the turn number by 1
          
          printf("Turn %d:\n",turn); // Display Turn number
          printf("Player\tMoney\n"); // "Player      Money"
          for (k=0;k<n;++k) 
              printf("%d\t%d\n",k+1,bet[k]);
          printf("\n");
          scanf("%c",&h);
          
          
          // Get money from each player at each round
          // If one has no more money, he/she is game over.
          for (i=0;i<n;++i) 
              if (bet[i]>=pay) bet[i]=bet[i]-pay;
              else no_money[i]=true;
              
          // Gives each player 4 digits from 0 to 9
          for (i=0;i<n;++i) 
            if (!no_money[i]) // Only give digits to those have money
             for (j=0;j<n;++j) digit[i][j]=rand()%10;
          
          // For each player k-th, check all the rules 
          for (k=0;k<n;++k) 
              if(!no_money[k])  // Only check for those who still playing
          {
          printf("Player %d:\t",k+1);
          for (i=0;i<4;++i)printf("%d\t",digit[k][i]);
          printf("\n");
          printf("Do you want to change a digit(Y/N) ?");
          scanf("%c",&c);
          if (c=='Y' || c=='y'){
             printf("Which digit do you want to change(1-4) ?");
             scanf("%d",&i);            
             // Add code here to change digit
             
            
             
             
             
             
             
             printf("New digit\n");
             for (i=0;i<4;++i)printf("%d\t",digit[k][i]);
             printf("\n");
          }
          
          bonus=0;  // Before checking there is no bonus

          // Check whether 4 are the same
          //Otherwise we proceed to check for 3 are the same          
          same4=true;
          for (i=0;i<3;++i) 
          same4=same4 && (digit[k][i]==digit[k][i+1]);
          //if (same4) same4=digit[k][i]==digit[k][i+1];
          
          
          // Check for odd or even parity
          parity=true;
          for (i=0;i<3;++i)
          parity=parity && (digit[k][i]%2==digit[k][i+1]%2);
          
          // Check for all primes.
          prime= true ;
          for ( i=0 ; i<4 ; ++i )  // Check all digits
              // If the previous all are prime
              // We proceed to check the next one
               if (prime) { 
                         z=true;
                         for ( j=2 ; j<=digit[k][i]-1 ; ++j )                    
                             if ( digit[k][i]%j==0 ) z=false;
                         prime = z ; 
                         }

          // Check for sequence
          sequence= true;
          difference = digit[k][1]-digit[k][0];
          for ( i=1;i<3;++i)
               if (digit[k][i+1]-digit[k][i]!=difference)
               sequence=false;


          // Check for 3 of them are the same
          // If there are already 4, no need to check
          if (!same4){
                      
                    // Draw (0,1,2 ) (0,1,3)
                    // (0,2,3) , (1,2,3) to check these pairs
                    // If anyone consists of the same number
                    // Then 3 of them are the same.
                    same3 = false;
                    for (h=0;h<4;++h)
                    for (i=h+1;i<4;++i)
                    for (j=i+1;j<4;++j)
                       if (digit[k][h]==digit[k][i])
                          if (digit[k][i]==digit[k][j])
                             same3=true;
                    
                    if (!same3){
                       // If 3 are the same, 
                       // don't need to check for 2
                       
                       pair=0;
                       // Draw 2 cards and check         
                       for (i=0;i<4;++i)
                       for (j=i+1;j<4;++j)
                       if (digit[k][i]==digit[k][j])
                       pair = pair + 1;         
                                
                    }
                    
          }
          
          // Give the bonus
          
          if (same4) bonus+=100; // 100% for all the same
          if (same3) bonus+=25;
          if (prime) bonus+=10;
          if (parity) bonus+=5;
          if (sequence) bonus+=50;
          if (pair==2) bonus+=75;
          if (pair==1) bonus+=10;
          
          if (bonus!=0) bet[k] = bet[k] + pay + pay * bonus/100;
          if (bonus) printf("Player %d get %d%% bonus\n",k+1,bonus);
          else printf("Player %d lose the bet\n",k+1);
          scanf("%c",&h);
          printf("\n");
          }// End of Checking 
          
          lose=0;
          for (k=0;k<n;++k)
              if (bet[k]<pay) lose = lose + 1;
              else winner=k;
          
          
          
          if (lose>=n-1) game_end=true;
    }

          printf("Player\tMoney\n"); // "Player      Money"
          for (k=0;k<n;++k) 
              printf("%d\t%d\n",k+1,bet[k]);
          scanf("%c",&h);
          if (lose==n-1) printf("Player %d wins the game.\n",winner+1);
          if (lose==n) printf("Oh... all of you lose.\n");


    
    int pause;
    scanf("%d",&pause);
    return 0;   
}

