编写剪刀(scissor)、石头(stone)、布(cloth)游戏

来源:互联网 发布:substr_replace php 编辑:程序博客网 时间:2024/04/29 07:19

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

int main(void)

{

    int user = -1;

    int computer = -1;

    int result = -1;

 

    char gesture[3][16] = {"scissor","stone", "cloth"};

#if 0

    printf("%s\n", gesture[0]);

    printf("%s\n", gesture[1]);

    printf("%c\n", gesture[1][3]);

#endif

 

#if 1

    srand(time(NULL));

    while (1) {

        // 1. 提示用户进行输入

INPUT:

        printf("Please input thegesture (0 - scissor 1 - stone 2 - cloth 3 - quit):\n");

 

        // 2. 接收用户的输入

        scanf("%d", &user);

        if (user < 0 || user > 3) {

            goto INPUT;

        }

 

        if (user == 3) {

            break;       

        }

 

        // 3. 程序随机生成石头、剪刀、布中的一种

        computer = rand() % 3;

 

        // 4. 打印用户输入和电脑生成的信息

        printf("\nYour:%s\tComputer: %s\n", gesture[user], gesture[computer]);

        // 5. 根据游戏规则,判断输赢结果

        // (U - C + 4 ) % 3 - 1

        result = (user - computer + 4) %3 - 1;

 

        // 6. 输出结果

        if (result > 0) {

            printf("You Win!\n");

        } else if (result == 0) {

            printf("Draw!\n");

        } else {

            printf("You lose!\n");

        }

    }

#endif

    printf("Thanks for yourjoin!\n");

    return 0;

}

0 0
原创粉丝点击