#include<bits/stdc++.h>
#include<windows.h>
#define kd(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
#define ku(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 0:1)
using namespace std;
/*======================Stated Variablev======================*/
int n,cnt,score,rnd,FI=1,yyy,ori_bullet=5,ori_prop=4,live,blank;
deque <int> bullet;

string gun[22]={"",
"                                                                           ",
"                                                                           ",
"                                                                           ",
"                                                                           ",
"                                                                           ",
"                                                                           ",
"                                                                           ",
"                            .............................................=.",
"                       .=*--++++++++++++++++++++++++++++++++---------+-***=",//9
"   ....===****=.  .=*-++++++-++++--------------+++++++++++++-*******--*    ",
" .+++++++++++++++-++++++++*. *-=                ............               ",
"  -+++--+++++*=.     *++++=.                                               ",
"  =+++-+++**=        -+-+*                                                 ",
"   ++++=.           -++++=                                                 ",
"   =+-*             =====                                                  ",
"                                                                           ",
"                                                                           ",
"                                                                           ",
"                                                                           ",
};

string prop[]={"","strengthen","recover","control","continue","prediction"},cl="                                                                                        \n                                                                                        \n                                                                                        \n                                                                                        \n";

namespace player{
	int ori_heart=4,heart_p1,heart_p2;
	int ori_attack=1,attack_p1,attack_p2;
	int control_p1,control_p2;
	int score_p1,score_p2;
	int prop_p1[22],prop_p2[22];
}
using namespace player;

/*======================Stated Main======================*/
void re(int &x);
void wr(int x);
void cre(char ccc[]);
void cwr(char ccc[]);
void enter();
int read();
void write(int x);
void gotoxy(int x,int y);
void hide();
void srt();
void setting();
void puts_(string s);
void work();
void print();
void relow();
bool check();

/*======================Function Body======================*/ 
void re(int &x){scanf("%lld",&x);}
void wr(int x){printf("%lld ",x);}
void cre(char ccc[]){scanf("%s",ccc+1);}
void cwr(char ccc[]){printf("%s",ccc+1);}
void enter(){putchar('\n');}
void puts_(string s){cout<<s;}
int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch == '-') f=-1;ch=getchar();}
	while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return x*f;
}
void write(int x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10+'0');
}
void gotoxy(int x,int y){
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X=x,pos.Y=y;
	SetConsoleCursorPosition(handle,pos);
}
void hide(){
	CONSOLE_CURSOR_INFO cursor_info={1,0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void setting(){
	GET:
	system("cls");
	puts_("1.original heart:"),write(ori_heart),enter();
	puts_("2.original attack:"),write(ori_attack),enter();
	puts_("3.original bullet:"),write(ori_bullet),enter();
	puts_("4.original prop:"),write(ori_prop),enter();
	puts_("0.exit");
	Sleep(500);
	enter();
	while(1){
		if(kd('1')){
			puts_("1:");
			Sleep(500);
			while(1){
				if(kd('1')){
					++ori_heart;
					gotoxy(18,0);
					puts_("                ");
					gotoxy(17,0);
					write(ori_heart);
				}
				if(kd('2')){
					--ori_heart;
					if(ori_heart<1) ori_heart=1;
					gotoxy(18,0);
					puts_("                ");
					gotoxy(17,0);
					write(ori_heart);
				}
				if(kd('0')){
					goto GET;
				}
				Sleep(100);
			}
		}else if(kd('2')){
			puts_("2:");
			Sleep(500);
			while(1){
				if(kd('1')){
					++ori_attack;
					gotoxy(19,1);
					puts_("                ");
					gotoxy(18,1);
					write(ori_attack);
				}
				if(kd('2')){
					--ori_attack;
					if(ori_attack<1) ori_attack=1;
					gotoxy(19,1);
					puts_("                ");
					gotoxy(18,1);
					write(ori_attack);
				}
				if(kd('0')){
					goto GET;
				}
				Sleep(100);
			}
		}else if(kd('3')){
			puts_("3:");
			Sleep(500);
			while(1){
				if(kd('1')){
					++ori_bullet;
					gotoxy(19,2);
					puts_("                ");
					gotoxy(18,2);
					write(ori_bullet);
				}
				if(kd('2')){
					--ori_bullet;
					if(ori_bullet<1) ori_bullet=1;
					gotoxy(19,2);
					puts_("                ");
					gotoxy(18,2);
					write(ori_bullet);
				}
				if(kd('0')){
					goto GET;
				}
				Sleep(100);
			}
		}else if(kd('4')){
			puts_("4:");
			Sleep(500);
			while(1){
				if(kd('1')){
					++ori_prop;
					gotoxy(17,3);
					puts_("                ");
					gotoxy(16,3);
					write(ori_prop);
				}
				if(kd('2')){
					--ori_prop;
					if(ori_prop<1) ori_prop=1;
					gotoxy(17,3);
					puts_("                ");
					gotoxy(16,3);
					write(ori_prop);
				}
				if(kd('0')){
					goto GET;
				}
				Sleep(100);
			}
		}else if(kd('Y')){
			yyy^=1;
			puts_("something is ");
			puts((yyy)?"open":"close");
			Sleep(1000);
			goto GET;
		}else if(kd('0')) return ;
	}
}
void srt(){
	while(++rnd){
		puts_("round:"),write(rnd),enter();
		system("pause");
		heart_p1=heart_p2=ori_heart;
		attack_p1=attack_p2=ori_attack;
		live=blank=0;
		relow();
		while(!bullet.empty()){
			work();
//			write(FI),write(control_p1),write(control_p2);system("pause");
			if(FI==1 && control_p1<=0) FI=0,control_p1=0;
			else if(FI==0 && control_p2<=0) FI=1,control_p2=0;
			if(FI==1) control_p1=max(0,control_p1-1);
			else control_p2=max(0,control_p2-1);
			if(heart_p2<=0){
				puts("p1 win!!!");
				score_p1++;
				FI=1;
				control_p1=max(0,control_p1-1);
				break;
			}else if(heart_p1<=0){
				puts("p2 win!!!");
				score_p2++;
				FI=0;
				control_p2=max(0,control_p2-1);
				break;
			}
		}
	}
}
bool check(){
	system("cls");
	for(int i=1;i<=19;i++) cout<<gun[i],enter();
	for(int i=1;i<=4;i++){
		gotoxy(78+2*i,8);
		puts_("--");
		Sleep(250);
		gotoxy(78+2*i,8);
		puts_("                      ");
	}
	if(!bullet.front()){
		blank--;
		bullet.pop_front();
		puts("nothing");
		gotoxy(0,18);
		system("pause");
		return 0;
	}else{
		live--;
		bullet.pop_front();
		puts("hurt");
		gotoxy(0,18);
		system("pause");
		return 1;
	}
}
void relow(){
	for(int i=1;i<=ori_bullet;i++){
		int op=(rand()%2+rand()%2+1)%2,x=rand()%2;
		if(op) live++;
		else blank++;
		if(x) bullet.push_front(op);
		else bullet.push_back(op);
	}
	//1 = live ammunition
	//0 = blank ammunition
	for(int i=1;i<=ori_prop;i++){
		int x=rand()%5+1,y=rand()%5+1;
		if(FI==1) prop_p1[x]++,prop_p2[y]++;
		else prop_p2[x]++,prop_p1[y]++;
	}
}
void work(){
	while(1){
		print();
		while(1){
			gotoxy(0,22);
			puts_(cl);
			gotoxy(0,22);
//			cout<<FI;
			if(kd('1')){
				puts_("Are you sure you want to use:");
				puts_(prop[1]);
				puts(" (y/n)");
				while(1){
					Sleep(100);
					if(kd('Y')){
						if(FI==1){
							if(!prop_p1[1]){
								puts_("You don`t have enough ");
								puts_(prop[1]);
							}else{
								prop_p1[1]--;
								attack_p1++;
								gotoxy(13,15);
								puts_("                      ");
								gotoxy(13,15);
								write(prop_p1[1]);
								gotoxy(0,22);
								puts_(cl);
							}
						}else{
							if(!prop_p2[1]){
								puts_("You don`t have enough ");
								puts_(prop[1]);
							}else{
								prop_p2[1]--;
								attack_p2++;
								gotoxy(13,15);
								puts_("                      ");
								gotoxy(13,15);
								write(prop_p2[1]);
								gotoxy(0,22);
								puts_(cl);
							}
						}
						break;
					}else if(kd('N')){
						break;
					}
				}
			}else if(kd('2')){
				puts_("Are you sure you want to use:");
				puts_(prop[2]);
				puts(" (y/n)");
				while(1){
					Sleep(100);
					if(kd('Y')){
						if(FI==1){
							if(!prop_p1[2]){
								puts_("You don`t have enough ");
								puts_(prop[2]);
							}else{
								prop_p1[2]--;
								heart_p1++;
								gotoxy(14,14);
								puts_("                      ");
								gotoxy(14,14);
								write(heart_p1);
								gotoxy(10,16);
								puts_("                      ");
								gotoxy(10,16);
								write(prop_p1[2]);
								gotoxy(0,22);
								puts_(cl);
							}
						}else{
							if(!prop_p2[2]){
								puts_("You don`t have enough ");
								puts_(prop[2]);
							}else{
								prop_p2[2]--;
								heart_p2++;
								gotoxy(14,14);
								puts_("                      ");
								gotoxy(14,14);
								write(heart_p2);
								gotoxy(10,16);
								puts_("                      ");
								gotoxy(10,16);
								write(prop_p2[2]);
								gotoxy(0,22);
								puts_(cl);
							}
						}
						break;
					}else if(kd('N')){
						break;
					}
				}
			}else if(kd('3')){
				puts_("Are you sure you want to use:");
				puts_(prop[3]);
				puts(" (y/n)");
				while(1){
					Sleep(100);
					if(kd('Y')){
						if(FI==1){
							if(!prop_p1[3]){
								puts_("You don`t have enough ");
								puts_(prop[3]);
							}else{
								prop_p1[3]--;
								control_p1+=2;
								gotoxy(10,17);
								puts_("                      ");
								gotoxy(10,17);
								write(prop_p1[3]);
								gotoxy(0,22);
								puts_(cl);
							}
						}else{
							if(!prop_p2[3]){
								puts_("You don`t have enough ");
								puts_(prop[3]);
							}else{
								prop_p2[3]--;
								control_p2+=2;
								gotoxy(10,17);
								puts_("                      ");
								gotoxy(10,17);
								write(prop_p2[3]);
								gotoxy(0,22);
								puts_(cl);
							}
						}
						break;
					}else if(kd('N')){
						break;
					}
				}
			}else if(kd('4')){
				puts_("Are you sure you want to use:");
				puts_(prop[4]);
				puts(" (y/n)");
				while(1){
					Sleep(100);
					if(kd('Y')){
						if(FI==1){
							if(!prop_p1[4]){
								puts_("You don`t have enough ");
								puts_(prop[4]);
							}else{
								prop_p1[4]--;
								gotoxy(11,18);
								puts_("                      ");
								gotoxy(11,18);
								write(prop_p1[4]);
								gotoxy(0,22);
								puts_(cl);
								gotoxy(0,22);
								puts_((bullet.front())?"live ammunition":"blank ammunition");
								bullet.pop_front();
								system("pause");
								if(bullet.empty()) return;
							}
						}else{
							if(!prop_p2[4]){
								puts_("You don`t have enough ");
								puts_(prop[4]);
							}else{
								prop_p2[4]--;
								gotoxy(11,18);
								puts_("                      ");
								gotoxy(11,18);
								write(prop_p2[4]);
								gotoxy(0,22);
								puts_(cl);
								gotoxy(0,22);
								puts_((bullet.front())?"live ammunition":"blank ammunition");
								bullet.pop_front();
								system("pause");
								if(bullet.empty()) return;
							}
						}
						break;
					}else if(kd('N')){
						break;
					}
				}
			}else if(kd('5')){
				puts_("Are you sure you want to use:");
				puts_(prop[5]);
				puts(" (y/n)");
				while(1){
					Sleep(100);
					if(kd('Y')){
						if(FI==1){
							if(!prop_p1[5]){
								puts_("You don`t have enough ");
								puts_(prop[5]);
							}else{
								prop_p1[5]--;
								gotoxy(0,22);
								puts_(cl);
								gotoxy(0,22);
								puts_((bullet.front())?"live ammunition":"blank ammunition");
								system("pause");
								gotoxy(13,19);
								puts_("                      ");
								gotoxy(13,19);
								write(prop_p1[5]);
								gotoxy(0,22);
								puts_(cl);
							}
						}else{
							if(!prop_p2[5]){
								puts_("You don`t have enough ");
								puts_(prop[5]);
							}else{
								prop_p2[5]--;
								gotoxy(0,22);
								puts_(cl);
								gotoxy(0,22);
								puts_((bullet.front())?"live ammunition":"blank ammunition");
								system("pause");
								gotoxy(13,19);
								puts_("                      ");
								gotoxy(13,19);
								write(prop_p2[5]);
								gotoxy(0,22);
								puts_(cl);
							}
						}
						break;
					}else if(kd('N')){
						break;
					}
				}
			}else if(kd('A')){
				puts_("Are you sure you want to attack");
				puts((FI==1)?"p1":"p2");
				puts(":(y/n)");
				while(1){
					Sleep(100);
					if(kd('Y')){
						if(!check()){
							if(FI==1) control_p1++;
							else control_p2++;
							return ;
						}
						if(FI==1){
							heart_p1-=attack_p1;
							attack_p1=1;
							control_p1++;
						}else{
							heart_p2-=attack_p2;
							attack_p2=1;
							control_p2++;
						}
						return ;
					}else if(kd('N')) break;
				}
			}else if(kd('B')){
				puts_("Are you sure you want to attack");
				puts((FI==1)?"p2":"p1");
				puts(":(y/n)");
				while(1){
					Sleep(100);
					if(kd('Y')){
						if(!check()){
							if(FI==1){
								control_p1--;
							}
							if(FI==0){
								control_p2--;
							}
							return ;
						}
						if(FI==1){
							heart_p2-=attack_p1;
						}else{
							heart_p1-=attack_p2;
						}
						return ;
					}else if(kd('N')) break;
				}
			}
		}
	}	
}
void print(){
	system("cls");
	puts_("score_p1 : score_p2 = ");
	write(score_p1),puts_(" : "),write(score_p2),enter();
	puts((FI==1)?"player2:":"player1");
	puts_((FI==1)?"player2_heart:":"player1_heart:");
	write((FI==1)?heart_p2:heart_p1);
	enter();
	for(int i=1;i<=5;i++){
		write(i),puts_(".");
		puts_(prop[i]+":");
		write(prop_p2[i]);enter();
	}
	puts("========================================================================================");
	puts_("live:"),write(live);puts_("        blank:"),write(blank);enter();enter();
	puts("strengthen:Ð¡µ¶  recover:ÏãÑÌ  control:ÊÖîí  continue:Æ¡¾Æ  prediction:·Å´ó¾µ");
	puts("========================================================================================");
	puts((FI==1)?"player1:":"player2:");
	puts_((FI==1)?"player1_heart:":"player2_heart:");
	write((FI==1)?heart_p1:heart_p2);
	enter();
	for(int i=1;i<=5;i++){
		write(i),puts_(".");
		puts_(prop[i]+":");
		write(prop_p1[i]);enter();
	}
	puts_("a.attack_");
	puts((FI==1)?"p1":"p2");
	puts_("b.attack_");
	puts((FI==1)?"p2":"p1");
}

/*======================Main Body======================*/
signed main(){
    hide();
    srand(time(NULL));
    while(1){
    	system("cls");
	    puts("1.start");
	    puts("2.setting");
	    while(1){
	    	if(kd('1')) srt();
	    	if(kd('2')) setting();
	    	if(kd('0')) break;
		}
	}
}
