For 3 points, your program must: • Create a user player and a computer competito
ID: 3626065 • Letter: F
Question
For 3 points, your program must:• Create a user player and a computer competitor.
• Ask the user for number of games. Number must be odd. If even, make number one more than what was entered. Print games needed to win.
o Example: Best of 5 means 3 wins are needed to win.
• User selects a weapon as a string.
• After the user selects a weapon, the computer draws an object at random.
• Chooses a winner based on standard RPS rules.
• Keep running tallies of wins, losses and ties for each player, as well as how many times each object is chosen by either player.
• Ties should not count as a game.
o Example: In a best of 5, it is possible to end with 3 wins, 2 losses, and 3 ties.
• Choose an overall winner. Display the tally of objects chosen.
• Your program must use functions to do all of this.
Expected output [input in bold]
Enter the number of matches.
5
Player 1, choose an object.
Scissors
Player 2 has selected Rock.
Rock breaks Scissors.
You win!
Rock chosen 1 time(s) Paper chosen 0 time(s) Scissors chosen 1 time(s) (Repeat after every match, must be running tally.)
(Do for best of 3, as above).
Player 1 wins the match.
Player 1 – 2 win(s), 1 loss(es), 1 tie(s).
Player 2 – 1 win(s), 2 loss(es), 1 tie(s).
Player 1 is the RPS Champion. Cue Queen music.
Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int computer();
int user();
void printtally(int,int,int,int[]);
void score(int ,int,int*,int*,int*);
int main()
{int games;
char ch;
int c=0,p=0,t=0,tally[3]={0,0,0},i;
printf("enter number of games to play: ");
scanf("%d",&games);
if(games%2==0)
{printf("must be odd ");
printf("we'll play %d games ",++games);
}
while ((ch = getchar()) != ' ' );
srand(time(0));
int usermove;
int computermove;
for(i=0;i<games;i++)
{ usermove=user();
computermove=computer();
tally[usermove]++;
tally[computermove]++;
score(usermove,computermove,&c,&p,&t);
printtally(c,p,t,tally);
}
if(c>p)
printf("Player 1 is the RPS Champion. Cue Queen music. ");
else if(p>c)
printf("Player 2 is the RPS Champion. Cue Queen music. ");
else
printf("It's a tie for the RPS Champion. Cue Queen music. ");
getch();
return 0;
}
void printtally(int c,int p,int t,int tally[])
{
printf("Player 1 - %d win(s), %d loss(es), %d tie(s). ",c,p,t);
printf("Player 2 - %d win(s), %d loss(es), %d tie(s). ",p,c,t);
printf("Rock chosen %d time(s) Paper chosen %d time(s) Scissors chosen %d time(s) ",
tally[0],tally[1],tally[2]);
}
int user()
{char a,ch;
int move;
printf("Choose your weapon (rock,paper,scissor): ");
a=getchar();
if(a>='A'&&a<='Z')
a=tolower(a);
while ((ch = getchar()) != ' ' );
while(a!='r'&&a!='p'&&a!='s')
{printf("*%c* ",a);
printf("invalid entry Choose your weapon ((rock,paper,scissor)): ");
scanf("%c",&a);
if(a>='A'&&a<='Z')
a=tolower(a);
a=tolower(a);
}
switch(a)
{case 'r': move=0;
break;
case 'p': move=1;
break;
case 's': move=2;
break;
}
return move;
}
int computer()
{char moves[3][9]={"rock","paper","scissors"};
int move=rand()%3;
printf("Player 2 has selected %s ",moves[move]);
return move ;
}
void score(int user,int computer,int *c,int *p,int *t )
{
if(user==computer)
{printf("Tie! ");
*t=*t+1;
}
else if(user==0&&computer==2)
{ printf("You break my sissors. you win! ");
*p=*p+1;
}
else if(user==0&&computer==1)
{printf("I cover your rock. you lose! ");
*c=*c+1;
}
else if(user==1&&computer==2)
{printf("I cut your paper. you lose! ");
*c=*c+1;
}
else if(user==1&&computer==0)
{printf("You cover my rock. you win! ");
*p=*p+1;
}
else if(user==2&&computer==0)
{printf("I break your sissors, you lose! ");
*c=*c+1;
}
else
{printf("You cut my paper. you win! ");
*p=*p+1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.