C语言学完了之后总是感觉做不出什么有意思的东西,今天我们就来做一个好玩的小游戏试试。
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include <windows.h>void win(int a, int b);
void wait();
void softgame();
void lefttime();
void gamestart();
void readf();
void writef();
void startf();
void zhifen();struct game
{int i;char name[10];
};
struct game machine[3] = { {1,"石头"},{2,"剪刀"},{3,"布"} };char nname[] = ""; // write
int zfen; // 积分write
char name[18]; // read
int zhif; // 积分readint main()
{srand((unsigned)time(0));startf();return 0;
}void zhifen()//名字积分
{printf("游戏前请输入名字(不能空格)和积分\n");printf("输入名:");scanf("%s", &nname);printf("输入积分:");scanf("%d", &zfen);
}void readf()//read文件
{FILE *file;file = fopen("user.txt", "r");if (file == NULL){printf("没文件,已建,请重新打开一次");FILE *fp;fp = fopen("user.txt", "w");fclose(fp);getch();return;}if (fscanf(file, "%s %d", &name, &zhif) != EOF){fscanf(file, "%s %d", &name, &zhif);printf("\n");printf("名:%s 积分:%d", name, zhif);printf("\n");}else {printf("文档为空\n");zhifen();writef();readf();gamestart();return;}if (zhif < 10){printf("任意键继续\n");getch();system("cls");printf("积分为0\n");zhifen();writef();readf();gamestart();return;}fclose(file);gamestart();
}void writef()//write
{FILE *file;file = fopen("user.txt", "w");if (file == NULL){printf("不能打开文件!");return;}fprintf(file, "%s %d", nname, zfen);fclose(file);
}void startf()
{//writef();readf();
}
void gamestart()//游戏开始
{while (1){//wait();printf("--------------------------------------\n");printf("和\"C语言狗\"玩石头、剪刀、布游戏\n\n");softgame();}
}void softgame()//玩家选出
{char k;int mycard;int num;printf("你选择出1石头2剪刀3布:");scanf("%s", &k);switch (k){case '1':mycard = 7;printf("你出了:石头!\n");break;case '2':mycard = 6;printf("你出了:剪刀!\n");break;case '3':mycard = 5;printf("你出了:布!\n");break;default:system("cls");lefttime();/* return; */gamestart();break;}num = rand() % 3;printf("电脑出了:%s\n", machine[num].name);win(machine[num].i, mycard);/*printf("\n\n");printf("......按任意键进行下一次对决......\n");getch();system("cls");*/
}void wait()
{printf("\n\n");printf("......按任意键进行下一次对决......\n");getch();// system("cls");
}void win(int a, int b)//判定胜负
{int sum;sum = a + b;if (sum == 8){printf("***竟然是平局!***");readf();}elseif (sum == 7 || sum == 10){printf("***很遗憾,\"C语言狗\"胜了!***");zfen = zhif - 10;strcpy(nname, name);writef();readf();}else{printf("***恭喜,你胜了!***");zfen = zhif + 10;strcpy(nname, name);writef();readf();}
}
void lefttime()
{int i = 3;for (int a = 0; a < 3; a++){printf("选择错误,请输入1或2或3选择\n");printf("%d秒后继续操作", i);i--;Sleep(1000);system("cls");}printf("\n");printf("名:%s 积分:%d", name, zhif);printf("\n");
}
这个就可以玩了,大家可以试试看哦!!!
完
关注它,
你就可以得到
很多你想要的C/C++
开发工具及学习视频资料