<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. 1: Enter player stored name
  3. 2: Recall stored name
  4. 3: Welcome player
  5. 4: Generate random card number so players can't predict cards
  6. 5: For 10 cards, ranging with values from 1-10
  7. 6: let each player input card numbers
  8. 7: store input values
  9. 8: repeat steps 4-7 for the second player
  10. 9: Fetch stored value for each player
  11. 10: compare each player picked value to decide on the winner
  12. */
  13. #include <stdio.h>  /*header file standard inout/output file*/
  14. #include <time.h>   /*grabs time from system for seeded random vairable*/
  15.  
  16. int main ()
  17.  
  18. {
  19.  
  20. printf("🎲WELCOME TO NUMBER STACKS!🎲 %c");
  21.  
  22.  char playername1 [20];       /*storing playernames as a char variable*/
  23.  char playername2 [20];
  24.  
  25.  
  26.  printf("\nplease type your name player1:");
  27.  scanf("%s", & playername1);
  28.  printf("\n Hello! ");
  29.  printf( "%s", playername1 );
  30.  
  31. printf("\nplease type your name player2:");
  32. scanf("%s", & playername2);
  33. printf("\n Hello! ");
  34. printf( "%s", playername2 );
  35.  
  36. int xi=0;
  37. int xhow_many_card=10;
  38. int xvalue[xhow_many_card];
  39. int xgame_val[10];
  40. int xrunning_value;
  41. int yi=0;
  42. int yhow_many_card=10;
  43. int yvalue[yhow_many_card];
  44. int ygame_val[10];
  45. int yrunning_value;
  46. char storage [10];
  47. char x;
  48.  
  49.  
  50. printf("\nYour up first %s", playername1);
  51. printf("\nYou have %d cards\n", xhow_many_card);
  52.  
  53. srand(time(NULL));                                   /*seeded random variable with the domain as time, always random as time is ever changing*/
  54. for(xi=0; xi<xhow_many_card; xi++){                  /*using for loop to limit the card from 1-10, in increments of 1*/
  55.     xvalue[xi]= (rand()%10)+1;                       /*storing random variables in array*/
  56.    
  57. }
  58.  
  59.  
  60. printf("here are your cards!\n");                    /*printf function to display text on console window*/
  61.  
  62.     for(xi=0; xi<xhow_many_card; xi++){
  63.     printf("card number:%d\t=",xi);                  /*using decimal placeholder to print interger from a predefined variable*/
  64.     printf("%d\n",xvalue[xi]);                       /*correct spacing and new line for easy viewing of information*/
  65.     }   
  66. printf("\nwhat card number would you like to play, please enter:");
  67. scanf("\n%d", &xgame_val[0]);                        /*using scan f to store a decimal value into an array*/
  68.  
  69. while (xgame_val[0]>10){                             /*using while loop to return to scanf function for incorrect user inputs defined by a range*/
  70.     printf("\ninvalid card, please play a card number from your stack");
  71.     scanf("\n%d", &xgame_val[0]);
  72.  
  73. }
  74.  
  75. switch(xgame_val[0])                                 /*A more organised structure to follow than using if else statement, better compatibility and allows for case by case actions to be excuted*/
  76. {
  77.     case 0:
  78.     printf("you played %d", xvalue[0]);
  79.     xvalue[0]=xrunning_value;
  80.     break;                                           /*allows the programme to ext closest operation*/
  81.    
  82.     case 1:
  83.     printf("you played %d", xvalue[1]);
  84.     xvalue[1]=xrunning_value;
  85.     break;
  86.  
  87.     case 2:
  88.     printf("you played %d", xvalue[2]);
  89.     xvalue[2]=xrunning_value;
  90.     break;
  91.    
  92.     case 3:
  93.     printf("you played %d", xvalue[3]);
  94.     xvalue[3]=xrunning_value;
  95.     break;
  96.  
  97.     case 4:
  98.     printf("you played %d", xvalue[4]);
  99.     xvalue[4]=xrunning_value;
  100.     break;
  101.  
  102.     case 5:
  103.     printf("you played %d", xvalue[5]);
  104.     xvalue[5]=xrunning_value;
  105.     break;
  106.  
  107.    
  108.     case 6:
  109.     printf("you played %d", xvalue[6]);
  110.     xvalue[6]=xrunning_value;
  111.     break;
  112.  
  113.     case 7:
  114.     printf("you played %d", xvalue[7]);
  115.     xvalue[7]=xrunning_value;
  116.     break;
  117.    
  118.     case 8:
  119.     printf("you played %d", xvalue[8]);
  120.     xvalue[8]=xrunning_value;
  121.     break;
  122.  
  123.     case 9:
  124.     printf("you played %d", xvalue[9]);
  125.     xvalue[9]=xrunning_value;
  126.     break;
  127.    
  128.     default:
  129.     printf("\nplease pick valid card number");
  130. }
  131.  
  132.  
  133. printf("\nYour up %s",playername2);
  134.  
  135. printf("\nYou have %d cards\n", yhow_many_card);
  136.  
  137. srand(time(NULL));
  138. for(yi=0; yi<yhow_many_card; yi++){
  139.     yvalue[yi]= (rand()%10)+1;
  140.    
  141. }
  142.  
  143. printf("here are your cards!\n");
  144.  
  145.     for(yi=0; yi<yhow_many_card; yi++){
  146.     printf("card number:%d\t=",yi);
  147.     printf("%d\n",yvalue[yi])
  148.     }   
  149. printf("\nwhat card number would you like to play, please enter:");
  150. scanf("\n%d", &ygame_val[0]);
  151.  
  152. while (ygame_val[0]>10){
  153.     printf("\ninvalid card, please play a card number from your stack");
  154.     scanf("\n%d", &ygame_val[0]);
  155. }
  156.  
  157.  
  158. switch(ygame_val[0])
  159. {
  160.     case 0:
  161.     printf("you played %d", yvalue[0]);
  162.     yvalue[0]=yrunning_value;
  163.     break;
  164.    
  165.     case 1:
  166.     printf("you played %d", yvalue[1]);
  167.     yvalue[1]=yrunning_value;
  168.     break;
  169.  
  170.     case 2:
  171.     printf("you played %d", yvalue[2]);
  172.     yvalue[2]=yrunning_value;
  173.     break;
  174.    
  175.     case 3:
  176.     printf("you played %d", yvalue[3]);
  177.     yvalue[3]=yrunning_value;
  178.     break;
  179.  
  180.     case 4:
  181.     printf("you played %d", yvalue[4]);
  182.     yvalue[4]=yrunning_value;
  183.     break;
  184.  
  185.     case 5:
  186.     printf("you played %d", yvalue[5]);
  187.     yvalue[5]=yrunning_value;
  188.     break;
  189.    
  190.     case 6:
  191.     printf("you played %d", yvalue[6]);
  192.     yvalue[6]=yrunning_value;
  193.     break;
  194.  
  195.     case 7:
  196.     printf("you played %d", yvalue[7]);
  197.     yvalue[7]=yrunning_value;
  198.     break;
  199.    
  200.     case 8:
  201.     printf("you played %d", yvalue[8]);
  202.     yvalue[8]=yrunning_value;
  203.     break;
  204.  
  205.     case 9:
  206.     printf("you played %d", yvalue[9]);
  207.     yvalue[9]=yrunning_value;
  208.     break;
  209.    
  210.     default:
  211.     printf("\nplease pick valid card number");
  212. }
  213.  
  214.  
  215. if(xrunning_value>yrunning_value){                                /*if statement to allow a specific action to be excuted is the contion in the bracketsa are true*/
  216.     printf("\n%s wins",playername1);}                             /*if else statement if first gcodition not true excute the following*/
  217. else if(xrunning_value<yrunning_value){
  218.     printf("\n%s wins",playername2);}
  219. else if(xrunning_value=yrunning_value){
  220.     printf("\nIts a draw");}
  221. else printf("\nunrecognised error");
  222.  
  223. return 0;                                                         /*end of prgram return*/
  224. }