Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create your own unique While - End or (For End) repetition C code . You decide t

ID: 3592818 • Letter: C

Question

Create your own unique While - End or (For End) repetition C code . You decide the theme. Be sure to provide an overview of what your repetition structure is doing. Please keep the design simple for this exercise. Just a few lines of code is all that is needed for this response. This should be code you wrote for an application that is interesting to you. In other words, make it your own and have fun with it. Provide the C code and a screen capture showing the results of testing your code in an online compiler. Be sure to test your code with several test cases and show your test case table.

Explanation / Answer

#include <stdio.h>

int main(void) {
   /* This program is designed to play Rock/Paper/Scissors with an AI opponent. */
  
    /* We start by defining the variables. */
    int wins, gamesPlayed;
    char yourChoice, aiChoice;
  
    /* This will be best of 3 games, with Draw not counting. */
    while(gamesPlayed < 3) {
        /* First, the AI needs to choose a random RPS toss. */
        aiChoice = choice(({ "R", "P", "S" }));
      
        /* Next, we let the player choose theirs. */
        printf("Which do you wish to throw? R, P, or S? ");
        scanf("%s", &yourChoice);
      
        /* Was the choice valid? If not, make them choose again. */
        if(member_array(yourChoice, ({ "R", "P", "S" })) < 0) {
            /* Choice was invalid, so we'll tell them that and the while will loop back around to the choice because no changes were made to gamesPlayed. */
            printf("That was an invalid choice. ");
        } else {
            /* Ok, we got here, so their choice was valid. Now we need to see if they're the same pick. If so, it'll be a draw. */
            if(aiChoice == yourChoice) {
                /* Since it was a draw, we tell them that and the while loops back to the next round without changing gamesPlayed. */
                printf("It was a draw! ");
            } else {
                /* Next, we'll check to see if you lose. */
                if((yourChoice == "R" && aiChoice == "P") || (yourChoice == "P" && aiChoice == "S") || (yourChoice == "S" && aiChoice == "R")) {
                    /* Since the player didn't win, we tell them so but don't increase the wins variable. */
                    printf("You lost! ");
                } else {
                    /* Since the connected if statement was for losing, this is where we put the win message and increase wins. */
                    printf("You win! ");
                    wins++;
                }
              
                /* Since both winning and losing count as a game played, we increase gamesPlayed here. */
                gamesPlayed++;
            }
        }
      
        /* The game is over, so we need to announce how many wins the player and AI have. */
        printf("You have won %d times. Your opponent has won %d times. ", wins, (gamesPlayed - wins));
      
        /* Since we're playing best of 3, if you have 2 wins, the game should end. */
        if(wins > 1) {
            printf("Congratulations! You're the champion! ");
          
            /* To make 2 wins end the game even if the gamesPlayed isn't 3, we make gamesPlayed equal 3. */
            gamesPlayed = 3;
        } else if(gamesPlayed > 2) {
            /* The player didn't win best of 3, so we'll tell them so. */
            printf("Too bad! Your opponent is the champion! ");
          
            /* Since gamesPlayed is already greather than 2, there's no need to go any further. The while statement will end after this. */
        }
    }  
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote