Advertisement
Guest User

jebane karty w dupe

a guest
Dec 6th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 KB | None | 0 0
  1. #include <time.h>
  2. #include <conio.h>
  3. #include <cstdlib>
  4.  
  5. #define DECK_SIZE 52
  6. //Stworzenie struktury card oraz tablicy zawierającej wszystkie karty - talii początkowej.
  7.  
  8. struct card {
  9. char suit[9];
  10. char rank[6];
  11. int value;
  12. };
  13. typedef struct card card;
  14. card empty {" ", " ", 0 };
  15.  
  16. card spades_2 { "Spades", "Two", 2 };
  17. card spades_3 { "Spades", "Three", 3 };
  18. card spades_4 { "Spades", "Four", 4 };
  19. card spades_5 { "Spades", "Five", 5 };
  20. card spades_6 { "Spades", "Six", 6 };
  21. card spades_7 { "Spades", "Seven", 7 };
  22. card spades_8 { "Spades", "Eight", 8 };
  23. card spades_9 { "Spades", "Nine", 9 };
  24. card spades_10 { "Spades", "Ten", 10 };
  25. card spades_jack { "Spades", "Jack", 11 };
  26. card spades_queen { "Spades", "Queen", 12 };
  27. card spades_king { "Spades", "King", 13 };
  28. card spades_ace { "Spades", "Ace", 14 };
  29.  
  30. card hearts_2 { "Hearts", "Two", 2 };
  31. card hearts_3 { "Hearts", "Three", 3 };
  32. card hearts_4 { "Hearts", "Four", 4 };
  33. card hearts_5 { "Hearts", "Five", 5 };
  34. card hearts_6 { "Hearts", "Six", 6 };
  35. card hearts_7 { "Hearts", "Seven", 7 };
  36. card hearts_8 { "Hearts", "Eight", 8 };
  37. card hearts_9 { "Hearts", "Nine", 9 };
  38. card hearts_10 { "Hearts", "Ten", 10 };
  39. card hearts_jack { "Hearts", "Jack", 11 };
  40. card hearts_queen { "Hearts", "Queen", 12 };
  41. card hearts_king { "Hearts", "King", 13 };
  42. card hearts_ace { "Hearts", "Ace", 14 };
  43.  
  44. card clubs_2 { "Clubs", "Two", 2 };
  45. card clubs_3 { "Clubs", "Three", 3 };
  46. card clubs_4 { "Clubs", "Four", 4 };
  47. card clubs_5 { "Clubs", "Five", 5 };
  48. card clubs_6 { "Clubs", "Six", 6 };
  49. card clubs_7 { "Clubs", "Seven", 7 };
  50. card clubs_8 { "Clubs", "Eight", 8 };
  51. card clubs_9 { "Clubs", "Nine", 9 };
  52. card clubs_10 { "Clubs", "Ten", 10 };
  53. card clubs_jack { "Clubs", "Jack", 11 };
  54. card clubs_queen { "Clubs", "Queen", 12 };
  55. card clubs_king { "Clubs", "King", 13 };
  56. card clubs_ace { "Clubs", "Ace", 14 };
  57.  
  58. card diamonds_2 { "Diamonds", "Two", 2 };
  59. card diamonds_3 { "Diamonds", "Three", 3 };
  60. card diamonds_4 { "Diamonds", "Four", 4 };
  61. card diamonds_5 { "Diamonds", "Five", 5 };
  62. card diamonds_6 { "Diamonds", "Six", 6 };
  63. card diamonds_7 { "Diamonds", "Seven", 7 };
  64. card diamonds_8 { "Diamonds", "Eight", 8 };
  65. card diamonds_9 { "Diamonds", "Nine", 9 };
  66. card diamonds_10 { "Diamonds", "Ten", 10 };
  67. card diamonds_jack { "Diamonds", "Jack", 11 };
  68. card diamonds_queen { "Diamonds", "Queen", 12 };
  69. card diamonds_king { "Diamonds", "King", 13 };
  70. card diamonds_ace { "Diamonds", "Ace", 14 };
  71. // koejnosc kart w talii trzeba zmodyfikować aby umożliwić dowolne wybieranie talii początkowej jak jest to opisane w wymaganiach
  72. const card deck[52] = { spades_2,spades_3,spades_4,spades_5,spades_6,spades_7,spades_8,
  73. spades_9, spades_10, spades_jack, spades_queen, spades_king, spades_ace,
  74. hearts_2, hearts_3, hearts_4, hearts_5, hearts_6, hearts_7, hearts_8,
  75. hearts_9, hearts_10, hearts_jack, hearts_queen, hearts_king, hearts_ace,
  76. clubs_2, clubs_3, clubs_4, clubs_5, clubs_6, clubs_7, clubs_8, clubs_9,
  77. clubs_10, clubs_jack, clubs_queen, clubs_king, clubs_ace,
  78. diamonds_2, diamonds_3, diamonds_4, diamonds_5, diamonds_6, diamonds_7,
  79. diamonds_8, diamonds_9, diamonds_10,diamonds_jack, diamonds_queen, diamonds_king, diamonds_ace };
  80.  
  81. //deklaracje funkcji
  82. void deal( card P1_hand[], card P2_hand[]); // funkcja losowo rozdaje karty
  83. void q_add( card hand[], card card); // funkcja dodaje kartę na koniec ręki
  84. card q_remove( card hand[]); // funkcja zwraca pierwszą kartę w ręce i ją usuwa
  85. int count_cards( card hand[]); // funkcja zlicza karty w ręce bądź stole
  86. void play( card P1_hand[], card P2_hand[]); // funkcja rozgrywa 1 turę gry
  87. int war( card P1_hand[], card P2_hand[], card card_1, card card_2); // funkcja zwraca nr gracza który wygrywa wojnę
  88. void clear_table( card table[], int i); // funkcja czyści stół po każdej wojnie
  89.  
  90. int main()
  91. {
  92. card P1_hand[DECK_SIZE] = {};
  93. card P2_hand[DECK_SIZE] = {};
  94.  
  95. int P1_cards_count=DECK_SIZE/2, P2_cards_count=DECK_SIZE/2;
  96. int* P1_count = &P1_cards_count;
  97. int* P2_count = &P2_cards_count;
  98.  
  99.  
  100. deal(P1_hand, P2_hand);
  101. _cprintf("Both players are ready, let's start the game!\n Press N in order to proceed in the game\n\n");
  102. _cprintf("P1 has %i cards, and P2 has %i cards\n", count_cards(P1_hand), count_cards(P2_hand));
  103. while (_getch()) {
  104. P1_cards_count = count_cards(P1_hand);
  105. P2_cards_count = count_cards(P2_hand);
  106. if (P1_cards_count == 0) _cprintf("Player 2 has won the game!");
  107. else if (P2_cards_count == 0) _cprintf("Player 1 has won the game!");
  108. else if (P1_cards_count > 0 && P2_cards_count > 0) {
  109. play(P1_hand, P2_hand);
  110. }
  111. }
  112.  
  113. }
  114.  
  115. //definicje funkcji
  116. void deal( card P1_hand[], card P2_hand[]) {
  117. int flag[DECK_SIZE] = { 0 };
  118. srand(time(NULL));
  119. int i = 0;
  120. while (i < DECK_SIZE/2) {
  121. int k = rand() % DECK_SIZE;
  122. if (flag[k] == 0) {
  123. P1_hand[i] = deck[k];
  124. flag[k] = 1;
  125. ++i;
  126. }
  127.  
  128. }
  129. int j = 0;
  130. while (j < DECK_SIZE / 2) {
  131. int k = rand() % DECK_SIZE;
  132. if (flag[k] == 0) {
  133. P2_hand[j] = deck[k];
  134. flag[k] = 1;
  135. ++j;
  136. }
  137.  
  138. }
  139. P1_hand[DECK_SIZE / 2 + 1] = empty;
  140. P2_hand[DECK_SIZE / 2 + 1] = empty;
  141. }
  142.  
  143. void q_add( card hand[], card card) {
  144. hand[count_cards(hand)] = card;
  145. }
  146.  
  147. card q_remove( card hand[]){
  148. card card = hand[0];
  149. for (int i = 0; i < count_cards(hand); ++i) {
  150. hand[i] = hand[i + 1];
  151. }
  152. return card;
  153. }
  154. int count_cards( card hand[]) {
  155. int count = 0;
  156. for (int i = 0; i < DECK_SIZE; ++i) {
  157. if (hand[i].value>=2 && hand[i].value<=14) count++;
  158. }
  159. return count;
  160. }
  161.  
  162. void play( card P1_hand[], card P2_hand[]) {
  163.  
  164. card card_1 = q_remove(P1_hand);
  165. card card_2 = q_remove(P2_hand);
  166.  
  167. _cprintf("%s of %s VS %s of %s\n", card_1.rank, card_1.suit, card_2.rank, card_2.suit);
  168.  
  169. if (card_1.value > card_2.value) {
  170. q_add(P1_hand, card_1);
  171. q_add(P1_hand, card_2);
  172. _cprintf("Player 1 wins this turn and takes both cards! He now has %i cards.\n\n", count_cards(P1_hand));
  173. _cprintf("P1 has %i cards, and P2 has %i cards\n", count_cards(P1_hand), count_cards(P2_hand));
  174. }
  175. else if (card_1.value < card_2.value) {
  176. q_add(P2_hand, card_2);
  177. q_add(P2_hand, card_1);
  178. _cprintf("Player 2 wins this turn and takes both cards! He now has %i cards\n\n", count_cards(P2_hand));
  179. _cprintf("P1 has %i cards, and P2 has %i cards\n", count_cards(P1_hand), count_cards(P2_hand));
  180. }
  181. else if (card_1.value == card_2.value) {
  182. war(P1_hand, P2_hand, card_1, card_2);
  183. _cprintf("\n");
  184. }
  185.  
  186.  
  187. }
  188.  
  189. int war( card P1_hand[], card P2_hand[], card card_1, card card_2) {
  190. _cprintf("\n War! those 2 card values are the same!\n");
  191.  
  192. int p1_count = count_cards(P1_hand);
  193. int p2_count = count_cards(P2_hand);
  194.  
  195. static card table[DECK_SIZE];
  196. q_add(table, card_1);
  197. q_add(table, card_2);
  198.  
  199. int winner = 0; // zmienna sprawdzająca kto jest zwycięzcą wojny
  200. while (winner == 0) {
  201.  
  202. q_add(table, q_remove(P1_hand));
  203. q_add(table, q_remove(P2_hand));
  204. _cprintf("Both players now put unknown cards on the table\n");
  205. card visible_1 = q_remove(P1_hand);
  206. card visible_2 = q_remove(P2_hand);
  207. _cprintf("Now they put another card on the table: \n%s of %s VS %s of %s\n", visible_1.rank, visible_1.suit, visible_2.rank, visible_2.suit);
  208. q_add(table, visible_1);
  209. q_add(table, visible_2);
  210.  
  211. int i = count_cards(table)-1; // i to ostatni indeks pod którym znajduje się karta w tablicy stołu
  212.  
  213. if (table[i].value < table[i-1].value) {
  214.  
  215. for (int j = 0; j <= i; ++j) {
  216. if (j % 2 == 0) q_add(P1_hand, table[j]);
  217. }
  218. for (int j = 0; j <= i; ++j) {
  219. if (j % 2 == 1) q_add(P1_hand, table[j]);
  220. }
  221. _cprintf("Player 1 wins the war and takes all these cards! He now has %i cards.\n", count_cards(P1_hand));
  222. _cprintf("P1 has %i cards, and P2 has %i cards\n", count_cards(P1_hand), count_cards(P2_hand));
  223. winner = 1;
  224. clear_table(table, i);
  225. return winner;
  226.  
  227. }
  228. else if (table[i].value > table[i-1].value) {
  229.  
  230. for (int j = 0; j <= i; ++j) {
  231. if (j % 2 == 1) {
  232. q_add(P2_hand, table[j]);
  233. }
  234. }
  235. for (int j = 0; j <= i; ++j) {
  236. if (j % 2 == 0) {
  237. q_add(P2_hand, table[j]);
  238. }
  239. }
  240. _cprintf("Player 2 wins the war and takes all these cards! He now has %i cards.\n", count_cards(P2_hand));
  241. _cprintf("P1 has %i cards, and P2 has %i cards\n", count_cards(P1_hand), count_cards(P2_hand));
  242. winner = 2;
  243. clear_table(table, i);
  244. return winner;
  245.  
  246. }
  247. else if (table[i + 1].value == table[i].value) {
  248. winner = war(P1_hand, P2_hand, table[i], table[i+1]);
  249.  
  250. }
  251.  
  252. }
  253. return winner;
  254. }
  255.  
  256. void clear_table( card table[], int i) {
  257. for (int j = 0; j <= i; ++j) {
  258. q_remove(table);
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement