C语言模拟剪刀石头布(附源码)

基于VIsual Studio:

#define _CRT_SECURE_NO_WARNINGS 1  
#include<stdio.h>  
#include<time.h>  
#include<windows.h>  
#define scissors 1
#define rock 2
#define paper 3
void introduce_game();//游戏介绍
int player_choice( );//玩家的选择
int computer_choice( );//电脑的选择
void print_choice(int choice);//打印选择的结果
void judge_winner(int player_choose, int compueter_choose);//判断胜利者
char  judge_continue();//判断是否要继续
int main(void)
{
    srand(time(NULL));
    char c = 'y';
    while(c=='y'){
     introduce_game();
    int player_choose = player_choice();
    int computer_choose = computer_choice();
    printf("你选择了:\n" ); 
    print_choice(player_choose);
    printf("电脑选择了:\n");
    print_choice(computer_choose);
    judge_winner(player_choose, computer_choose);
    c= judge_continue();
    system("cls");//清屏
    }
    return 0;

void introduce_game() {//游戏介绍
    puts("剪刀石头布人机对决现在开始!");
    puts("规则:剪刀1石头2布3");
    puts("———————————————————————————");
}
int player_choice() {//玩家选择
    int choice;
    puts("请输入你的选择:");
    scanf("%d", &choice);
    while (choice< scissors || choice>paper) {
        puts("无效的选择请重新选择:剪刀1石头2布3");//判断是否符合要求
    scanf("%d", &choice);
    }
    return choice;
}
int computer_choice( ) {//电脑选择
    int choice;
    choice = rand() % 3 + 1;
    return  choice;
}
void print_choice(int choice){//数字转化为文字
    switch (choice) {
    case scissors: puts("剪刀"); break;
    case rock: puts("石头"); break;
    case paper: puts("布"); break;
    }
}
void judge_winner(int player_choose, int compueter_choose) {//判断胜利者
    if ( player_choose== compueter_choose) {
        puts("平局");
    }
    else if ((player_choose == scissors && compueter_choose == paper) || (player_choose == rock && compueter_choose == scissors) || (player_choose == paper && compueter_choose == rock)) {
        puts("你赢了");
    }
    else {
        puts("你输了");
    }
}
char  judge_continue() {//判断玩家是否需要继续游戏
    char c;
    puts("是否继续(y/n)");
    scanf(" %c", &c);//注意缓冲区
    if (c == 'y' || c == 'Y') {
        c = 'y';
    }
    else {
        c = 'n';
    }
    return c;
}

  • 21
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值