当前位置: 首页 > 新闻动态 > 软件编程

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

作者:用户投稿 浏览: 发布日期:2026-01-11
[导读]:这篇文章主要介绍了C语言实现最简单的剪刀石头布小游戏,涉及C语言数组、随机数与数值运算等相关操作技巧,需要的朋友可以参考下

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

#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://sczxchw.cn/news/219320.html

扫一扫高效沟通

多一份参考总有益处

免费领取网站策划SEO优化策划方案

请填写下方表单,我们会尽快与您联系
感谢您的咨询,我们会尽快给您回复!