• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Sep 18th, 2019 85 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.   Function that checks if a character is a valid rank or not
10. */
11.
12. int compareRank(char c, char c2,char ){
13.   if(c==trump)
14.
15.
16. }
17.
18.
19.
20.
21. char isValidRank(char c){
22.     char result='0';
23.     int lengthofranks = sizeof(ranks) / sizeof(ranks[0]);
24.     int i = 0;
25.     int found = 0;
26.     while (i<lengthofranks & !found)
27.     {
28.          if(c==ranks[i]){
29.             result = '1';
30.             found = 1;
31.          }
32.          i++;
33.     }
34.
35.     //returns '1' if char c is in ranks[], '0' if it isnt.
36.     return result;
37. }
38.
39. /*
40.   Function that checks if a character is a valid suit or not
41. */
42. char isValidSuit(char c){
43.     char result='0';
44.     int lengthofsuits = sizeof(suits) / sizeof(suits[0]);
45.     int i = 0;
46.     int found = 0;
47.     while (i<lengthofsuits & !found)
48.     {
49.          if(c==suits[i]){
50.             result = '1';
51.             found = 1;
52.          }
53.          i++;
54.     }
55.     //returns 1 if 'c' is in suit[], 0 if it isnt.
56.     return result;
57. }
58.
59. /*
60.   Returns char if valid suit is entered
61. */
62. char getTrump(){
63. int errormsg = 0;
64. int flagforisvalidsuit;
65. char trumpsuit[10];
66. char trump_suit;
67. char flag1;
68. do{
69.     if(errormsg){
70.       printf("Try again please.. you entered a invalid suit\n");
71.     }
72.     printf("What is the trump suit?\n");
73.     scanf(" %s", trumpsuit);
74.     trump_suit = trumpsuit[0];
75.     flag1 = isValidSuit(trump_suit);
76.     if(flag1=='0' || (strlen(trumpsuit) != 1)){
77.         flagforisvalidsuit = 1;
78.         errormsg = 1;
79.    } else if((flag1 == '1') && (strlen(trumpsuit) == 1)){
80.         flagforisvalidsuit = 0;
81.         printf("Successfully added %c as trump suit.\n",trumpsuit[0]);
82.         return trump_suit;
83.         }
84. } while(flagforisvalidsuit);
85. // never gets out of while loop, just used this for debugging.
86. printf("out of while loop");
87. }
88.
89.
90.
91.
92.
93. int main()
94. {
95.    char trump_suit = getTrump();
96.    char player1_card[2];
97.    char player2_card[2];
98.    char player3_card[2];
99.    char player4_card[2];
100.    char player1_rank;
101.    char player1_suit;
102.    char player2_rank;
103.    char player2_suit;
104.    char player3_rank;
105.    char player3_suit;
106.    char player4_rank;
107.    char player4_suit;
108.    char flagforrank;
109.    char flagforsuit;
110.    int question1flag = 1;
111.    int question2flag = 0;
112.    int question3flag = 0;
113.    int question4flag = 0;
114.    int flagloop = 1;
115.   while(flagloop){
116.     if(question1flag){
117.     printf("Enter player 1's card(Rank followed by suit): \n");
118.     scanf(" %s",player1_card);
119.     flagforrank = isValidRank(player1_card[0]);
120.     flagforsuit = isValidSuit(player1_card[1]);
121.     if(player1_card[0] == '.'){
122.       exit(0);
123.     }
124.     if((flagforrank=='0') || (flagforsuit=='0') || (strlen(player1_card)!=2)){
125.       printf("Invalid Card Please Re Enter.");
126.     }  else{
127.       player1_rank = player1_card[0];
128.       player1_suit = player1_card[1];
129.       question1flag = 0;
130.       question2flag = 1;
131.     }
132.     }
133.    if(question2flag){
134.     printf("Enter player 2's card(Rank followed by suit): \n");
135.     scanf(" %s",player2_card);
136.     flagforrank = isValidRank(player2_card[0]);
137.     flagforsuit = isValidSuit(player2_card[1]);
138.     if(player2_card[0] == '.'){
139.       exit(0);
140.     }
141.     if((flagforrank=='0') || (flagforsuit=='0') || (strlen(player2_card)!=2)){
142.       printf("Invalid Card Please Re Enter.");
143.     }  else{
144.       player2_rank = player2_card[0];
145.       player2_suit = player2_card[1];
146.       question2flag = 0;
147.       question3flag = 1;
148.  }
149.  }
150.  if(question3flag){
151.     printf("Enter player 3's card(Rank followed by suit): \n");
152.     scanf(" %s",player3_card);
153.     flagforrank = isValidRank(player3_card[0]);
154.     flagforsuit = isValidSuit(player3_card[1]);
155.     if(player3_card[0] == '.'){
156.       exit(0);
157.     }
158.     if((flagforrank=='0') || (flagforsuit=='0') || (strlen(player3_card)!=2)){
159.       printf("Invalid Card Please Re Enter.");
160.     }  else{
161.       player3_rank = player3_card[0];
162.       player3_suit = player3_card[1];
163.       question3flag = 0;
164.       question4flag = 1;
165.  }
166.  }
167.  if(question4flag){
168.     printf("Enter player 4's card(Rank followed by suit): \n");
169.     scanf(" %s",player4_card);
170.     flagforrank = isValidRank(player4_card[0]);
171.     flagforsuit = isValidSuit(player4_card[1]);
172.     if(player4_card[0] == '.'){
173.       exit(0);
174.     }
175.     if((flagforrank=='0') || (flagforsuit=='0') || (strlen(player4_card)!=2)){
176.       printf("Invalid Card Please Re Enter.");
177.     }  else{
178.       player4_rank = player4_card[0];
179.       player4_suit = player4_card[1];
180.       question4flag = 0;
181.       flagloop = 0;
182.  }
183.  }
184. }
185.  printf("%c%c, ",player1_card[0],player1_card[1]);
186.  printf("%c%c, ",player2_card[0],player2_card[1]);
187.  printf("%c%c, ",player3_card[0],player3_card[1]);
188.  printf("%c%c, ",player4_card[0],player4_card[1]);
189.
190. if(player1_card[0]==trump_suit)
191.
192.
193.
194.     return 0;
195. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?