//头文件
#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; // 积分read
int main()
{
srand((unsigned)time(0));
startf();
return 0;
}
void readf()//read文件
{
FILE *file;
file = fopen("user.txt", "r");
if (file == NULL)
{
printf("没文件,已建,请重新打开一次");
FILE *fp;
fp = fopen("user.tnt", "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()
{
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();
gamestart();
break;
}
num = rand() % 3;
printf("电脑出了:%s\n", machine[num].name);
win(machine[num].i, mycard);
}
void wait()
{
printf("\n\n");
printf("......按任意键进行下一次对决......\n");
getch();
}
void win(int a, int b)
{
int sum;
sum = a + b;
if (sum == 8)
{
printf("***竟然是平局!***");
readf();
}
else
if (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");
}