-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrock_paper_scissors.c
67 lines (55 loc) · 1.87 KB
/
rock_paper_scissors.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function to get the computer's choice as a string
const char* getChoiceString(int choice) {
switch (choice) {
case 0: return "Rock";
case 1: return "Paper";
case 2: return "Scissors";
default: return "Unknown";
}
}
// Function to get the computer's choice
int getComputerChoice() {
return rand() % 3; // 0 = Rock, 1 = Paper, 2 = Scissors
}
// Function to determine the winner
void determineWinner(int playerChoice, int computerChoice) {
printf("\nYou chose: %s\n", getChoiceString(playerChoice));
printf("Computer chose: %s\n", getChoiceString(computerChoice));
if (playerChoice == computerChoice) {
printf("\nIt's a tie!\n");
} else if ((playerChoice == 0 && computerChoice == 2) ||
(playerChoice == 1 && computerChoice == 0) ||
(playerChoice == 2 && computerChoice == 1)) {
printf("\nCongratulations! You win!\n");
} else {
printf("\nSorry, Computer wins this time!\n");
}
}
int main() {
int playerChoice;
int computerChoice;
// Seed the random number generator
srand(time(NULL));
printf("=========================================\n");
printf(" Rock, Paper, Scissors\n");
printf("=========================================\n");
printf("Enter your choice:\n");
printf(" 0 for Rock\n");
printf(" 1 for Paper\n");
printf(" 2 for Scissors\n");
printf("=========================================\n");
// Get player input
printf("Your choice: ");
scanf("%d", &playerChoice);
// Validate input
if (playerChoice < 0 || playerChoice > 2) {
printf("\nInvalid choice. Please enter 0, 1, or 2.\n");
return 1;
}
computerChoice = getComputerChoice();
determineWinner(playerChoice, computerChoice);
return 0;
}