Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.80 KB | None | 0 0
  1. //GoFish.c
  2. //DSC
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <string.h>
  8.  
  9. void printDeck(int deck[][5]);
  10. int goFish(int deck[][5], int player);
  11. void printHand(int p, int d[][5]);
  12. char ps(int suit);
  13. int checkInput(int chkfor, int p, int d[][5]);
  14. int checkPlayer(int chkfor, int src, int tgt, int deck[][5]);
  15. void checkFull(int deck[][5], int p);
  16. void printScores(int d[][5]);
  17. int checkEmpty(int p, int d[][5]);
  18. int checkGameOver(int d[][5]);
  19. void determineWinner(int d[][5]);
  20.  
  21. // Deck[x][y] where
  22. // x = Rank (2-10,J=11,Q=12,K=13,A=14)
  23. // y = Suit (0=Status,1=Club,2=Diamond,3=Heart,4=Spade)
  24.  
  25.  
  26. int main(void)
  27. {
  28. int deck[15][5] = { { 0 },{ 0 } };
  29. //int score[15][5]; // Use [0][x] as Deck Score
  30. int p, x;
  31. //int r=0,s=0;
  32. int chkfor;
  33. int chkplayer;
  34. int valinp;
  35. int newcard;
  36. int gameOver = 1;
  37. char rank;
  38. // Seed the random number generator
  39. srand(time(NULL));
  40.  
  41. printf("Welcome to Go Fish\n");
  42. printf("Dealing cards...\n");
  43. for (x = 1; x <= 5; x++) {
  44. for (p = 1; p <= 3; p++) {
  45. //printf("GoFish:P=%d\n",p);
  46. goFish(deck, p);
  47. //printDeck(deck);
  48. }
  49. }
  50.  
  51. // Manually assign some cards
  52.  
  53.  
  54. //Print the results of the deal (debug)
  55. //printDeck(deck);
  56.  
  57. // Take turns asking
  58. p = 1; // Player 1
  59. while (gameOver) { // while always true, need to fix that
  60. valinp = 0; // Get some valid input
  61. while (!valinp) {
  62. // Make sure the player has a card to select first
  63. if (checkEmpty(p, deck)) {
  64. // If P's hand is empty, we need another card...
  65. printf("You have no cards! Go Fish!\n");
  66.  
  67. if (goFish(deck, p) == -1) { // No more cards in the deck
  68. printf("Player %d is Out\n", p);
  69. // Move on to the next player
  70. if (p < 3) {
  71. p++;
  72. }
  73. else {
  74. p = 1;
  75. }
  76. printf("New Player = %d\n", p);
  77. }
  78. }
  79. else { // Continue with current player
  80. //printDeck(deck);
  81. printf("Player %d, please select your card:\n", p);
  82. printf("P%d = ",p);
  83. printHand(p, deck);
  84. /*printf("P2 = ");
  85. printHand(2, deck);
  86. printf("P3 = ");
  87. printHand(3, deck);*/
  88.  
  89. scanf("%s", &rank);
  90. char str2[];
  91. str2="j";
  92. int result;
  93.  
  94. result = strcmp(rank, str2)==0;
  95.  
  96. if (result <0 && result >0){
  97. chkfor=11;}
  98.  
  99. if (chkfor == 0) { //debug
  100. printScores(deck);
  101. }
  102.  
  103. if (checkInput(chkfor, p, deck)) {
  104. valinp = 1;
  105. // Select a player to ask
  106. int notSelf;
  107. do {
  108. notSelf = 0;
  109. printf("Player %d, who do you want to ask?\n", p);
  110. //Debug:
  111. /*printf("P1 = ");
  112. printHand(1, deck);
  113. printf("P2 = ");
  114. printHand(2, deck);
  115. printf("P3 = ");
  116. printHand(3, deck);
  117. printf("\n");*/
  118. scanf("%d", &chkplayer);
  119. if (chkplayer == p) {
  120. printf("You can't pick yourself!\n");
  121. notSelf = 1;
  122. }
  123. else if ((chkplayer >3 || chkplayer <1)){
  124. printf("Choose valid player\n");
  125. notSelf = 1;
  126. }
  127. } while (notSelf);
  128. //check for yourself
  129. if (checkPlayer(chkfor, p, chkplayer, deck)) {
  130. // Go Again
  131. }
  132. else {
  133. // Go Fish
  134. printf("No Match. Go fish.\n");
  135. newcard = goFish(deck, p);
  136. printf("Hoped for %d, Fished %d\n", chkfor, newcard);
  137. if (newcard == chkfor) {
  138. // if gofish=1, go again
  139. printf("Got what we asked for! Go Again\n");
  140. }
  141. else {
  142. //printf("Else Move on\n");
  143. // Move on to the next player
  144. if (p < 3) {
  145. p++;
  146. }
  147. else {
  148. p = 1;
  149. }
  150. printf("Enter ""c"" to clear screen for next player\n"); //allows player to clear the screen
  151. while ( getchar() != 'c'); //so the next player doesnt see his hand
  152.  
  153. for (int i = 0; i < 100; i++) { //makes a bunch of newlines to "clear" the screen
  154. printf("\n");
  155.  
  156. }
  157. printf("Enter ""c"" to continue to next player's turn\n");
  158. while ( getchar() != 'c');
  159. for (int i = 0; i < 100; i++) { //makes a bunch of newlines to "clear" the screen
  160. printf("\n");
  161.  
  162. }
  163.  
  164. //printf("New Player = %d\n", p);
  165. //turn over
  166. }
  167.  
  168. }
  169. // Check to see if Player owns all 4, and remove from hand
  170. //printf("Check Full\n");
  171. checkFull(deck, p);
  172.  
  173. }
  174. else { // Input was not valid
  175. printf("Please select a card you have\n");
  176. valinp = 0;
  177. }
  178. } // switching players
  179.  
  180. }
  181. /*int rank, suit;
  182. int gameOver = 0;
  183. for (rank = 2; rank <= 14; rank++) {
  184. for (suit = 1; suit <= 4; suit++) {
  185. deck[rank][suit] = -1;
  186. }
  187. }*/
  188. gameOver = checkGameOver(deck);
  189. //This is where need to determine if the game is over
  190. //game over = all cards = -1
  191.  
  192. }
  193. return 0;
  194. }
  195.  
  196. int goFish(int d[][5], int player)
  197. {
  198. int rank, suit;
  199. int found = 0;
  200.  
  201. // Need to check if deck has any cards available before
  202. // doing a random seek
  203.  
  204.  
  205. for (rank = 2; rank <= 14; rank++) {
  206. for (suit = 1; suit <= 4; suit++) {
  207. if (d[rank][suit] == 0) {
  208. found = 1;
  209. }
  210. }
  211. }
  212.  
  213.  
  214. if (found) { // We found at least 1 card available
  215. found = 0; // now go look for a random one
  216.  
  217. while (!found) {
  218. rank = (2 + rand() % 13);
  219. suit = (1 + rand() % 4);
  220. //printf("Check %d,%d\n",rank,suit);
  221. if (d[rank][suit] == 0) {
  222. found = 1;
  223. d[rank][suit] = player;
  224. //printf("ASSIGN: P%d=%d,%d=%d\n",player,rank,suit,d[rank][suit]);
  225.  
  226. }
  227. }
  228. }
  229. else { // There are no cards to GoFish
  230. printf("The deck is empty.\n");
  231. return -1;
  232. }
  233. return rank;
  234. }
  235.  
  236. void printDeck(int d[][5])
  237. {
  238. int x, y;
  239.  
  240. printf("R 2 3 4 5 6 7 8 9 0 J Q K A\n");
  241. printf("-:--------------------------\n");
  242. for (x = 0; x <= 4; x++) {
  243. switch (x) {
  244. case 0: printf("W: "); break;
  245. case 1: printf("C: "); break;
  246. case 2: printf("D: "); break;
  247. case 3: printf("H: "); break;
  248. case 4: printf("S: "); break;
  249. }
  250.  
  251. for (y = 2; y <= 14; y++) {
  252. printf("%d ", d[y][x]);
  253. }
  254. printf("\n");
  255. }
  256. printf("\n");
  257. }
  258.  
  259. void printHand(int p, int d[][5])
  260. {
  261. // Prints out the hand for a specific player
  262.  
  263. int r, s;
  264. char c;
  265.  
  266. for (r = 2; r <= 14; r++) {
  267. for (s = 1; s <= 4; s++) {
  268. if (d[r][s] == p) {
  269. switch (s) {
  270. case 1: c = 'C'; break;
  271. case 2: c = 'D'; break;
  272. case 3: c = 'H'; break;
  273. case 4: c = 'S'; break;
  274. }
  275. printf("%d%c ", r, c);
  276. }
  277. }
  278.  
  279. }
  280. printf("\n");
  281. }
  282.  
  283. char ps(int suit)
  284. {
  285. // Return the suit associated with that value
  286. switch (suit) {
  287. case 1: return 'C'; break;
  288. case 2: return 'D'; break;
  289. case 3: return 'H'; break;
  290. case 4: return 'S'; break;
  291. }
  292. return ' ';
  293. }
  294.  
  295. int checkEmpty(int p, int deck[][5])
  296. {
  297. // Check to see if the player has any cards,
  298. // If the player's hand is empty, return 1 (true)
  299.  
  300. int r, s;
  301. //printf("CheckEmpty P=%d\n", p);
  302. for (r = 2; r <= 14; r++) {
  303. for (s = 1; s <= 4; s++) {
  304. if (deck[r][s] == p) {
  305. //printf("Return 0");
  306. return 0;
  307. }
  308. }
  309. }
  310. // Nothing hit, we must be empty
  311. //printf("Return 1");
  312. return 1;
  313. }
  314.  
  315.  
  316. int checkInput(int chkfor, int p, int d[][5]) //checks if you have card
  317. {
  318. int s;
  319.  
  320. for (s = 1; s <= 4; s++) {
  321. if (d[chkfor][s] == p) {
  322. printf("Found\n");
  323. return 1;
  324. }
  325. }
  326. return 0;
  327. }
  328.  
  329. int checkPlayer(int chkfor, int src, int tgt, int deck[][5]) { //checkPlayer determines if the target player has the card the source player is looking for.
  330. int suit;
  331. int found;
  332.  
  333. found = 0;
  334. for (suit = 1; suit <= 4; suit++) {
  335. if (deck[chkfor][suit] == tgt) {
  336. found = 1;
  337. printf("Found %d%c\n",chkfor,ps(suit));
  338. deck[chkfor][suit] = src;
  339. }
  340. }
  341. if (found)
  342. return 1;
  343. else
  344. return 0;
  345.  
  346. }
  347.  
  348. void checkFull(int deck[][5], int p) //checks if player has all of specific rank
  349. {
  350. int r, s;
  351. int rankctr;
  352.  
  353. for (r = 2; r <= 14; r++) {
  354. rankctr = 0;
  355. for (s = 1; s <= 4; s++) {
  356. if (deck[r][s] == p) {
  357. rankctr++;
  358. }
  359. }
  360. if (rankctr == 4) {
  361. // We have all 4 cards.
  362. // Set "owner" bit to player
  363. printf("Player %d has all the %d's\n", p, r);
  364. deck[r][0] = p;
  365.  
  366. // Remove 4 cards from player's hand
  367. for (s = 1; s <= 4; s++) {
  368. deck[r][s] = -1;
  369. }
  370. }
  371. }
  372. }
  373.  
  374. void printScores(int d[][5])
  375. {
  376. int y;
  377.  
  378. printf("Scoreboard\n");
  379. printf("2 3 4 5 6 7 8 9 0 J Q K A\n");
  380. printf("--------------------------\n");
  381.  
  382. for (y = 2; y <= 14; y++) {
  383. printf("%d ", d[y][0]);
  384. }
  385. printf("\n");
  386.  
  387. printf("\n");
  388. }
  389.  
  390. int checkGameOver(int d[][5]) {
  391. int rank, suit;
  392. int gameOver = 0;
  393.  
  394. for (rank = 2; rank <= 14; rank++) {
  395. for (suit = 1; suit <= 4; suit++) {
  396. if (d[rank][suit] != -1) {
  397. gameOver = 1;
  398. }
  399. }
  400. }
  401. return gameOver;
  402. }
  403.  
  404.  
  405.  
  406. void determineWinner(int d[][5]) {
  407. int p1Score = 0, p2Score = 0, p3Score = 0;
  408. int winner;
  409. //Tallies score
  410. for (int i = 2; i < 14; i++) {
  411. switch (d[i][0]) {
  412. case 1: p1Score++;
  413. break;
  414. case 2: p2Score++;
  415. break;
  416. case 3: p3Score++;
  417. break;
  418. }
  419. }
  420. //determines winner
  421. if ((p1Score > p2Score) && (p1Score > p3Score)) {
  422. winner = 1;
  423. }
  424. else if ((p2Score > p1Score) && (p2Score > p3Score)) {
  425. winner = 2;
  426. }
  427. else if ((p3Score > p1Score) && (p3Score > p2Score)) {
  428. winner = 3;
  429. } else if ((p1Score == p2Score) && (p1Score > p3Score)) {
  430. winner = 12;
  431. }
  432. else if ((p1Score == p3Score) && (p1Score > p2Score)) {
  433. winner = 13;
  434. }
  435. else if ((p2Score == p3Score) && (p2Score > p1Score)) {
  436. winner = 23;
  437. }
  438.  
  439.  
  440. printf("Player 1 Score: %d\n", p1Score);
  441. printf("Player 2 Score: %d\n", p2Score);
  442. printf("Player 3 Score: %d\n", p3Score);
  443. switch (winner) {
  444. case 1: printf("Player 1 wins!"); break;
  445. case 2: printf("Player 2 wins!"); break;
  446. case 3: printf("Player 3 wins!"); break;
  447. case 12: printf("Players 1 and 2 tie!"); break;
  448. case 13: printf("Players 1 and 3 tie!"); break;
  449. case 23: printf("Players 2 and 3 tie!"); break;
  450. }
  451.  
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement