summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-05-19 19:08:24 -0700
committerFuwn <[email protected]>2023-05-19 19:08:24 -0700
commit8385f09ce8ccefe8296c08542facc5bb3586994a (patch)
tree371016a2ab41c5dda49fd8ae60e4b7b2dc99e511
parentfeat(week_5): quiz 5 (diff)
downloadcst120-8385f09ce8ccefe8296c08542facc5bb3586994a.tar.xz
cst120-8385f09ce8ccefe8296c08542facc5bb3586994a.zip
feat(week_5): homework 5
-rw-r--r--cst120/week_5/BUILD1
-rw-r--r--cst120/week_5/homework_5.c157
2 files changed, 158 insertions, 0 deletions
diff --git a/cst120/week_5/BUILD b/cst120/week_5/BUILD
index 6d2f536..12b0271 100644
--- a/cst120/week_5/BUILD
+++ b/cst120/week_5/BUILD
@@ -1,3 +1,4 @@
load("//:rules.bzl", "assignment")
assignment("quiz_5")
+assignment("homework_5")
diff --git a/cst120/week_5/homework_5.c b/cst120/week_5/homework_5.c
new file mode 100644
index 0000000..ba69aff
--- /dev/null
+++ b/cst120/week_5/homework_5.c
@@ -0,0 +1,157 @@
+/* Author: Zoltan Szabatin
+ * Professor: Professor Phong N.
+ * Project: Homework 5
+ * Course: CST 120 */
+
+/* Disable security warnings when using MSVC */
+/* #ifdef _MSC_VER
+#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 */
+
+/* An organisation to hold a card
+ *
+ * The suit isn't necessary for evaluation, other than to look good */
+struct Card {
+ uint8_t suit;
+ uint8_t rank;
+};
+
+/* Initialise and shuffle a deck */
+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]);
+
+/* Print a hand */
+void print_hand(struct Card [3]);
+
+/* Obtain the worth of a hand */
+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]);
+
+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 };
+
+ /* Initialise the global random seed; not very secure, but fast */
+ srand((unsigned int)time(NULL));
+
+ do {
+ /* Initialise and shuffle the deck */
+ shuffle_deck(deck);
+
+ /* Deal hands to player A and B */
+ deal_hands(deck, hand_a, hand_b);
+
+ /* Print player A and player B's hands and outcome */
+ print_game_outcome(hand_a, hand_b);
+
+ /* Prompt user to go again */
+ printf("\nWould you like to go again? (y/n) ");
+ scanf(" %c", &user_go_again);
+ printf("\n");
+ } while (user_go_again == 'y' || user_go_again == 'Y');
+
+ return 0;
+}
+
+void shuffle_deck(struct Card deck[52]) {
+ size_t deck_index = 0;
+
+ /* Initialise the deck, unshuffled */
+ for (deck_index = 0; deck_index < 52; deck_index += 1) {
+ deck[deck_index].rank = deck_index % 13 + 1;
+ deck[deck_index].suit = (uint8_t)(deck_index / 13 + 1);
+ }
+
+ /* Shuffle the deck */
+ for (deck_index = 0; deck_index < 52; deck_index += 1) {
+ size_t new_index = (size_t)(rand() % 52);
+ struct Card current_card = deck[deck_index];
+
+ deck[deck_index] = deck[new_index];
+ deck[new_index] = current_card;
+ }
+}
+
+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;
+
+ /* Deal hand A */
+ for (hand_index = 0; hand_index <= 3; hand_index += 1) {
+ hand_a[hand_index] = deck[deck_index];
+ deck_index += 1;
+ }
+
+ /* Deal hand B */
+ for (hand_index = 0; hand_index <= 3; hand_index += 1) {
+ hand_b[hand_index] = deck[deck_index];
+ deck_index += 1;
+ }
+}
+
+void print_hand(struct Card hand[3]) {
+ uint8_t deck_index = 0;
+
+ /* Print a hand */
+ for (deck_index = 0; deck_index <= 3; deck_index += 1) {
+ uint8_t rank = hand[deck_index].rank;
+
+ /* 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;
+ }
+
+ /* 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;
+ }
+ }
+}
+
+size_t hand_worth(struct Card hand[3]) {
+ return (hand[0].rank + hand[1].rank + hand[2].rank) % 10;
+}
+
+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");
+ }
+}
+