-
Notifications
You must be signed in to change notification settings - Fork 0
/
rock-paper-scissor.c
78 lines (65 loc) · 1.98 KB
/
rock-paper-scissor.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
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROCK 1
#define PAPER 2
#define SCISSORS 3
int main() {
int playerScore = 0;
int computerScore = 0;
int rounds = 0;
printf("Welcome to Rock-Paper-Scissors game!\n\n");
while(1) {
int playerMove;
printf("Enter your move (1 = Rock, 2 = Paper, 3 = Scissors, 0 = Quit): ");
scanf("%d", &playerMove);
if (playerMove == 0) {
break;
}
if (playerMove < 1 || playerMove > 3) {
printf("Invalid move! Please try again.\n");
continue;
}
int computerMove = rand() % 3 + 1;
printf("You played: ");
switch(playerMove) {
case ROCK:
printf("Rock\n");
break;
case PAPER:
printf("Paper\n");
break;
case SCISSORS:
printf("Scissors\n");
break;
}
printf("Computer played: ");
switch(computerMove) {
case ROCK:
printf("Rock\n");
break;
case PAPER:
printf("Paper\n");
break;
case SCISSORS:
printf("Scissors\n");
break;
}
if (playerMove == computerMove) {
printf("It's a tie!\n");
} else if (playerMove == ROCK && computerMove == SCISSORS ||
playerMove == PAPER && computerMove == ROCK ||
playerMove == SCISSORS && computerMove == PAPER) {
printf("You win this round!\n");
playerScore++;
} else {
printf("Computer wins this round!\n");
computerScore++;
}
rounds++;
printf("Current score: You %d - %d Computer\n\n", playerScore, computerScore);
}
printf("\nFinal score: You %d - %d Computer\n", playerScore, computerScore);
printf("Total rounds played: %d\n", rounds);
return 0;
}