Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- static char cards_color[4] = { 'K', 'C', 'P', 'T' };
- static int cards[13][2] =
- {
- { 1, 'A' },
- { 2, '2' },
- { 3, '3' },
- { 4, '4' },
- { 5, '5' },
- { 6, '6' },
- { 7, '7' },
- { 8, '8' },
- { 9, '9' },
- { 10, 'D' },
- { 11, 'J' },
- { 12, 'Q' },
- { 13, 'K' }
- };
- void done (int game[5][3])
- {
- int i, j;
- int card, color;
- for (i = 0; i < 5; ++i)
- {
- if (game[i][2] == 0)
- {
- int ok = 1;
- /* choose a card randomly */
- card = cards[rand() % 13][0];
- color = cards_color[rand() % 4];
- /* check if the card is in the game */
- for (j = 0; j < i; ++j)
- {
- if (game[j][0] != card || game[j][1] != color)
- {
- ok = 0;
- break;
- }
- }
- if (!ok || i == 0)
- {
- game[i][0] = card;
- game[i][1] = color;
- }
- else
- --i; /* if the card is already in the game, decrease i to choose again */
- }
- }
- }
- void check (int game[5][3])
- {
- int tab[13] = { 0 };
- int have_color = 1;
- int old_color;
- int i;
- old_color = game[0][1];
- for (i = 0; i < 5; ++i)
- {
- tab[game[i][0]] += 1; /* set number of same cards */
- if (old_color != game[i][1]) /* check if we have the same colors */
- have_color = 0;
- old_color = game[i][1];
- }
- /* if have_color = true, every card are K, or C, or T, or P */
- /* tab contains the number of cards in the game (for exemple : tab[2] = 2 means that we have 2 cards '2') */
- for (i = 0; i < 13; ++i)
- {
- if (tab[i] > 0)
- {
- /* TODO: push (stack, tab[1]) */
- }
- }
- }
- void print_game (int game[5][3])
- {
- int i;
- printf ("Here is your game :\n");
- for (i = 0; i < 5; ++i)
- printf ("Card %d: %d (%c)\n", i, game[i][0], game[i][1]);
- }
- int main (void)
- {
- int no_exit = 0;
- int game[5][3]; /* game[][0] = card, game[][1] = color, game[][2] = keep the card */
- int i, j;
- srand (time (NULL));
- do
- {
- for (i = 0; i < 5; ++i)
- for (j = 0; j < 3; ++j)
- game[i][j] = 0;
- done (game);
- print_game (game);
- for (i = 0; i < 5; ++i)
- {
- printf ("Keep card %d (no:0/yes:1) ? ", i);
- scanf ("%d", &game[i][2]);
- }
- done (game);
- print_game (game);
- printf ("Play again (no:0/yes:1) ? ");
- scanf ("%d", &no_exit);
- } while (no_exit);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement