C语言实现猜拳游戏

来自:互联网
时间:2020-05-26
阅读:

一、问题

C语言实现猜拳游戏,用户自己选择对手,可以创建玩家角色。可以记录当前对战情况(对战局数,得分情况)

二、解决思路

猜拳游戏大家都不陌生,从小玩到大,遇到棘手的选择,猜拳往往是最能服众的处理办法。那么今天我们就用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%sn", 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%sn", name, comname);
 printf("对战次数:%dn", count);
 printf("姓名 得分n");
 printf("%6s %dn", name, ptimes);
 printf("%6s %dn", comname, ctimes);
}

四、运行结果

C语言实现猜拳游戏

C语言实现猜拳游戏

C语言实现猜拳游戏

C语言实现猜拳游戏

五、写在最后

既然在家都闲着没事,不如写个小游戏;不耽误学习,不耽误娱乐。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

返回顶部
顶部