介绍
本篇文章给大家分享的是有关使用C语言怎么实现一个猜拳小游戏,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
具体内容如下
# include# include # include # include int pcinput,//电脑输入 int userinput,//用户输入 # define echo “祝辞_ “,//俺觉得好看 char case_win (int 左,int 右);//判断哪边赢了,剩下的是玩家,右边是电脑 int echoinput (int inputnum);//根据两个全局变量输出对应进行的操作 int main (), { ,printf(“规则\ n \ n"); ,把(“1。石头\ n"); ,把(“;2。剪刀\ n"); ,把(“3。布,\ n"); ,printf (“% s”你的输入:“,,回声); ,scanf_s (“% d",,, userinput); ,printf (“\ n"); ,if (echoinput (userinput),==, 1), { ,睡眠(1000); ,系统(“cls"); ,main (); ,} ,else { ,printf (“\ n"); ,printf (“\ n"); ,printf (“% s等待电脑结果。\ 0“,,回声); ,睡眠(500); ,printf (“!”); ,睡眠(500); ,printf (“\ n"); ,将srand(时间(NULL));=,,pcinput  rand (), %, 3, +, 1; ,printf (“\ n"); ,echoinput (pcinput); ,printf (“\ n"); ,printf (“\ n"); ,if (case_win (userinput pcinput),==, & # 39; l # 39;), { ,printf(“玩家胜利“); ,} ,else 如果(case_win (userinput pcinput)==& # 39; " # 39;) { ,printf(“电脑胜利“); ,} ,else { ,printf(“双方平局“); ,} ,printf (“\ n"); ,printf (“\ n"); ,printf (“- - - - - - - - - - - - - - - - - - - - -“); ,printf (“\ n"); ,printf (“\ n"); ,printf(“三秒后继续…“); ,睡眠(1000); ,printf(“二秒后继续…“); ,睡眠(1000); ,printf(“一秒后继续…“); ,睡眠(1000); ,系统(“cls"); ,main (); ,} ,return 0; } char case_win (int 左,int 右),{ ,char reslut; ,/* ,*声明 ,*石头→1 ,*剪刀→2 ,* _布→3 ,*/,/* ,*规则: ,*石头→剪刀 ,*剪刀→布 ,*布→石头 ,*/,/*判断,左边是玩家*/,if (left ==,右),{=,reslut & # 39; p # 39;; ,} ,else { ,if (left ==, 1), { ,if (right ==, 2), {=,reslut & # 39; l # 39;; ,} ,else if (==right 3), {=,reslut & # 39; " # 39;; ,} ,} ,else if (==left 2), { ,if (right ==, 1), {=,reslut & # 39; " # 39;; ,} ,else if (==right 3), {=,reslut & # 39; l # 39;; ,} ,} ,else if (==left 3), { ,if (right ==, 1), {=,reslut & # 39; l # 39;; ,} ,else if (==right 2), {=,reslut & # 39; " # 39;; ,} ,} ,} ,return reslut; } int echoinput (int inputnum), { ,int reslut =, 0; ,switch (inputnum), { ,case 1: ,printf(“(石头)“); ,打破; ,case 2: ,printf(“(剪刀)“); ,打破; ,case 3: ,printf(“(布)“); ,打破; ,默认值: ,printf(“(输入有误)“);=,reslut 1; ,打破; } ,return reslut; }
全局变量
int pcinput;,//电脑输入 int userinput,//用户输入
函数定义
char case_win (int 左,int 右);//判断哪边赢了,离开是玩家,权利是电脑 int echoinput (int inputnum);//根据两个全局变量输出对应进行的操作
打印规则和输入提示
printf(“规则\ n \ n"); 把(“1。石头\ n"); 把(“;2。剪刀\ n"); 把(“3。布,\ n"); printf (“% s”你的输入:“,,回声); scanf_s (“% d",,, userinput); printf (“\ n");
主方法中的主要逻辑
int 主要(),{ ,printf(“规则\ n \ n"); ,把(“1。石头\ n"); ,把(“;2。剪刀\ n"); ,把(“3。布,\ n"); ,printf (“% s”你的输入:“,,回声); ,scanf_s (“% d",,, userinput); ,printf (“\ n"); ,if (echoinput (userinput),==, 1), { ,睡眠(1000); ,系统(“cls"); ,main (); ,} ,else { ,printf (“\ n"); ,printf (“\ n"); ,printf (“% s等待电脑结果。\ 0“,,回声); ,睡眠(500); ,printf (“!”); ,睡眠(500); ,printf (“\ n"); ,将srand(时间(NULL));=,,pcinput  rand (), %, 3, +, 1; ,printf (“\ n"); ,echoinput (pcinput); ,printf (“\ n"); ,printf (“\ n"); ,if (case_win (userinput pcinput),==, & # 39; l # 39;), { null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null使用C语言怎么实现一个猜拳小游戏