VC++6.0 to achieve the rock paper scissors game procedures

  • 2020-04-02 02:57:09
  • OfStack

The source program is from the Internet,
Geek_monkey fixed a bug on March 3, 2015 (player wins if input character is not rock paper scissors)
Compile environment for VC++6.0
Adding "god mode" and statistics is pure entertainment.
I am a C language beginner, light spray


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int exist_in(char *arr1[][2], char *arr2[], int length);//
void nextround (void);//
after the end of the round enum {QUIT,OK} status;//Token bit, QUIT=0,OK=1
int vcnt = 0,dcnt = 0,tcnt = 0;//Data statistics
int main(void)
{
    int i = 0, length;
    time_t t;
    status = OK;
    char *person = (char *) malloc(100);//Dynamic memory allocation
    char *computer = (char *) malloc(100);
    char *computer_person[2];
    char *guess_arr[] = {" stone ", " scissors ", " cloth "};
    char *win[3][2] = {{" cloth ", " stone "}, {" stone ", " scissors "}, {" scissors ", " cloth "}};   
    length = (int)sizeof(win)/(int)sizeof(win[0]);//In this example length=3
    while (status == OK)//Flag bit 1, execute dead loop
    {
        srand((unsigned) time(&t));
        i = rand() % 3 ;//Srand and rand combine to produce a random number
        computer = guess_arr[i];//Converts a random number to a string
of rock scissors or paper         computer_person[0] = computer;//Puts the result of the computer's punch into *computer_person
        do{
            printf(" Please enter the scissors stone cloth :n");
            scanf("%s",person);
            if (strcmp(person,"god") == 0)//God mode, implementation can see the computer punch results
            {
                printf("*********************************n");
                printf(" Oh, my god, this is from computer bureau : %s n",computer);
                printf("*********************************n");
                continue;
            }
            else
                printf(" What you typed is :%sn", person);//The next line determines whether the user's input is rock paper scissors
                if ((strcmp(person," stone ") == 0 )||(strcmp(person," scissors ") == 0 )||(strcmp(person," cloth ") == 0 ))
                    break;//The result is one of rock, paper, scissors, jumping out of the loop. < br / >                 else
                    printf(" Please check if your input is rock scissors or paper: n");
        }while(1);
        computer_person[1] = person;
        tcnt++;       
        if (strcmp(computer, person) == 0 )
        {
            printf(" A draw! nn");
        }
        else if (exist_in(win, computer_person, length))
        {
              printf(" Computer win nn");
              dcnt++;
        }
        else
        {
            printf(" The player won nn");
            vcnt++;
            nextround();
        }  
    }
    person = NULL;
    computer = NULL;   
    free(person);
    free(computer);
    return 0;
}
/**********************************************
 exist_in Function to determine if the computer wins, length for 3 And compare the 3 Time, arr1 and arr2 Medium string
  The order determines whether the computer wins or not.
 *********************************************/
int exist_in(char *arr1[][2], char *arr2[], int length)
{
    int i;
    for (i = 0; i < length; i++)
    {
        if (strcmp(arr1[i][0], arr2[0]) == 0 && strcmp(arr1[i][1], arr2[1]) == 0)
        {
            return 1;
        }
    }
    return 0;
 }
void nextround (void)
{
    int m = 0;
    printf(" Please enter instructions: 1 Exit. 2 To continue, 3 Summary data n");
    scanf ("%d",&m);
    switch (m)
    {
    case 1:
        status = QUIT;
        break;
    case 2:
        printf(" New start n");
        break;
    case 3:
        printf(" Your wins are %d The number of defeats is %d The total number of fields is %dn",vcnt,dcnt,tcnt);
        status = QUIT;
        break;
    default:
        printf(" Error exit ");
        status = QUIT;
        break;
    }
}

Above is the use of vc++ stone scissors paper program all the code, I hope to help you learn vc++.


Related articles: