C-language implementation of Scissors game code sharing _c language

Source: Internet
Author: User
Tags clear screen rand

This is a simple scissors game (scissors stuffed hammer) that lets you duel with the computer. Your fist is up to you, the computer randomly punches, and the final judge wins.

The following code implements a scissors game that allows you to duel with the computer. Your fist is up to you, the computer randomly punches, and the final judge wins.

After starting the program, let the user punch, screenshot:

User out fist, show showdown result: Screenshot:

Code implementation:

#include <stdio.h> #include <stdlib.h> #include <time.h> int main () {char gamer;//player out fist int compu ter Computer out fist int result;
    Game Results//To avoid playing a game on the exit program, you can put the code in the Loop while (1) {printf ("This is a scissors game, please enter your fist: \ n");
    printf ("A: Scissors \NB: Stone \nc: Cloth \nd: No play \ n");
    scanf ("%c%*c", &gamer);
        Switch (gamer) {case,//a case://a gamer=4;
      Break
        Case://b case://b gamer=7;
      Break
        Case://c case://c gamer=10;
      Break
     
      Case://d,//d return 0;
        default:printf ("Your choice for%c selection error, exit ... \ n", gamer);
        GetChar (); System ("CLS");
        Clear screen return 0;
    Break } srand ((unsigned) time (NULL)); Random number seed computer=rand ()%3; Generate random number and take the remainder, get the computer out fist result= (int) gamer+computer;
    Gamer is a char type, and you want to cast the type printf ("Computer Out") in mathematical operations;
    Switch (computer) {case 0:printf ("scissors \ \"); break;//4 1  Case 1:printf ("Stone \ n");  7 2 case 2:printf ("cloth \ n");
    3} printf ("You are out");
      Switch (gamer) {Case 4:printf ("scissors \ \");
      Case 7:printf ("Stone \ n");
    Case 10:printf ("cloth \ n"); } if (result==6| | result==7| |
    result==11) printf ("You won!"); else if (result==5| | result==9| |
    result==10) printf ("Computer wins!");
    else printf ("Deuce"); System ("Pause>nul&&cls");
Pause and Clear Screen} return 0;
 }

Code Analysis

1 First, we need to define 3 variables to store the player's Fist (gamer), the computer's fist (computer), and the final result, and then give a text hint to get the player to punch.

Next receive player input:

scanf ("%c%*c", &gamer);

Note: Because each entry ends with a carriage return, the buffer has a return character in addition to the letter entered by the player. Returns are skipped so that the next entry is not affected. The SCANF () function has a format that controls more than the number of parameters, Scanf ("%c%*c", &gamer), and the effect is to output one character (carriage return) from the buffer without assigning any variables.

Player input end, use the switch statement to determine the input, (a), (a), (a), the (b), (b), (c), (c), (d), (d) for the corresponding character of the ASCII code.

Note: System ("CLS"); The function of the statement is to clear the screen. The System () function is used to execute DOS commands, which are equivalent to entering CLS commands in DOS.

2 player out of the fist end, the computer began to punch.

The computer punches by generating random numbers:

Srand ((unsigned) time (NULL)); In order to avoid the same result of multiple runs, the preceding join (requires time.h)
Computer=rand ()%3;//Get random number of 0~2

Finally, the player and the computer out of the boxing and to judge the winning and losing:

result= (int) gamer+computer;
// ...
if (result==6| | result==7| | result==11) printf ("You won!");
else if (result==5| | result==9| | result==10) printf ("Computer wins!");
else printf ("Deuce");

This is a very clever algorithm, the player and the computer punches different, result value is different, and will not repeat, see the following table:

Computer--Player stone (4) Scissors (7) cloth (
0) 4 7
Scissors (1) 5 8 One
cloth (2) 6 9 12

3 Each scissors end, suspend and clear screen, into the next scissors:

System ("Pause>nul&&cls");  Suspend operation and clear screen

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.