In C language only please not C++!!!!! sample output is given and so is an outli
ID: 3774566 • Letter: I
Question
In C language only please not C++!!!!! sample output is given and so is an outline of program.
Write an interactive program to play the game of "Rock Paper Scissors" with the computer as your opponent. Your program will prompt the user for input, generate a random input for the opponent, calculate and display who won. The program will run in a loop continually until Esc is pressed.
Sample Output :
Welcome to Rock Paper Scissors
May the best sentient being win.
Enter R, P, or S. Press ESC to end the game.
Human Puter Winner
--------------------------
Rock Rock Draw
Paper Rock Human
Scissor Rock Puter
Press any key to continue . . .
Psuedocode:
// This is a mix of psuedocode and actual C code
#include "stdio.h"
#include "conio.h"
#include "stdlib.h" // rand is in here
#include "time.h" // contains time structure
#pragma warning (disable : 4996) // turn off stdio function warnings
time_t t; // a time object for rand seed
// seed random number generator
srand((unsigned)time(&t));
// MAIN PROCESSION LOOP
do {
// Get human's move
// character input validation loop
do {
get ch1
switch on ch1
{
case 'R'
case 'r'
case 'P'
case 'p'
case 'S'
case 's'
case 27
chOK = 1
break
default
chOK = 0
}
Page 2 of 7
} while chOK is true
// Convert input to uppercase
// this is actual C code, please tell me why this line works
if (ch1 > 'Z') ch1 -= ' ';
// Get computer's move
ch2 = rand() % 3; // C code to get a rand #
switch on ch2
{
case 0
ch2 = R
break
//etc...
}
// Check who won
if (ch1 and ch2 are both R)
print "Rock Rock Draw"
if (ch1 is an R and ch2 is a P)
print "Rock Paper Puter"
//etc... there are 9 total possible combinations
} while ch1 is not ESC
Explanation / Answer
#include <stdio.h>
#include "stdlib.h" // rand is in here
/*
int findIndex(char ch, char [] inputArray, int length){
int i ;
for (i = 0; i<length; i++){
if (inputArray[i] == ch) return i;
}
printf (" Given character is not found in array ::");
return -1;
}
*/
int getWinner(int player, int computer){
int winner = 0; // Default winner is user
// Assume 'rock'=0 'paper'=1 or 'scissor'=2'
if (player == 0 && computer == 0) {
printf ("' tie, rock - rock' ");
winner = -1; // Draw
}
else if (player == 1 && computer == 1) {
printf ("' tie, paper - paper' ");
winner = -1;
}
else if (player == 2 && computer == 2) {
printf ("' tie, scissors - scissors' ");
winner = -1;
}
else if (player==0 && computer==1) {
printf (" U loss, paper covers rock ");
winner = 1;
}
else if (player==0 && computer==2) {
printf (" U win, rock breaks scissors ");
}
else if (player==1 && computer==0) {
printf (" U win, paper covers rock ");
}
else if (player==1 && computer==2) {
printf (" U loss, scissors cut paper ");
winner = 1;
}
else if (player==2 && computer==0) {
printf (" U loss, rock breaks scissors ");
winner = 1;
}
else if (player==2 && computer==1) {
printf (" U win, scissors cut paper ");
}
return winner;
}
int main (void) {
int computer,winner = 0,userScore = 0,computerScore = 0,index;
char player ;
printf ("type in your choice of R,P,S To stop ESC ");
scanf(" %c", &player);
getchar();
while (player != 27){
printf(" player:: %c", player );
switch (player){
case 'R':
case 'r':
index = 0;
break;
case 'P':
case 'p':
index = 1;
break;
case 'S':
case 's':
index = 2;
break;
default:
printf (" Given input is invalid..!! Try again ");
index = -1;
}
if (index != -1){
computer = rand ()%3; // generates the computer's choice of rock, paper, or scissor randomly
//index = findIndex(player,inputArray,3);
winner = getWinner(index, computer);
if (!winner){
userScore += 1;
}else if (winner == 1){
computerScore += 1;
}
}
printf ("choice of R,P,S To stop ESC ");
scanf("%c", &player);
getchar();
}
if ( userScore > computerScore){
printf (" Congratulations ..!!! You won the game ");
}else if ( userScore < computerScore){
printf (" Sorry ..!!! Computer won the game ");
}else{
printf(" Draw...!!! Good attempt..!! Try again " );
}
return 0;
}