C语言实现最简单的剪刀石头布小游戏示例

时间:2022-05-02 23:31:37

本文实例讲述了C语言实现最简单的剪刀石头布小游戏。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/*************\
* 剪刀 石头 布 *
* 最简单小游戏 *
\*************/
int main(void){
    char gesture[3][10] = {"scissor","stone","cloth"};
    int man, computer, result, ret;
    /*随机数初始化函数*/
    srand(time(NULL));
    while(1){
        computer = rand()%3;
        printf("\nInput your gesture 0-scissor 1-stone 2-cloth:\n");
        ret = scanf("%d", &man);
        if(ret !=1 || man<0 || man>2){
            printf("Invalid input!\n");
            return 1;
        }
        printf("Your gesture:%s\tComputer's gesture: %s\n",
        gesture[man], gesture[computer]
        );
        result = (man - computer + 4) %3 -1;
        if(result > 0)
            printf("YOU WIN!\n");
        else if(result == 0)
            printf("Draw!\n");
        else
            printf("You lose!\n");
    }
    return 0;
}

PS:游戏使用ctrl+c退出程序。

希望本文所述对大家C语言程序设计有所帮助。

原文链接:http://blog.csdn.net/e421083458/article/details/38978177