使用C语言怎么实现一个猜拳游戏

  介绍

本篇文章为大家展示了使用C语言怎么实现一个猜拳游戏,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

<强>第一步强,编写游戏。h头文件,把需要用到的函数声明及一些宏定义写在里面

# ifndef  __GAME_H__   # define  __GAME_H__      # include  & lt; stdio.h>   # include  & lt; windows.h>   # include  & lt; time.h>      # pragma 警告(禁用:4996)      void 菜单();   void  gamestart (int , com, char *, comname,, char *,名称);   时间的选择,int 法官(int  int  c);   int  computer_round ();   void 显示(int 统计,,int  ptimes,, int  ctimes,, char *, comname,, char *,名称);      # endif

<强>第二步强,编主要写函数,从这里调用函数

# include “game.h"   int  main ()   {   ,菜单();//调用菜单函数   ,系统(“pause");   ,return  0;   }

<强>第三步强,编写游戏。c,把需要用到的函数都写在里面。

菜单函数,指引用户做出选择

void 菜单()//菜单函数,指引用户做出选择   {   ,printf (“* * * * * * * * * * * * * * \ n");   ,printf(“* *猜拳,开始* * \ n");   ,printf (“* * * * * * * * * * * * * * \ n");   ,printf(“请选择对方角色(1。奥特曼2。葫芦娃3。孙悟空)\ n");   ,int  com =, 0;   ,char  comname [20],=, {, 0};   ,int  flag =, 1;   ,while (国旗),{,//为用户选择的对手创建名字   ,scanf (“% d",,, com);   ,switch  (com), {   ,case  1:拷贝字符串(comname,“奥特曼“);=,flag  0;   ,打破;   ,case  2:拷贝字符串(comname,“葫芦娃“);=,flag  0;   ,打破;   ,case  3:拷贝字符串(comname,“孙悟空“);=,flag  0;   ,打破;   ,默认值:printf(“输入有误! \ n");   ,打破;   ,}   ,}   ,printf(“请输入你的名字:“);   名字,char  [20],=, {, 0};   ,scanf (“% s",,名字);//用户自己创建角色   ,printf (“% sv % s \ n",,名字,comname);   ,printf(“要开始吗? (y/n) \ n");   ,char  choice =, 0;   ,while  (1), {   ,if (国旗),{,//判断用户是不是第一次进行游戏   ,printf(“要继续吗? (y/n) \ n");   ,}=,flag  1;   ,获取字符();   ,scanf (“% c",,,选择);   ,switch (选择),{   ,case  & # 39; y # 39;:, gamestart (comname、名称);//& # 39;y # 39;,开始游戏,调用gamestart函数   ,打破;   ,case  & # 39; n # 39;:, printf(“拜拜! \ n");//& # 39; n # 39;,游戏结束,函数调用结束   ,返回;   ,默认值:printf(“输入有误,请重新输入! \ n");   ,打破;   ,}   ,}   }

gamestart函数,游戏开始

void  gamestart (char *, comname, char *,名称)   {   ,int  static  ptimes =, 0;//用户赢的次数   ,int  static  ctimes =, 0;//电脑赢得次数   ,int  static  count =, 0;,//游戏对战次数   ,if (计数),{,   ,显示(计数,ptimes、ctimes comname,名字),,//如果不是第一次进入游戏,则显示当前对战情况   ,}   ,+ +,//每进行一次游戏,自数加一   ,printf(“请出拳:1。石头2。剪刀3。布\ n");   ,int  choice =, 0;   ,printf(“你出拳:“);   ,int  flag =, 1;   ,while (国旗),{   ,scanf (“% d",,,选择);   ,switch (选择),{   ,case  1: printf(“石头\ n");=,flag  0;   ,打破;   ,case  2: printf(“剪刀\ n");=,flag  0;   ,打破;   ,case  3: printf(“布\ n");=,flag  0;   ,打破;   ,默认值:printf(“输入有误,请重新输入! \ n");   ,}   ,}   ,printf (“% s出拳:“,,comname);   ,int  result =,法官(选择,,computer_round());//先调用computer_round函数,得到电脑的选择   ,,,,,//然后调用法官函数,判断输赢   ,switch (结果),{   ,case  1: printf(“很遗憾,你输了! \ n");   ,ctimes + +,,//记录电脑赢的次数   ,打破;   ,case  0: printf(“还不错,平局! \ n");   ,打破;   ,case  1: printf(“恭喜你,你赢了! \ n");   ,ptimes + +,,//记录用户赢的次数   ,打破;   ,}   }

法官函数,判断输赢

int 法官(int 选择,int  c)//判断函数,判断输赢   {   ,if  (choice ==, c),{,//如果两个选择相同,则平局   ,return  0;   ,}   ,if  (choice 作用;c ==, 1, | |, choice 作用;c ==, 2),{,//选择是用户选择,若符合这两个结果,则证明用户输   ,return  1;   ,}   ,else  {,,   ,return  1;//否则用户赢   ,}   }

使用C语言怎么实现一个猜拳游戏