Question

codeblocks program c rock paper scissor tallies win loss and draw need to be repeating ask...

codeblocks program c
rock paper scissor
tallies win loss and draw
need to be repeating ask user for input if they wanna play again using a yes or no
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

#include <stdio.h> // Standard IO

#include <stdlib.h> // other stuff

#include <time.h>

#include <string.h>

//This should add weighted random function to "The Elite Noob"'s code, stolen from above code because it does calculation so well

//closest I could make it to original but without pointless attempt to make code look smaller than above code by putting code on the same lines

int rand_i(int n)

{

  int rand_max = RAND_MAX - (RAND_MAX % n);

  int ret;

  while ((ret = rand()) >= rand_max);

  return ret/(rand_max / n);

}

int weighed_rand(int *tbl, int len)

{

  int i, sum, r;

  for (i = 0, sum = 0; i < len; sum += tbl[i++]);

  if (!sum) return rand_i(len);

  r = rand_i(sum) + 1;

  for (i = 0; i < len && (r -= tbl[i]) > 0; i++);

  return i;

}

int main(int argc, const char *argv[])

{

  char umove[10], cmove[10], line[255];

  int user, comp;

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

  int tbllen=3;

int win = 0, lost = 0, tied = 0;

mainloop:

  while(1)

  { // infinite loop :)

    printf("\n\nPlease type in 1 for Rock, 2 For Paper, 3 for Scissors\n");

    srand(time(NULL));

    comp = (weighed_rand(tbl, tbllen) + 1) % 3;

    fgets(line, sizeof(line), stdin);

    while(sscanf(line, "%d", &user) != 1) //1 match of defined specifier on input line

    {

      printf("You have not entered an integer.\n");

      fgets(line, sizeof(line), stdin);

    }       

    if( (user > 4) || (user < 1) )

    {

      printf("Please enter a valid number!\n");

      continue;

    }

    switch (comp)

    {

      case 1 :

        strcpy(cmove, "Rock");

        break;

      case 2 :

        strcpy(cmove, "Paper");

        break;

      case 3 :

        strcpy(cmove, "Scissors");

        break;

      default :

        printf("Computer Error, set comp=1\n");

        comp=1;

        strcpy(cmove, "Rock");

        break;

    }

    switch (user)

    {

      case 1 :

        strcpy(umove, "Rock");

        break;

      case 2 :

        strcpy(umove, "Paper");

        break;

      case 3 :

        strcpy(umove, "Scissors");

        break;

      default :

        printf("Error, user number not between 1-3 exiting...");

        goto mainloop;

    }

    if( (user+1)%3 == comp )

    {

      printf("Comp Played: %s\nYou Played: %s\nSorry, You Lost!\n", cmove, umove);

lost ++;

    }

    else if(comp == user)

    {

      printf("Comp Played: %s\nYou Played: %s\nYou Tied :p\n", cmove, umove);

tied ++;

    }

    else

    {

      printf("Comp Played: %s\nYou Played: %s\nYay, You Won!\n", cmove, umove);

win ++;

    }

    tbl[user-1]++;

char cont = 'y';

printf("Do you want to continue (y/n)? ");

scanf("%c", &cont);

if (cont !='y' && cont != 'Y') {

break;

}

  }

printf("Wins = %d, ties = %d, lost = %d", win, tied, lost);

}

OUTPUT

1
Comp Played: Paper
You Played: Rock
Sorry, You Lost!
Do you want to continue (y/n)? y


Please type in 1 for Rock, 2 For Paper, 3 for Scissors
3
Comp Played: Rock
You Played: Scissors
Sorry, You Lost!
Do you want to continue (y/n)? y


Please type in 1 for Rock, 2 For Paper, 3 for Scissors
2
Comp Played: Rock
You Played: Paper
Yay, You Won!
Do you want to continue (y/n)? N
Wins = 1, ties = 0, lost = 2

Add a comment
Know the answer?
Add Answer to:
codeblocks program c rock paper scissor tallies win loss and draw need to be repeating ask...
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
  • Assignment C++: Rock-Scissor-Paper & Tic-Tac-Toe i need you to write two different program for RSP and...

    Assignment C++: Rock-Scissor-Paper & Tic-Tac-Toe i need you to write two different program for RSP and TTT: R-S-P Requirement: - write one program that mimics the Rock-Scissor-Paper game. This program will ask user to enter an input (out of R-S-P), and the computer will randomly pick one and print out the result (user wins / computer wins). - User's input along with computer's random pick will be encoded in the following format: -- user's rock: 10 -- user's scissor: 20...

  • Write a program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a...

    Write a program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. Here are sample runs: Enter your selection: scissor (0),...

  • *3.17 (Game: scissor, rock, paper) Write a JAVA program that plays the popular scissor-rock-paper game. (A...

    *3.17 (Game: scissor, rock, paper) Write a JAVA program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws ** Have to...

  • Write a java program that plays the popular scissor-rock-paper game. (A scissor can cut a paper,...

    Write a java program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can break a scissor, and a paper can cover a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. Allow the user to continue playing or quit.

  • Write a program to score the paper-rock-scissor game. Each of two users types in either P,R...

    Write a program to score the paper-rock-scissor game. Each of two users types in either P,R or S. The program call function Find winner to announces the winner. For determining the winner: Paper covers rock, Rock breaks scissors, Scissors cut paper, or it is a tie. Be sure to allow the users to use lower case as well as uppercase letters. Your program should include a loop that lets the user play again until the user says she or he...

  • 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...

  • 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...

  • I need the create a Python code that makes a two-player Rock-Paper-Scissors game. (Hint: Ask for...

    I need the create a Python code that makes a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game) Please help me see why my "continue" is asking the players to start a new game. Here is my code; user1= input("Whats your name? ") user2= input("And your name? ") a = input("%s, do yo want to choose...

  • For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-...

    For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-Oriented Programming. You will be working with me on a team to build the program. I have already written my part of the program, and the Game.java file is attached. Your task will be to write a RockPaperScissors class that contains the following methods: getUserChoice: Has the user choose Rock, Paper, or Scissors. After validating the input, the method returns a String containing the user choice....

  • Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember...

    Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember the rules: 1. Rock beats scissors 2. Scissors beats paper 3. Paper beats rock The program should ask the users for their names, then ask them for their picks (rock, paper or scissors). After that, the program should print the winner's name. Note, the players may pick the same thing, in this case the program should say it's tie. when the user input an...

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