C语言剪刀石头布小游戏

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

 

今天写了个小游戏 很有成就感

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的剪刀石头游戏的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int player_choice, computer_choice; int player_score = 0, computer_score = 0; char player_choice_name[10], computer_choice_name[10]; printf("欢迎来到剪刀石头游戏!\n"); printf("游戏规则:\n"); printf("1. 剪刀胜利石头石头胜利胜利剪刀。\n"); printf("2. 每次出拳后会显示双方的选择和得分。\n"); printf("3. 游戏结束后会显示最终结果。\n"); while (1) { // 玩家出拳 printf("\n请出拳:\n"); printf("1. 剪刀\n"); printf("2. 石头\n"); printf("3. \n"); scanf("%d", &player_choice); // 生成电脑出拳 srand(time(NULL)); computer_choice = rand() % 3 + 1; // 判断胜负 if ((player_choice == 1 && computer_choice == 3) || (player_choice == 2 && computer_choice == 1) || (player_choice == 3 && computer_choice == 2)) { player_score++; printf("你出了%s,电脑出了%s,你赢了!\n", player_choice_name[player_choice - 1], computer_choice_name[computer_choice - 1]); } else if ((player_choice == 1 && computer_choice == 2) || (player_choice == 2 && computer_choice == 3) || (player_choice == 3 && computer_choice == 1)) { computer_score++; printf("你出了%s,电脑出了%s,你输了!\n", player_choice_name[player_choice - 1], computer_choice_name[computer_choice - 1]); } else { printf("你出了%s,电脑出了%s,平局!\n", player_choice_name[player_choice - 1], computer_choice_name[computer_choice - 1]); } // 显示得分 printf("当前得分:\n"); printf("你:%d 电脑:%d\n", player_score, computer_score); // 判断是否继续游戏 printf("\n是否继续游戏?\n"); printf("1. 继续\n"); printf("2. 结束\n"); scanf("%d", &player_choice); if (player_choice == 2) { break; } } // 显示最终结果 printf("\n游戏结束!\n"); printf("最终得分:\n"); printf("你:%d 电脑:%d\n", player_score, computer_score); if (player_score > computer_score) { printf("你获胜了!\n"); } else if (player_score < computer_score) { printf("电脑获胜了!\n"); } else { printf("平局!\n"); } return 0; } ``` 注意,在上面的代码中,我使用了两个字符串数组来存储玩家和电脑的选择,以便在输出时显示选择的名称。如果你想要跑这个程序,请确保你的编译器支持C99标准,否则需要将这两个数组的定义放在函数的开头。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值