#include <stdio.h> #include <stdlib.h> int main() { int player; int computer; int result; printf("Choose one of the following:\n"); printf("1. Rock\n2. Paper\n3. Scissors\n"); scanf("%d", &player); computer = rand() % 3 + 1; if (player == computer) { result = 0; } else if ((player == 1 && computer == 3) (player == 2 && computer == 1) (player == 3 && computer == 2)) { result = 1; } else { result = -1; } if (result == 0) { printf("Tie!\n"); } else if (result == 1) { printf("You win!\n"); } else { printf("You lose!\n"); } return 0; }
The given code first includes two standard libraries: <stdio.h> and <stdlib.h>, for input/output functions and for generating random numbers, respectively. Then the program declares three integer variables: 'player' to store the user's input, 'computer' to store the computer-generated random number, and 'result' to store the result of the game. The program prompts the user to choose an option from the menu of rock, paper, and scissors using printf() and waits for the user's input using scanf(). Then, the program generates a random number between 1 and 3 for the computer's choice using rand() function. After that, the program determines the result of the game by comparing the user's choice with the computer's choice using a series of if-else statements. If the player and computer choose the same option, the result is a tie. If the player wins, the result is 1, otherwise, the result is -1. Finally, the program prints the result of the game using another series of if-else statements, displaying "Tie!" if the result is 0, "You win!" if the result is 1, and "You lose!" if the result is -1.