-
Notifications
You must be signed in to change notification settings - Fork 13
/
Rock-Paper-Scissors.cxx
96 lines (94 loc) · 2.17 KB
/
Rock-Paper-Scissors.cxx
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int pickrandom(int n)
{
srand(time(NULL)); //srand takes seed as an input and is defined inside stdlib.h
return rand() % n;
}
//Create Rock, Paper & Scissors Game
// Player 1: rock
// Player 2 (computer): scissors -->player 1 gets 1 point
// rock vs scissors - rock wins
// paper vs scissors - scissors wins
// paper vs rock - paper wins
// Write a C program to allow user to play this game three times with computer. Log the scores of computer and the player. Display the name of the winner at the end
// Notes: You have to display name of the player during the game. Take users name as an input from the user.
int main()
{
int i, j, k, m = 0, n = 0;
char name[20];
char choise[3][9] = {{"rock"}, {"scissors"}, {"paper"}};
printf("\nEnter your name : ");
gets(name);
for (i = 0; i < 3; i++)
{
printf("\nEnter 0 for rock \nEnter 1 for scissors \nEnter 2 for paper\n");
scanf("%d", &j);
k = pickrandom(3);
if (k == 0 && j == 1)
{
printf("\nComputer : %s", choise[k]);
printf("\n%s : %s", name, choise[j]);
printf("\ncomputer won");
m++;
}
if (k == 0 && j == 2)
{
printf("\nComputer : %s", choise[k]);
printf("\n%s : %s", name, choise[j]);
printf("\nyou won");
n++;
}
if (k == 1 && j == 2)
{
printf("\nComputer : %s", choise[k]);
printf("\n%s : %s", name, choise[j]);
printf("\ncomputer won");
m++;
}
if (k == 1 && j == 0)
{
printf("\nComputer : %s", choise[k]);
printf("\n%s : %s", name, choise[j]);
printf("\nyou won");
n++;
}
if (k == 2 && j == 0)
{
printf("\nComputer : %s", choise[k]);
printf("\n%s : %s", name, choise[j]);
printf("\ncomputer won");
m++;
}
if (k == 2 && j == 1)
{
printf("\nComputer : %s", choise[k]);
printf("\n%s : %s", name, choise[j]);
printf("\nyou won");
n++;
}
if (k == j)
{
printf("\nComputer : %s", choise[k]);
printf("\n%s : %s", name, choise[j]);
printf("\nDraw");
}
}
printf("\nyor point : %d", n);
printf("\ncomputers point : %d", m);
if (m < n)
{
printf("\nYou won");
}
else if (n < m)
{
printf("\ncomputer won");
}
else
{
printf("\nDraw");
}
return 0;
}