Question

C program: Your task is to write a program which asks the user for a move...

C program:

Your task is to write a program which asks the user for a move (Rock, Paperor Scissors) and then, based on the user’s move, chooses a response (Rock, Paper or Scissors). This process continues until the user decides to exit the game.

If the program “knew” the rules of the game, is would be trivial to make the program choose Paper when the user plays Rock, and Scissors when the user plays Paper, etc. Instead, your program should play randomly at first, and change its play based on previous games, through reinforcement learning.

After every game, the program should output the user’s move, the computer’s move, the outcome (win / draw), and the probability of the computer’s move.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Main.c

#include <stdio.h>

#include <stdlib.h>

#define LENGTH 3

int randomization(double *ptr, int num)

{

  double dbl = rand() / (RAND_MAX + 1.0);

  int x;

  for (x = 0; x < num - 1 && (dbl -= ptr[x]) >= 0; x++);

  return x;

}

int main()

{

  int u_input, comp_input;

  int arr[] = {0, 0, 0};

  const char *arr1[] = { "Rock", "Paper", "Scissors" };

  char str[2];

  const char *wins[] = { "We tied.", "AI wins.", "You win." };

  double ptr[LENGTH] = { 1./3, 1./3, 1./3 };

  while (1) {

    comp_input = randomization(ptr,LENGTH);

    printf("\nYour choice [1-3]:\n"

      " 1. Rock\n 2. Paper\n 3. Scissors\n> ");

   if (!scanf("%d", &u_input)) {

      scanf("%1s", str);

      if (*str == 'q') {

        printf("Your choices [rock : %d , paper : %d , scissors %d] ",arr[0],arr[1], arr[2]);

        return 0;

      }

      continue;

    }

    u_input --;

    if (u_input > 2 || u_input < 0) {

      printf("invalid choice; again\num");

      continue;

    }

    printf("You choose %s; I choose %s. %s\n",

      arr1[u_input], arr1[comp_input],

      wins[(comp_input - u_input + 3) % 3]);

    arr[u_input]++;

  }

}

Output:

Add a comment
Know the answer?
Add Answer to:
C program: Your task is to write a program which asks the user for a move...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

  • In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play...

    In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play the game of Rock, Paper, Scissors against the computer.Your program should have the following: •Make the name of the project RockPaperScissors•Write a method that generates a random number in the range of 1 through 3. The randomly generated number will determine if the computer chooses rock, paper, or scissors. If the number is 1, then the computer has chosen rock. If the number is...

  • Write a Python program (using python 3.7.2) that lets the user play the game of Rock,...

    Write a Python program (using python 3.7.2) that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don’t display the...

  • java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...

    java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates the...

  • Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will...

    Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will pick a move (Rock Paper Scissors) from 3 radio buttons and click the play button to play the game. The application will use random number generator to pick a move for the computer. Then, the application display the computer's move and also display whether you won or lost the game, or the game is tie if both moves are the same. The application must...

  • Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game...

    Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game in Python 3 that a user plays against the computer with the ability to save and load a game and its associated play statistics. Purpose: The purpose of this challenge is to assess the developer’s ability to create an interactive application with data persistence in Python 3. Requirements: Create a Rock, Paper, Scissors game in Python named rps.py according to the requirements specified in...

  • C++ You are asked to implement scissors-rock-paper game where the players are a computer and a...

    C++ You are asked to implement scissors-rock-paper game where the players are a computer and a user. The player that wins 3 hands successively is the winner of the game. Your program should prompt the user with a message at the beginning of each hand to enter one of scissors, rock or paper. If the user’s entry is not valid, i.e. entry is neither scissors, rock, nor paper, your program throws a message to the user to enter a valid...

  • (C++) Hey guys we just went over loops and it got me confused, it has me...

    (C++) Hey guys we just went over loops and it got me confused, it has me scrathcing my head for the past two days. I would appreciate any help you guys have to offer. Few places I have a hard time is get input and determine winner(cummulative part). Write a program that lets the user play the Rock, Paper, Scissors game against the computer. The computer first chooses randomly between rock, paper and scissors, but does not display its choice....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT