Guest User

Untitled

a guest
Sep 17th, 2019
108
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4.  
  5. const char ranks[] = {'2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};
  6. const char suits[] = {'H', 'D', 'S', 'C'};
  7.  
  8. /*
  9. Returns char if valid suit
  10. */
  11. char isValidRank(char c){
  12. char result='0';
  13. int lengthofranks = sizeof(ranks) / sizeof(ranks[0]);
  14. for(int i =0;i<lengthofranks;i++){
  15. if(c==ranks[i]){
  16. result = '1';
  17. }
  18. }
  19. //returns 1 if 'c' is in ranks[], 0 if it isnt.
  20. return result;
  21. }
  22.  
  23. /*
  24. Returns char of 1 if valid suit otherwise it becomes 0
  25. */
  26. char isValidSuit(char c){
  27. char result='0';
  28. int lengthofsuits = sizeof(suits) / sizeof(suits[0]);
  29. for(int i = 0;i<lengthofsuits;i++){
  30. if(c ==suits[i]){
  31. result = '1';
  32. }
  33. }
  34. //printf("%c",result);
  35. return result;
  36. }
  37.  
  38. /*
  39. Returns char if valid suit is entered
  40. */
  41. char getTrump(){
  42. int errormsg = 0;
  43. int flagforisvalidsuit;
  44. char trumpsuit[1];
  45. char trump_suit;
  46. char flag1;
  47. do{
  48. if(errormsg){
  49. printf("Try again please\n");
  50. }
  51. printf("What is the trump suit?\n");
  52. scanf(" %s", trumpsuit);
  53. trump_suit = trumpsuit[0];
  54. flag1 = isValidSuit(trump_suit);
  55. if(flag1=='0' || (strlen(trumpsuit) != 1)){
  56. flagforisvalidsuit = 1;
  57. errormsg = 1;
  58. } else if((flag1 == '1') && (strlen(trumpsuit) == 1)){
  59. flagforisvalidsuit = 0;
  60. printf("Successfully added %c as trump suit.\n",trumpsuit[0]);
  61. return trump_suit;
  62. }
  63. } while(flagforisvalidsuit);
  64. // never gets out of while loop, just used this for debugging.
  65. printf("out of while loop");
  66. }
  67.  
  68.  
  69.  
  70. int main() {
  71. char trump_suit = getTrump();
  72. char player1_card[2];
  73. char player2_card[2];
  74. char player3_card[2];
  75. char player4_card[2];
  76. char player1_rank;
  77. char player1_suit;
  78. char player2_rank;
  79. char player2_suit;
  80. char player3_rank;
  81. char player3_suit;
  82. char player4_rank;
  83. char player4_suit;
  84. char flagforrank;
  85. char flagforsuit;
  86. int question1flag = 1;
  87. int question2flag = 0;
  88. int question3flag = 0;
  89. int question4flag = 0;
  90. int flagloop = 1;
  91. while(flagloop){
  92. if(question1flag){
  93. printf("Enter player 1's card(Rank followed by suit): \n");
  94. scanf(" %s",player1_card);
  95. flagforrank = isValidRank(player1_card[0]);
  96. flagforsuit = isValidSuit(player1_card[1]);
  97.  
  98. if((flagforrank=='0') || (flagforsuit=='0') || (strlen(player1_card)!=2)){
  99. printf("Invalid Card Please Re Enter.");
  100. } else{
  101. player1_rank = player1_card[0];
  102. player1_suit = player1_card[1];
  103. question1flag = 0;
  104. question2flag = 1;
  105. }
  106. }
  107. if(question2flag){
  108. printf("Enter player 2's card(Rank followed by suit): \n");
  109. scanf(" %s",player2_card);
  110. flagforrank = isValidRank(player2_card[0]);
  111. flagforsuit = isValidSuit(player2_card[1]);
  112.  
  113. if((flagforrank=='0') || (flagforsuit=='0') || (strlen(player2_card)!=2)){
  114. printf("Invalid Card Please Re Enter.");
  115. } else{
  116. player2_rank = player2_card[0];
  117. player2_suit = player2_card[1];
  118. question2flag = 0;
  119. question3flag = 1;
  120. }
  121. }
  122. if(question3flag){
  123. printf("Enter player 3's card(Rank followed by suit): \n");
  124. scanf(" %s",player3_card);
  125. flagforrank = isValidRank(player3_card[0]);
  126. flagforsuit = isValidSuit(player3_card[1]);
  127.  
  128. if((flagforrank=='0') || (flagforsuit=='0') || (strlen(player3_card)!=2)){
  129. printf("Invalid Card Please Re Enter.");
  130. } else{
  131. player3_rank = player3_card[0];
  132. player3_suit = player3_card[1];
  133. question3flag = 0;
  134. question4flag = 1;
  135. }
  136. }
  137. if(question4flag){
  138. printf("Enter player 4's card(Rank followed by suit): \n");
  139. scanf(" %s",player4_card);
  140. flagforrank = isValidRank(player4_card[0]);
  141. flagforsuit = isValidSuit(player4_card[1]);
  142.  
  143. if((flagforrank=='0') || (flagforsuit=='0') || (strlen(player4_card)!=2)){
  144. printf("Invalid Card Please Re Enter.");
  145. } else{
  146. player4_rank = player4_card[0];
  147. player4_suit = player4_card[1];
  148. question4flag = 0;
  149. flagloop = 0;
  150. }
  151. }
  152. }
  153. printf("%c%c, ",player1_card[0],player1_card[1]);
  154. printf("%c%c, ",player2_card[0],player2_card[1]);
  155. printf("%c%c, ",player3_card[0],player3_card[1]);
  156. printf("%c%c, ",player4_card[0],player4_card[1]);
  157.  
  158.  
  159. return 0;
  160. }
RAW Paste Data