Name this program one.c - This program plays the classic childhood game of \"roc
ID: 3887278 • Letter: N
Question
Name this program one.c - This program plays the classic childhood game of "rock-paper-scissors." Your program reads two moves and then prints the winner. You store each move in a string of length ten (all input is lower-case and the user will never enter invalid input). Read the two moves and print the winner of the game. Each time you run the program, it plays the game once. You should print "Tie" (no winner) if both users enter the Scissors same string. The rules of the game are shown at the right. 1. Player 1 Player 2 Winner rock paper P2 rock scissors P1 P1 paper scissors P2 P2 scissors paper P1 rock rock rocke orushes scissors, paper covers rock, scissors eut paper A few sample executions are shown below: /a.out Player 1 move: rock Player 2 move: paper Winner is Player 2 /a.out Player 1 move: scissors Player 2 move: paper Winner is Player 1 ./a.out Player 1 move:scissors Player 2 move: scissors Game is a tieExplanation / Answer
#include<stdio.h>
int match_rock(char a[])//matching with rock
{
int l=0;
while(a[l]!='')l++;//finding length
char b[] ="rock";
if(l==4)
{
l=0;
while(a[l]!='')
{
if(a[l]==b[l])l++;
else return 0;
}
return 1;
}
return 0;
}
int match_scissors(char a[])//matching with scissors
{
int l=0;
while(a[l]!='')l++;//finding length
char b[] ="scissors";
if(l==8)
{
l=0;
while(a[l]!='')
{
if(a[l]==b[l])l++;
else return 0;
}
return 1;
}
return 0;
}
int match_paper(char a[])
{
int l=0;
while(a[l]!='')l++;//finding length
char b[] ="paper";
if(l==5)
{
l=0;
while(a[l]!='')
{
if(a[l]==b[l])l++;
else return 0;
}
return 1;
}
return 0;
}
int main()
{
//variable declarations
char p1[10],p2[10];
//reading input
printf("Player 1 move:");
scanf("%s",p1);
printf("Player 2 move:");
scanf("%s",p2);
//checking and printing output..
if(match_rock(p1)==1)
{
if(match_paper(p2)==1)
{
printf("Winner is Player2 ");
}
else if(match_scissors(p2)==1)
{
printf("Winner is Player1 ");
}
else
{
printf("Game is tie ");
}
}
else if(match_paper(p1)==1)
{
if(match_rock(p2)==1)
{
printf("Winner is Player1 ");
}
else if(match_scissors(p2)==1)
{
printf("Winner is Player2 ");
}
else
{
printf("Game is tie ");
}
}
else if(match_scissors(p1)==1)
{
if(match_rock(p2)==1)
{
printf("Winner is Player2 ");
}
else if(match_paper(p2)==1)
{
printf("Winner is Player1 ");
}
else
{
printf("Game is tie ");
}
}
return 0;
}
output
Player 1 move:paper
Player 2 move:rock
Winner is Player1
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.