|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Rock–Paper–Scissors (Player vs Computer) |
| 4 | + * @details |
| 5 | + * Matches Player vs Computer in best-of-N rounds. |
| 6 | + * Uses same clean structure as Hangman template. |
| 7 | + * ASCII icons included for ROCK, PAPER, SCISSORS. |
| 8 | + * Author: [Nexora](https://github.com/nxora) |
| 9 | + */ |
| 10 | + |
| 11 | +#include <stdio.h> |
| 12 | +#include <stdlib.h> |
| 13 | +#include <time.h> |
| 14 | + |
| 15 | +/* --- DATA STRUCTURE — HOLDS MATCH RESULTS --- */ |
| 16 | +struct game_instance { |
| 17 | + int player_score; |
| 18 | + int computer_score; |
| 19 | + int rounds; |
| 20 | +}; |
| 21 | + |
| 22 | +/* --- FUNCTION PROTOTYPES --- */ |
| 23 | +struct game_instance new_game(void); |
| 24 | +int play_round(void); |
| 25 | +const char* move_name(int); |
| 26 | +void picture(int, int); |
| 27 | +void print_round_result(int); |
| 28 | + |
| 29 | +/* ---------------- MAIN ---------------- */ |
| 30 | +int main() { |
| 31 | + |
| 32 | + char again = 'y'; |
| 33 | + |
| 34 | + while (again == 'y' || again == 'Y') { |
| 35 | + |
| 36 | + struct game_instance game = new_game(); |
| 37 | + |
| 38 | + printf("\n====== ROCK — PAPER — SCISSORS ======\n"); |
| 39 | + |
| 40 | + for (int i = 1; i <= game.rounds; i++) { |
| 41 | + |
| 42 | + printf("\n---- Round %d ----\n", i); |
| 43 | + |
| 44 | + int result = play_round(); |
| 45 | + print_round_result(result); |
| 46 | + |
| 47 | + if (result == 1) game.player_score++; |
| 48 | + else if (result == -1) game.computer_score++; |
| 49 | + |
| 50 | + printf("Score → You: %d | Computer: %d\n", |
| 51 | + game.player_score, game.computer_score); |
| 52 | + } |
| 53 | + |
| 54 | + printf("\n===== MATCH RESULTS =====\n"); |
| 55 | + |
| 56 | + if (game.player_score > game.computer_score) |
| 57 | + printf("🎉 You win the match!\n"); |
| 58 | + else if (game.player_score < game.computer_score) |
| 59 | + printf("💀 Computer wins the match.\n"); |
| 60 | + else |
| 61 | + printf("🤝 It's a draw!\n"); |
| 62 | + |
| 63 | + printf("\nPlay again? (y/n): "); |
| 64 | + scanf(" %c", &again); |
| 65 | + } |
| 66 | + |
| 67 | + printf("\nThanks for playing!\n"); |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | +/* ---------------- CREATE NEW GAME ---------------- */ |
| 72 | +struct game_instance new_game(void) { |
| 73 | + |
| 74 | + struct game_instance game; |
| 75 | + |
| 76 | + printf("\nHow many rounds do you want to play? "); |
| 77 | + scanf("%d", &game.rounds); |
| 78 | + |
| 79 | + game.player_score = 0; |
| 80 | + game.computer_score = 0; |
| 81 | + |
| 82 | + srand(time(NULL)); // seed RNG |
| 83 | + |
| 84 | + return game; |
| 85 | +} |
| 86 | + |
| 87 | +/* ---------------- PLAY SINGLE ROUND ---------------- */ |
| 88 | +int play_round(void) { |
| 89 | + |
| 90 | + int player, comp; |
| 91 | + |
| 92 | + printf("Choose your move:\n"); |
| 93 | + printf("[0] Rock\n[1] Paper\n[2] Scissors\n"); |
| 94 | + printf("Enter choice: "); |
| 95 | + scanf("%d", &player); |
| 96 | + |
| 97 | + while (player < 0 || player > 2) { |
| 98 | + printf("Invalid choice. Enter 0-2: "); |
| 99 | + scanf("%d", &player); |
| 100 | + } |
| 101 | + |
| 102 | + comp = rand() % 3; |
| 103 | + |
| 104 | + printf("\nYou chose: %s\n", move_name(player)); |
| 105 | + printf("Computer chose: %s\n", move_name(comp)); |
| 106 | + |
| 107 | + picture(player, comp); |
| 108 | + |
| 109 | + /* winner logic */ |
| 110 | + if (player == comp) return 0; |
| 111 | + if ((player == 0 && comp == 2) || |
| 112 | + (player == 1 && comp == 0) || |
| 113 | + (player == 2 && comp == 1)) |
| 114 | + return 1; |
| 115 | + |
| 116 | + return -1; |
| 117 | +} |
| 118 | + |
| 119 | +/* ---------------- NAME OF MOVE ---------------- */ |
| 120 | +const char* move_name(int m) { |
| 121 | + switch (m) { |
| 122 | + case 0: return "Rock"; |
| 123 | + case 1: return "Paper"; |
| 124 | + default: return "Scissors"; |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/* ---------------- ASCII ART ---------------- */ |
| 129 | +void picture(int p, int c) { |
| 130 | + |
| 131 | + const char* rock = |
| 132 | + " _______\n" |
| 133 | + "---' ____)\n" |
| 134 | + " (_____)\n" |
| 135 | + " (_____)\n" |
| 136 | + " (____)\n" |
| 137 | + "---.__(___)\n"; |
| 138 | + |
| 139 | + const char* paper = |
| 140 | + " _______\n" |
| 141 | + "---' ____)____\n" |
| 142 | + " ______)\n" |
| 143 | + " _______)\n" |
| 144 | + " _______)\n" |
| 145 | + "---.__________)\n"; |
| 146 | + |
| 147 | + const char* scissors = |
| 148 | + " _______\n" |
| 149 | + "---' ____)____\n" |
| 150 | + " ______)\n" |
| 151 | + " __________)\n" |
| 152 | + " (____)\n" |
| 153 | + "---.__(___)\n"; |
| 154 | + |
| 155 | + printf("\nYour move:\n%s", (p == 0 ? rock : p == 1 ? paper : scissors)); |
| 156 | + |
| 157 | + printf("\nComputer move:\n%s", (c == 0 ? rock : c == 1 ? paper : scissors)); |
| 158 | +} |
| 159 | + |
| 160 | +/* ---------------- PRINT ROUND WINNER ---------------- */ |
| 161 | +void print_round_result(int r) { |
| 162 | + if (r == 1) printf("🟢 You win this round!\n"); |
| 163 | + else if (r == -1) printf("🔴 Computer wins this round!\n"); |
| 164 | + else printf("🟡 It's a draw!\n"); |
| 165 | +} |
0 commit comments