数字游戏(继Day 10)
主体:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include"mygetch.h"
#define MAX 51 //定义测试字母的最大长度
void help()
{
printf("\n****************************************");
printf("\n*输入过程中无法退出,输入错误则以_表示!*");
printf("\n按任意键开始测试,按下首字母时开始计时!*");
printf("\n****************************************");
}
void start(char *str)
{
srand((unsigned int)time(NULL));//随机种子
int i = 0;
for(i = 0;i < MAX-1;i++)//打印50个英文字符用于测试
{
*(str+i) = rand()%26+'a';
}
*(str+MAX-1) = '\0'; //写完字符后补上结束标志位
}
void on_game(char *str)
{
char ch;
int i = 0;
int count = 0;
time_t t_start,t_end;
system("clear"); //先清屏
printf("\n%s\n",str);
count = 0;
for(i = 0;i < MAX-1;i++)
{
ch = mygetch();
if(i == 0)
{
t_start = time(NULL);
}
if(ch == *(str+i))
{
printf("%c",ch);
count++;
}
else
{
printf("_"); //打印出错标志
}
}
t_end = time(NULL); //取结束时间
printf("\n正确输入!\n用时 %ld s\n",t_end - t_start);
printf("正确率%lf %%\n",(count*1.0/(MAX-1))*100);
printf("按下Esc退出,任意键继续!\n\n");
}
int main()
{
char str[MAX] = {0};
while(1)
{
help(); //游戏说明菜单
start(str); //产生随机字母
mygetch();
on_game(str); //开始游戏
char ch = mygetch();
if(ch == 27)//Esc的ascii为27
{
break;
}
system("clear"); //先清屏
}
return 0;
}
mygetch.c
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include "mygetch.h"
int mygetch(void)
{
struct termios oldt, newt;
int ch;
// 获取当前终端设置
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
// 修改终端设置 - 禁用回显和规范模式
newt.c_lflag &= ~(ICANON | ECHO);
// 应用新设置
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
// 获取字符
ch = getchar();
// 恢复原始终端设置
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
mygetch.h
#ifndef MYGETCH_H
#define MYGETCH_H
// 声明mygetch函数
int mygetch(void);
#endif