项目介绍 会子手游戏是一种流行的儿童猜词游戏。选择一个秘密词,玩家尝试通过猜测字母来找出这个词 每次玩家输入正确的字母后,会有相应的显示 如图显示: 具体代码如下
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
//Public variable
char user[20],pass[20];
char target[20];
void game_menu();
void sys_game();
void user_game();
void MakeRandStr();
void start_game();
int k=0;
int game_if=0;
int access(const char *pathname, int mode);
void A1()
{
printf("------------------------\n");
}
void A2()
{
printf("|\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("------------------------\n");
}
void A3()
{
printf("--------------------");
printf("|\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("------------------------\n");
}
void A4()
{
printf("--------------------");
printf("| |\n");
printf("| |\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("------------------------\n");
}
void A5()
{
printf("--------------------");
printf("| |\n");
printf("| |\n");
printf("| 0\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("------------------------\n");
}
void A6()
{
printf("--------------------");
printf("| |\n");
printf("| |\n");
printf("| 0\n");
printf("| !\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("------------------------\n");
}
void A7()
{
printf("--------------------");
printf("| |\n");
printf("| |\n");
printf("| 0\n");
printf("| !\n");
printf("| /\n");
printf("|\n");
printf("|\n");
printf("------------------------\n");
}
void A8()
{
printf("--------------------");
printf("| |\n");
printf("| |\n");
printf("| 0\n");
printf("| |\n");
printf("| /\\\n");
printf("|\n");
printf("|\n");
printf("------------------------\n");
}
void A_SW(int i)
{
switch(i)
{
case 1:A1();break;
case 2:A2();break;
case 3:A3();break;
case 4:A4();break;
case 5:A5();break;
case 6:A6();break;
case 7:A7();break;
case 8:A8();break;
}
}
//Import system secret
void MakeRandStr()
{
FILE *fp = NULL;
char filename[20];
while(1)
{
printf("\n请输入您要导入的秘密:");
scanf("%s",filename);
if((access(filename,F_OK))!=-1)
{
fp = fopen(filename,"r");
fscanf(fp,"%s",target);
break;
}
else
{
printf("没有找到该秘密\n");
}
}
fclose(fp);
}
//user login
int login()
{
printf("请输入用户名: \n");
scanf("%s",user);
char path[10]="user\\";
char temp[10];
strcat(path,user);
if((access(path,F_OK))!=-1)
{
FILE *fp = NULL;
fp = fopen(path, "r");
fscanf(fp, "%s",temp);
int i =0;
while(1)
{
if(i==3){printf("请输入密码次数超过3次,程序进入保护模式: \n");break;}
printf("请输入密码: \n");
scanf("%s",pass);
if(!strcmp(pass, temp)){
printf("登录成功\n");
fclose(fp);
return 1;
}
i++;
}
//printf("file mytest.c exist.\n");
}
else
{
printf("没有找到该用户\n");
}
return 0;
}
// user regedit
int regedit()
{
printf("请输入用户名: \n");
scanf("%s",user);
printf("请输入密码: \n");
scanf("%s",pass);
// printf("%s,%s",user,pass);
FILE *fp = NULL;
FILE *fp1 = NULL;
char path[10]="user\\";
strcat(path,user);
fp = fopen(path, "w");
fprintf(fp,"%s",pass);
strcat(path,"game");
fp1 = fopen(path,"w");
fclose(fp);
fclose(fp1);
return 0;
}
//system menu
int menu()
{
int x;
int bol=0;
while (1)
{
printf("-------------操作选项-----------\n");
printf("1:注册 \n");
printf("2:登录 \n");
printf("3:退出程序 \n");
printf("--------------------------------\n");
printf("按数字键选择要执行的操作: ");
scanf("%d",&x);
printf("\n");
if(x==3) {break;}
switch(x)
{
case 1:regedit(); break;
case 2:bol=login(); break;
default: printf("输入的数字不正确\n");break;
}
if(bol==1){printf("即将进入游戏\n");game_menu();bol=0;}
// system("cls");
}
return 0;
}
// User battle information
void user_mess()
{
FILE *FP = NULL;
char *mess;
char file_mess[25] ="user\\";
strcat(file_mess,user);
strcat(file_mess,"game");
FP = fopen(file_mess,"r");
fseek(FP,0,SEEK_END);
long SIZE = ftell(FP);
mess=(char*)malloc(SIZE+1);
rewind(FP);
fread(mess,sizeof(char),SIZE,FP);
mess[SIZE] = '\0';
//fscanf(FP,"%s",mess);
printf("历史挑战记录:\n%s\n\n",mess);
fclose(FP);
}
// Game operation interface
void game_menu()
{
int x;
while (1)
{
printf("-------------操作选项-----------\n");
printf("1:用户输入模式\n");
printf("2:系统秘密模式\n");
printf("3:查看用户挑战记录 \n");
printf("4:清除用户挑战记录 \n");
printf("5:返回主菜单 \n");
printf("--------------------------------\n");
printf("按数字键选择要执行的操作: ");
scanf("%d",&x);
printf("\n");
if(x==5) break;
switch(x)
{
case 1:user_game();break;
case 2:sys_game();break;
case 3:user_mess();break;
case 4:
{
FILE *fp;
char lss[30]="user\\";
strcat(lss,user);
strcat(lss,"game");
fp = fopen(lss,"w");
fclose(fp);
printf("清除完成\n");
break;
}
default: printf("输入的数字不正确\n");break;
}
if(x==1||x==2)
if(game_if)
{
printf("恭喜您挑战成功\n");
FILE *fp = NULL;
char file_game[25]="user\\";
strcat(file_game,user);
strcat(file_game,"game");
fp = fopen(file_game,"a+");
// fprintf(fp,"%s",strcat(target,"挑战成功\n"));
fseek(fp, 0, SEEK_END);
fwrite(strcat(target,"挑战成功\n"), strlen(strcat(target,"挑战成功\n")), 1, fp);
fclose(fp);
}
else
{
printf("很遗憾您挑战失败\n");
FILE *fp = NULL;
char file_game[25]="user\\";
strcat(file_game,user);
strcat(file_game,"game");
fp = fopen(file_game,"a+");
fprintf(fp,"%s",strcat(target,"挑战失败\n"));
fseek(fp, 0, SEEK_END);
fwrite(strcat(target,"挑战失败\n"), strlen(strcat(target,"挑战失败\n")), 1, fp);
fclose(fp);
}
}
// system("cls");
}
//Secret length calculation
void pass_str(int str)
{
printf("秘密长度:");
while(str--){
printf("*");
}
printf("\n");
}
//String comparison results
void str_compare(char b[1])
{
int i=0;
printf("显示:");
while(i<strlen(target))
{
if(target[i]==b[0])
{
printf("%c",b[0]);
}
else
{
printf("*");
}
i++;
}
printf("\n");
}
//Game start interface
void start_game()
{
system("cls");
printf("-----------------开始游戏-------------------\n");
pass_str(strlen(target));
printf("--------请开始进行猜测(一共八次机会)--------\n");
char temp[1];
char str_temp [20];
while(k++<8)
{
printf("请输入猜测字符(第%d次机会):\n",k);
scanf("%s",temp);
str_compare(temp);
printf("请输入猜测的秘密:\n");
scanf("%s",str_temp);
if(!strcmp(str_temp,target)){game_if=1;break;}
//if(i==7){break;}
A_SW(k);
}
}
//System mode
void sys_game()
{
MakeRandStr();
printf("系统已经随机生成了,即将开始游戏");
k=0;
start_game();
}
//user mode
void user_game()
{
printf("请手动输入一个秘密单词: \n");
scanf("%s",target);
printf("\n即将开始游戏");
k=0;
start_game();
}
//Main procedure
int main()
{
menu();
return 0;
}
实在不会的小伙伴也可以直接下载源码哦! https://download.csdn.net/download/qq_29737119/57028464添加链接描述
|