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;
}

ctrl+c退出。



使用ctrl+c退出程序。

  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是一个C语言实现的石头剪刀布小游戏,可以控制胜率的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int play, computer, winCount = 0, loseCount = 0, drawCount = 0; char choice; srand(time(NULL)); do { printf("请选择:\n"); printf("1. 石头\n"); printf("2. 剪刀\n"); printf("3. 布\n"); printf("0. 退出\n"); scanf("%d", &play); if (play == 0) { break; } computer = rand() % 3 + 1; printf("你选择了:"); switch (play) { case 1: printf("石头\n"); break; case 2: printf("剪刀\n"); break; case 3: printf("布\n"); break; default: printf("无效选择\n"); continue; } printf("电脑选择了:"); switch (computer) { case 1: printf("石头\n"); break; case 2: printf("剪刀\n"); break; case 3: printf("布\n"); break; } if (play == computer) { printf("平局!\n"); drawCount++; } else if ((play == 1 && computer == 2) || (play == 2 && computer == 3) || (play == 3 && computer == 1)) { printf("你赢了!\n"); winCount++; } else { printf("你输了!\n"); loseCount++; } printf("胜利次数:%d,失败次数:%d,平局次数:%d\n", winCount, loseCount, drawCount); printf("是否继续游戏?(y/n) "); scanf(" %c", &choice); } while (choice == 'y' || choice == 'Y'); return 0; } ``` 这个程序使用了随机数生成电脑的选择,然后根据玩家和电脑的选择进行比较,判断胜负或平局,并统计胜利、失败和平局的次数。你可以通过多次运行程序来控制胜率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

e421083458

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值