diff options
| author | Fuwn <[email protected]> | 2023-05-19 23:55:51 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-05-19 23:55:51 -0700 |
| commit | a10c17162e98d17168b2602e1b226dfb47e9cdaa (patch) | |
| tree | e876565658f300ab95cdc7c6ae08d0c61a46eee1 | |
| parent | feat(week_5): homework 5 (diff) | |
| download | cst120-a10c17162e98d17168b2602e1b226dfb47e9cdaa.tar.xz cst120-a10c17162e98d17168b2602e1b226dfb47e9cdaa.zip | |
fmt(homework_5): clang-format
| -rw-r--r-- | cst120/week_5/homework_5.c | 108 |
1 files changed, 64 insertions, 44 deletions
diff --git a/cst120/week_5/homework_5.c b/cst120/week_5/homework_5.c index ba69aff..97b5aaa 100644 --- a/cst120/week_5/homework_5.c +++ b/cst120/week_5/homework_5.c @@ -8,10 +8,10 @@ #define _CRT_SECURE_NO_WARNINGS #endif */ -#include <stdio.h> /* printf, scanf */ -#include <stdlib.h> /* srand, rand, size_t */ #include <stdint.h> /* uint8_t */ -#include <time.h> /* time */ +#include <stdio.h> /* printf, scanf */ +#include <stdlib.h> /* srand, rand, size_t */ +#include <time.h> /* time */ /* An organisation to hold a card * @@ -22,25 +22,25 @@ struct Card { }; /* Initialise and shuffle a deck */ -void shuffle_deck(struct Card [52]); +void shuffle_deck(struct Card[52]); /* Deal a hand to two players from a deck */ -void deal_hands(struct Card [52], struct Card [3], struct Card [3]); +void deal_hands(struct Card[52], struct Card[3], struct Card[3]); /* Print a hand */ -void print_hand(struct Card [3]); +void print_hand(struct Card[3]); /* Obtain the worth of a hand */ -size_t hand_worth(struct Card [3]); +size_t hand_worth(struct Card[3]); /* Print hand A and hand B, along with the winner */ -void print_game_outcome(struct Card [3], struct Card [3]); +void print_game_outcome(struct Card[3], struct Card[3]); int main(void) { char user_go_again = 'n'; - struct Card deck[52] = { 0 }; - struct Card hand_a[3] = { 0 }; - struct Card hand_b[3] = { 0 }; + struct Card deck[52] = {0}; + struct Card hand_a[3] = {0}; + struct Card hand_b[3] = {0}; /* Initialise the global random seed; not very secure, but fast */ srand((unsigned int)time(NULL)); @@ -83,7 +83,8 @@ void shuffle_deck(struct Card deck[52]) { } } -void deal_hands(struct Card deck[52], struct Card hand_a[3], struct Card hand_b[3]) { +void deal_hands(struct Card deck[52], struct Card hand_a[3], + struct Card hand_b[3]) { uint8_t deck_index = 0; uint8_t hand_index = 0; @@ -109,20 +110,40 @@ void print_hand(struct Card hand[3]) { /* Fancy print card rank */ switch (rank) { - case 1: { printf(" Ace "); } break; - case 11: { printf(" Jack "); } break; - case 12: { printf(" Queen "); } break; - case 13: { printf(" King "); } break; - default: { printf(" %d ", rank); } break; + case 1: { + printf(" Ace "); + } break; + case 11: { + printf(" Jack "); + } break; + case 12: { + printf(" Queen "); + } break; + case 13: { + printf(" King "); + } break; + default: { + printf(" %d ", rank); + } break; } /* Fancy print card suit */ switch (hand[deck_index].suit) { - case 1: { printf("of Clubs\n"); } break; - case 2: { printf("of Diamonds\n"); } break; - case 3: { printf("of Hearts\n"); } break; - case 4: { printf("of Spades\n"); } break; - default: { printf("This outcome should not have occured ...\n"); } break; + case 1: { + printf("of Clubs\n"); + } break; + case 2: { + printf("of Diamonds\n"); + } break; + case 3: { + printf("of Hearts\n"); + } break; + case 4: { + printf("of Spades\n"); + } break; + default: { + printf("This outcome should not have occured ...\n"); + } break; } } } @@ -132,26 +153,25 @@ size_t hand_worth(struct Card hand[3]) { } void print_game_outcome(struct Card hand_a[3], struct Card hand_b[3]) { - size_t hand_a_worth = hand_worth(hand_a); - size_t hand_b_worth = hand_worth(hand_b); - - /* Print hands A and B */ - printf("Player A's Hand:\n"); - print_hand(hand_a); - printf("\n Worth %lu\n", hand_a_worth); - printf("\nPlayer B's Hand:\n"); - print_hand(hand_b); - printf("\n Worth %lu\n\n", hand_b_worth); - - /* Print winner */ - if (hand_b_worth == hand_a_worth) { - printf("Player B wins from a tie!\n"); - } else if (hand_b_worth > hand_a_worth) { - printf("Player B wins!\n"); - } else if (hand_b_worth < hand_a_worth) { - printf("Player A wins!\n"); - } else { - printf("This outcome should not have occured ...\n"); - } + size_t hand_a_worth = hand_worth(hand_a); + size_t hand_b_worth = hand_worth(hand_b); + + /* Print hands A and B */ + printf("Player A's Hand:\n"); + print_hand(hand_a); + printf("\n Worth %lu\n", hand_a_worth); + printf("\nPlayer B's Hand:\n"); + print_hand(hand_b); + printf("\n Worth %lu\n\n", hand_b_worth); + + /* Print winner */ + if (hand_b_worth == hand_a_worth) { + printf("Player B wins from a tie!\n"); + } else if (hand_b_worth > hand_a_worth) { + printf("Player B wins!\n"); + } else if (hand_b_worth < hand_a_worth) { + printf("Player A wins!\n"); + } else { + printf("This outcome should not have occured ...\n"); + } } - |