#include<bits/stdc++.h>
#include<windows.h>
#include<Windows.h>
#include<conio.h> 
#include<time.h>
using namespace std;
//--------gotoxy函数-----------
void gotoxy(int x,int y)//注意:x为列,y为行
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}
//--------隐藏光标-----------
void HideCursor() 
{ 
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
 	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); 
}
//--------int转换为string-----------
string inttostr(int num) {
    std::stringstream ss;
    ss << num;
    std::string str = ss.str();
    return str ;
}
//--------string转换为int-----------
int strtoint(string s)
{
	int num1 = std::atoi(s.c_str());
	return num1;
}
bool flag;
string underline(string s)
{
	HideCursor();
	system("cls");
	while(1)
	{
		int end=clock();
		gotoxy(0,0);
		cout<<s;
		gotoxy(0,0);
		int len=s.length();
		for(int i=1;i<=len;i++)
		{
			cout<<"-";
		}
		if(kbhit())
		{
			char c=getch();
			if(c==27)
			{
				flag=false;
				return s;
				
			}
			
		}
		Sleep(0);
	 } 
}
int r(int a,int b)
{
	
	return rand()%(b-a+1)+a;
}
void still_open()
{
    HWND hwnd = GetConsoleWindow();
	HMENU hmenu = GetSystemMenu(hwnd, false);
	RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
	LONG style = GetWindowLong(hwnd, GWL_STYLE);
	style &= ~(WS_MINIMIZEBOX);
	SetWindowLong(hwnd, GWL_STYLE, style);
	SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_HIDEWINDOW);
	ShowWindow(hwnd, SW_SHOWNORMAL);
	DestroyMenu(hmenu);
	ReleaseDC(hwnd, NULL);
}

string s; 
int main()
{
	srand(time(0));
	system("title 眼力大挑战");
	flag=true;
	while(1)
	{
		if(!flag)
		{
			system("cls");
			cout<<"请输入上一题的答案:";
			string ss;
			while(1)
			{
				char c=getch();
				if(c==8)
				{
					cout<<(char)8<<" "<<(char)8;
					continue;
				}
				int q;
				if(c=='\r')break;
				else if(c>='0'&&c<='9')
				{
					q=c-'0';
				}
				else continue; 
				ss+=inttostr(q);
				cout<<q;
			}
			if(s==ss)
			{
				cout<<"\n答对了!\n";
				system("pause");
			}
			if(s!=ss)
			{
				cout<<"\n答错了!\n";
				system("pause");
			}
			flag=true;
			continue;
		}
		int tims;
		system("cls");
		cout<<"欢迎来到眼力大挑战(已玩"<<tims<<"次)"<<"\n";
		system("cls");
		cout<<"请输入字符串长度:(1~20,建议10)";
		int len;
		cin>>len;
		gotoxy(0,1);
		cout<<"难度为:"<<len<<"\n";
		s=""; 
		if(len<1||len>20)len=10;
		for(int i=0;i<len;i++)
		{
			int a=r(0,9);
			Sleep(10);
			s+=inttostr(a);
		}
		system("pause");
		system("cls");
		s=underline(s);
		system("cls"); 
		tims++;
	}
	
}