#ifdef _VS_WALK
#include "wkClassDefine.h"
#endif

#include "wcDialogConstant.h"
/*	ӷ */

#define _Digit1 100
#define _Digit2 101
#define _Answer 102
#define _Tip	103
#define _CurTime	104
#define _BestTime	105
#define _BestAnswer	106

//
void FillDigit(wkDialog &dlg, int base)
{
	dlg.setText(_Digit1, toString(abs(random())%base));
	dlg.setText(_Digit2, toString(abs(random())%base));
	dlg.setText(_Answer, "");
	dlg.show(_Answer,4);
}

//Ի
void CreateDlg(wkDialog &dlg)
{
	int x=20, y=10, w=40, h=14;
	dlg.create("ӷ");

	dlg.addCtrl(_Digit1, wdCtrlEdit, x, y, w, h, ES_READONLY|ES_RIGHT);
	y+=20; dlg.addCtrl("+", 8, y);
	dlg.addCtrl(_Digit2, wdCtrlEdit, x, y, w, h, ES_READONLY|ES_RIGHT);
	dlg.addCtrl("----------------", 8, y+12);
	y+=20; dlg.addCtrl("=", 8, y);
	dlg.addCtrl(_Answer, wdCtrlEdit, x, y, w, h, ES_CENTER);
	
	x+=w+6; y=10;
	//ǰʱ
	dlg.addCtrl(_CurTime, wdCtrlEdit, x, y, w*2+16, h, ES_READONLY|ES_CENTER);
	//ָʾ
	y+=20; dlg.addCtrl(_Tip, wdCtrlEdit, x, y, w*2+16, h, ES_READONLY|ES_CENTER);
	//ʱ(10ȷʱٵĽ)
	y+=20; dlg.addCtrl(_BestTime, wdCtrlEdit, x, y, w+8, h, ES_READONLY|ES_CENTER);
	//ȷ
	x+=w+16; dlg.addCtrl(_BestAnswer, wdCtrlEdit, x, y, w, h, ES_READONLY|ES_CENTER);

	dlg.setAction(wdIdOk, wdMsgBnClicked);
	dlg.show();
	randomize();
	FillDigit(dlg, 100);
}

void main()
{
	wkDialog dlg;
	int ctrlId;
	CreateDlg(dlg);

	int clocksPerSec, beginClock;
	beginClock=clock(clocksPerSec);
	int year, mon, mday, hour, min, sec=-1, now;
	string s;

	while(1)	//ѭȴģʽ
	{
		int act=dlg.peekAction(ctrlId);
		if (act == -1)//ûֱֹԻ
			break;

		//õǰʱ--------------------------------
		timeTm(time(), year, mon, mday, hour, min, now);
		if (sec<0 || now!=sec)
		{
			s.format("%d-%d-%d %02d:%02d:%02d", year, mon, mday, hour, min, now);
			dlg.setText(_CurTime, s);
			sec=now;
		}

		//Ӧ"ȷ"Ϣ------------------------------
		if (act && ctrlId==wdIdOk)
		{
			s=dlg.getText(_Tip);
			if (s.isEmpty())	//ӻش1⿪ʼʱ
				beginClock=clock();
			//ʾԴʶ
			bool right = (atoi(dlg.getText(_Answer)) ==
					atoi(dlg.getText(_Digit1)) + atoi(dlg.getText(_Digit2)));
			dlg.setText(_Tip, s+(right? "":""));

			//10ȷʺʱ
			s=dlg.getText(_Tip);
			if (s.getLength()==20)
			{
				//10ʱ
				double span=1.0*(clock()-beginClock)/clocksPerSec;
				//ȷ
				for (int i=0, correct=0; i<s.getLength(); i+=2)
					if (s.mid(i,2)=="") correct++;
				//ȷ
				int lastCorrect=atoi(dlg.getText(_BestAnswer));
				if (correct>=lastCorrect)
				{
					s.format("%d", correct); dlg.setText(_BestAnswer, s);
					//дʱ
					if (correct>lastCorrect || span<atof(dlg.getText(_BestTime)))
					{
						s.format("%.2lf", span); dlg.setText(_BestTime, s);
					}
				}
				//մʾ
				dlg.setText(_Tip, "");
			}
			//
			FillDigit(dlg, 100);
		}
	}
}