本篇文章为大家展示了使用C语言怎么实现一个猜拳游戏,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
第一步,编写game.h头文件,把需要用到的函数声明及一些宏定义写在里面
#ifndef __GAME_H__
#define __GAME_H__
#include <stdio.h>
#include <windows.h>
#include <time.h>
#pragma warning(disable:4996)
void menu();
void gamestart(int com, char* comname, char* name);
int judge(int choice, int c);
int computer_round();
void show(int count, int ptimes, int ctimes, char* comname, char* name);
#endif
第二步,编写main函数,从这里调用函数
#include "game.h"
int main()
{
menu();//调用menu函数
system("pause");
return 0;
}
第三步,编写game.c,把需要用到的函数都写在里面。
menu函数,指引用户做出选择
void menu()//menu函数,指引用户做出选择
{
printf("**************\n");
printf("**猜拳,开始**\n");
printf("**************\n");
printf("请选择对方角色(1.奥特曼2.葫芦娃3.孙悟空)\n");
int com = 0;
char comname[20] = { 0 };
int flag = 1;
while (flag) { //为用户选择的对手创建名字
scanf("%d", &com);
switch (com) {
case 1:strcpy(comname,"奥特曼");
flag = 0;
break;
case 2:strcpy(comname, "葫芦娃");
flag = 0;
break;
case 3:strcpy(comname, "孙悟空");
flag = 0;
break;
default:printf("输入有误!\n");
break;
}
}
printf("请输入你的名字:");
char name[20] = { 0 };
scanf("%s", name);//用户自己创建角色
printf("%sVS%s\n", name,comname);
printf("要开始吗?(y/n)\n");
char choice = 0;
while (1) {
if (flag) { // 判断用户是不是第一次进行游戏
printf("要继续吗?(y/n)\n");
}
flag = 1;
getchar();
scanf("%c", &choice);
switch (choice) {
case 'y': gamestart(comname,name);//'y',开始游戏,调用gamestart函数
break;
case 'n': printf("拜拜!\n");//'n',游戏结束,函数调用结束
return;
default: printf("输入有误,请重新输入!\n");
break;
}
}
}
gamestart函数,游戏开始
void gamestart(char* comname,char* name)
{
int static ptimes = 0;//用户赢的次数
int static ctimes = 0;//电脑赢得次数
int static count = 0; //游戏对战次数
if (count) {
show(count,ptimes,ctimes,comname,name); //如果不是第一次进入游戏,则显示当前对战情况
}
count++;//每进行一次游戏,count自加一
printf("请出拳:1.石头2.剪刀3.布\n");
int choice = 0;
printf("你出拳:");
int flag = 1;
while (flag) {
scanf("%d", &choice);
switch (choice) {
case 1:printf("石头\n");
flag = 0;
break;
case 2:printf("剪刀\n");
flag = 0;
break;
case 3:printf("布\n");
flag = 0;
break;
default:printf("输入有误,请重新输入!\n");
}
}
printf("%s出拳:", comname);
int result = judge(choice, computer_round());//先调用computer_round函数,得到电脑的选择
//然后调用judge函数,判断输赢
switch (result) {
case -1:printf("很遗憾,你输了!\n");
ctimes++; //记录电脑赢的次数
break;
case 0:printf("还不错,平局!\n");
break;
case 1:printf("恭喜你,你赢了!\n");
ptimes++; //记录用户赢的次数
break;
}
}
judge函数,判断输赢
int judge(int choice, int c)//judge函数,判断输赢
{
if (choice == c) { //如果两个选择相同,则平局
return 0;
}
if (choice - c == 1 || choice - c == -2) { // choice是用户选择,若符合这两个结果,则证明用户输
return -1;
}
else {
return 1; //否则用户赢
}
}
computer_round函数,电脑回合
int computer_round()//computer_round函数,电脑选择出什么
{
srand((unsigned long)time(NULL));
int c = rand() % 2 + 1; //与人的选项一样,1.石头2.剪刀3.布
if (c == 1) {
printf("石头\n");
}
else if (c == 2) {
printf("剪刀\n");
}
else {
printf("布\n");
}
return c;
}
show函数,显示当前对战情况
void show(int count, int ptimes, int ctimes, char* comname, char* name)
{
system("cls");
printf("%sVS%s\n", name, comname);
printf("对战次数:%d\n", count);
printf("姓名 得分\n");
printf("%6s %d\n", name, ptimes);
printf("%6s %d\n", comname, ctimes);
}
四、运行结果
上述内容就是使用C语言怎么实现一个猜拳游戏,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。