/************************************************************
单词打字练习
回车或者空格:审核输入 ESC:退出 F1~F12:暂停 退格键:退格
输入个数等同字符长度时开始审核?
作者:cdmalcl
日期:2007.4.8
************************************************************/
#include <fcntl.h>
#include <math.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <graphics.h>
#include <dos.h>
#define ESC 27
#define ENTER 13
#define BACKSPACE 8
#define SPACE 32
#define F1 0
#define IO_BKCOLOR 6
#define S_COLOR 3
#define SPEED 55
#define MAX_MW_LEN 20
typedef unsigned long LONG;
struct File{
FILE *fp;
char WordFileName[20];
LONG Len;
}WFile;/* 定义文件数据类型 */
void Init(); /* 程序数据和模式初始化 */
void OpenWordFile(); /* 打开英语单词库 */
int END(int kind); /* 程序结束模式 */
int DrawBKView(); /* 绘制程序背景 */
int Game(); /* 游戏过程 */
int Process(); /* 输入过程 */
int MakeWord(char Word[]); /* 产生待输入单词 */
void OutMakeWord(char Word[] ,int Word_Len); /* 输出带输入单词 */
void OutCNum(int CNum); /* 输出正确个数 */
int CheckWord(char Word[] ,char InputWord[] ,int Word_Len); /* 核对单词 */
void ClearView(int x0 ,int y0 ,int x1 ,int y1 ,int color); /* 清楚或绘制区域 */
void OutSorce(); /* 输出积分 */
int StopTime(int Space); /* 时间控制 */
int Progress(int Len ,int color); /* 进度条控制 */
void OutEndSorce(LONG time); /* 输出最终成绩 */
int Sorce = 0; /* 总成绩变量 */
LONG NewT = 1 ,OldT = 0; /* 时间控制变量 */
LONG AllInputWord = 0; /* 所有待输入单词字母个数 */
/* 主函数 */
int main(void)
{
Init();
Game();
END(0);
}
void Init()
{
int gd = DETECT ,gm = 0;
initgraph(&gd ,&gm ,"");
srand((unsigned)time(NULL));
strcpy(WFile.WordFileName ,"word.txt");
OpenWordFile();
DrawBKView();
Sorce = 0;
NewT = 1;
OldT = 0;
AllInputWord = 0;
}
void OpenWordFile()
{
if((WFile.fp = fopen(WFile.WordFileName ,"rb")) == NULL)
{
puts("Please check your word file!\nThe file name is:");
puts(WFile.WordFileName);
getch();
END(1);
}
rewind(WFile.fp); /* 获得文件长度 */
fseek(WFile.fp ,0l ,SEEK_END);
WFile.Len = ftell(WFile.fp);
rewind(WFile.fp);
}
int END(int kind)
{
fclose(WFile.fp);
switch(kind)
{
case 0:exit(0);
case 1:exit(1);
default:;
}
}
int DrawBKView()
{
rectangle(0 ,0 ,getmaxx() ,getmaxy());
setfillstyle(1 ,12);
floodfill(1 ,1 ,getcolor());
}
int Game()
{
LONG TimeLen = 0 ,OldTimeSave = 0;
int MakeWordLen = 0;
char Num_Char[6];
OldTimeSave = biostime(0 ,OldTimeSave); /* 开始时间保存 */
while(MakeWordLen < MAX_MW_LEN)
{
ClearView(180 ,100 ,260 ,120 ,IO_BKCOLOR);
ltoa(MAX_MW_LEN-MakeWordLen ,Num_Char ,10);
outtextxy(182 ,102 ,Num_Char);
Sorce+=Process();
OutSorce();
MakeWordLen++;
}
TimeLen = biostime(0 ,OldTimeSave)-OldTimeSave; /* 结束时间计算 */
OutEndSorce(TimeLen);
while(getch() != ESC);
}
int Process()
{
char Word[21] ,Wr[1]=" " ,InputWord[10] = "";
int Word_Len;
int i;
char InKey = 0;
int CNum = 0;
Word_Len = MakeWord(Word); /* 获得带输入单词及其长度 */
AllInputWord += Word_Len;
OutMakeWord(Word ,Word_Len);
ClearView(180 ,192 ,500 ,226 ,IO_BKCOLOR);
Wr[1] = 0;
i = 0;
while(StopTime(SPEED)) /* 带输入控制 */
{
if(kbhit())
{
InKey = bioskey(0);
if(i >= Word_Len || InKey == ENTER ||InKey == SPACE)
{
InputWord[i] = 0;
break;
}
if(InKey <= 'z'&&InKey >= 'a')
{
Wr[0] = InKey;
InputWord[i] = InKey;
outtextxy(i++*32+200 ,200 ,Wr);
}
else
{
switch(InKey)
{
case ESC:END(0);
case BACKSPACE:i--;
ClearView(i*32+200 ,200 ,(i+1)*32+200 ,216 ,IO_BKCOLOR);
break;
}
}
if(InKey == F1)
{
bioskey(0);
StopTime(0);
break;
}
fflush(stdin);
}
}
CNum = CheckWord(Word ,InputWord ,i);
OutCNum(CNum);
return CNum;
}
int MakeWord(char Word[])
{
LONG WordSeek = 0;
int i;
char getfc;
WordSeek = random(WFile.Len-1);
for(i = 0;i < 20;i++)
{
if(WordSeek-i < 1)
{
rewind(WFile.fp);
break;
}
fseek(WFile.fp ,WordSeek-i ,SEEK_SET);
getfc = fgetc(WFile.fp);
if(getfc == 10)
{
break;
}
}
for(i = 0;i < 20;i++)
{
Word[i] = fgetc(WFile.fp);
if(Word[i] == 13 || Word[i] == -1)
{
Word[i] = 0;
return i;
}
}
}
void OutMakeWord(char Word[] ,int Word_Len)
{
char Wr[1]=" ";
int i;
ClearView(180 ,142 ,500 ,176 ,IO_BKCOLOR);
Wr[1] = 0;
for(i = 0;i < Word_Len;i++)
{
Wr[0] = Word[i];
outtextxy(i*32+200 ,150 ,Wr);
}
}
void OutCNum(int CNum)
{
char CuChar[6];
ClearView(180 ,240 ,228 ,276 ,IO_BKCOLOR);
itoa(CNum ,CuChar ,10);
outtextxy(200 ,250 ,CuChar);
}
int CheckWord(char Word[] ,char InputWord[] ,int Len)
{
int i;
int CNum = 0;
OldT = NewT;
for(i = 0;i < Len;i++)
{
if(Word[i] == InputWord[i])
{
CNum++;
}
}
return CNum;
}
void ClearView(int x0 ,int y0 ,int x1 ,int y1 ,int color)
{
int i ,j;
if(x0 > x1)
{
i = x0;
x0 = x1;
x1 = i;
}
if(y0 > y1)
{
i = y0;
y0 = y1;
y1 = i;
}
for(i = x0;i < x1;i++)
for(j = y0;j < y1;j++)
{
putpixel(i ,j ,color);
}
}
void OutSorce()
{
char CuChar[6];
ClearView(180 ,292 ,228 ,328 ,IO_BKCOLOR);
itoa(Sorce ,CuChar ,10);
outtextxy(200 ,300 ,CuChar);
}
int StopTime(int Space)
{
long Pro = 0;
Pro = ((NewT-OldT)*10)/SPEED; /* 进度计算 */
Progress(Pro ,5);
if((NewT = biostime(0 ,NewT)) > OldT+Space)
{
OldT = NewT;
return 0;
}
return 1;
}
int Progress(int Len ,int color)
{
if(Len == 10||Len == 0)
ClearView(180 ,350 ,182+92 ,370 ,IO_BKCOLOR);
else
ClearView(172+Len*10 ,352 ,172+Len*10+10 ,368 ,color);
}
void OutEndSorce(LONG time)
{
char *Num_Char;
Num_Char = (char *)calloc(10,sizeof(char));
ClearView(180 ,420 ,260 ,445 ,S_COLOR);
ltoa(time ,Num_Char ,10);
outtextxy(190 ,426 ,Num_Char);
Num_Char = (char *)calloc(10,sizeof(char));
ClearView(280 ,420 ,360 ,445 ,S_COLOR);
itoa(Sorce ,Num_Char ,10);
outtextxy(290 ,426 ,Num_Char);
Num_Char = (char *)calloc(10,sizeof(char));
ClearView(380 ,420 ,460 ,445 ,S_COLOR);
ltoa(Sorce*100l/AllInputWord ,Num_Char ,10);
Num_Char[strlen(Num_Char)] = '%';
Num_Char[strlen(Num_Char)] = 0;
outtextxy(390 ,426 ,Num_Char);
}
[此贴子已经被作者于2007-4-8 18:42:30编辑过]