Question

In: Computer Science

#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the...

#include <stdio.h>

#include <stdlib.h>

int play_game(int *); // Returns 0 if player won, 1 if the computer won, 2 if there is a tie, and -1 if the player decides to quit

int menu(int *); // Displays choices to user // Receives score array

int main() { srand(42); // Seeding Random with 42

int score[3]; // Array keeping Player, Computer, and Tie Scores

score [0] = 0; // Player - initialized to Zero

score [1] = 0; // Computer - initialized to Zero

score [2] = 0; // Tie - initialized to Zero

int winner=0; // Winner index of

printf("ROCK PAPER SCISSORS!\n\n");

while(winner >=0) // If Winner == -1 -> the game stops {

winner = play_game(score); // Plays the game

. if(winner >=0) // If a value to not stop the game comes in.

score[winner]++; // Increment score counter }

printf("GOOD BYE!\n");

return 0; }

please help with the two functions listed on top.

need to know

int menu(int *): Receives: integer array score Returns: Menu number for Rock, Paper, Scissors, or Exit. Returns -1 for any other number than applicable menu choice Grading: 1) Displays “ROCK PAPER SCISSORS!” at the top of the menu 2) Displays the current score between the player and the computer (including ties) 3) Displays choice numbers for Rock, Paper, Scissors, and Exit. 4) Asks for a choice – If improper choice is given (alpha-numeric and wrong number), asks for another choice 5) Console Display is organized and easily read 6) Code is organized and easily read (Comments where needed) HINT: Use of a switch statement makes the code a little easier to read.

int play_game(int *): Receives: integer array score Returns: an index of score to be incremented. If the player wins, returns 0. If the computer wins, returns 1. If there is a tie, returns 2. Grading: 1) The computer choice is randomly generated between 3 numbers using rand(). 2) Displays BOTH the User choice and the Computer Choice 3) Displays whether the result is a win, loss, or tie for the User. 4) Displays the following statements depending on what was played by the User and Computer  “ROCK CRUSHES SCISSORS!”  “SCISSORS CUT PAPER!”  “PAPER COVERS ROCK!”  “TIE – BAZINGA!” 5) Returns 0 if the User won, 1 if the Computer won, and 2 if the User and Computer Tied.

Solutions

Expert Solution

#include <iostream>

#include <cmath>

#include <time.h>

#include <cstdlib>

using namespace std;

int main(){

    char ch;

    int win = 0;

    int tie = 0;

    int lose = 0;

    do{

    int choice;

    cout << "--------------------------------------" << endl;

    cout << "-- Lets play Rock, Paper, Scissors! --" << endl;

    cout << "--------------------------------------" << endl;

    cout << "Press 1 for Rock, 2 for Paper, 3 for Scissors:" << endl;

    cin >> choice;

    int ai = rand() % 3 + 1;

    cout << "The computer chose: " << ai << endl;

    if(choice == 1 && ai == 1){

         cout << "Rock meets Rock its a tie!" << endl;

         tie++;

         }

    else if(choice ==1 && ai== 2){

         cout << "Rock is covered by Paper the computer wins!." << endl;

         lose++;

         }

    else if(choice == 1 && ai == 3){

         cout << "Rock crushes Scissors you win!" << endl;

         win++;

         }

    else if(choice == 2 && ai == 1){

         cout << "Paper covers Rock you win!" << endl;

         win++;

         }

    else if(choice == 2 && ai == 2){

         cout << "Paper meets Paper its a tie!" << endl;

         tie++;

         }

    else if(choice == 2 && ai == 3){

         cout << "Paper is cut by Scissors the computer wins!" << endl;

         lose++;

         }

    else if( choice == 3 && ai == 1){

         cout << "Scissors are crushed by Rock computer wins!" << endl;

         lose++;

         }

    else if( choice == 3 && ai == 2){

         cout << "Scissors cuts Paper you win!" << endl;

         win++;

         }

    else if(choice == 3 && ai == 3){

         cout << "Scissors meet Scissors its a tie!" << endl;

         tie++;

         }

    else{

         cout << "You didn't select 1, 2, or 3" << endl;

         }

         cout << "Wins: " << win << endl;

         cout << "Ties:" << tie << endl;

         cout << "Losses:" << lose << endl;

         cout << "Would you like to play again? Y/N" << endl;

         cin >> ch;

         system("CLS");

         }while(ch == 'Y' || ch == 'y');

    return 0;

}


Related Solutions

example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int c;    char line[100]; //declaring string for storing data in the line of text    FILE *fp; // declaring a FILE pointer    fp=fopen("numbers.txt","r"); // open a text file for reading    while(fgets(line, sizeof line, fp)!=NULL) {       // looping until end of the file         filenum[i]=atoi(line); //converting data in the line to integer and storing it into array        i++;    }...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE *fp;        char c;        errno_t err;        err = 0;        err = fopen_s(&fp,"Desktop/test.txt", "r"); file is on my desktop but I err=2 but I don't think it is opening the file?        printf("%d\n", err);        if (err == 2)        {            printf("The file was opened\n");            while (1)       ...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data;...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data; struct list *next; }; typedef struct list node; typedef node *link; int main() { link ptr,head; int num,i; head = ( link ) malloc(sizeof(node)); ptr = head; printf("enter 10 data \n"); for ( i = 0; i <= 9; i++ ) { scanf("%d",&num); ptr->data = num; ptr->next = ( link ) malloc(sizeof(node)); if ( i == 9 ) ptr->next = NULL; else ptr =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT