#include <iostream.h>
#include <windows.h>

const int screen_width=80;
const int screen_height=40;
const int height = 30;
const int width = 20;
const int no_blocks=6;
int play[height][width];

struct matrix16
{
	int matrix[4][4];
};

struct shape
{
	int rotation[4][4][4];
	int x;
	int y;
	int colour;
	bool active;
};

struct shape2
{
	struct shape matrix[10];
};

void cls()
{
	COORD const topleft = {0,0};
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	DWORD len;FillConsoleOutputCharacter(console, ' ', 4000, topleft, & len);
	SetConsoleCursorPosition(console, topleft);
	return;
}
void locate(int x, int y)
{
	COORD const topleft = {x,y};
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(console, topleft);
	return;
}
void screensize()
{
	COORD const size = {screen_width,screen_height};

	_CONSOLE_CURSOR_INFO ccinfo;
	ccinfo.dwSize = 99;
	ccinfo.bVisible = 1;
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(console, &ccinfo);
	SetConsoleScreenBufferSize(console, size);
	SetConsoleTitle("Super PayRoll Advanced!!! (SPRA).");
	return;
}

struct shape2 init_blocks()
{
	struct shape2 temp;
	struct shape temp1[no_blocks]=
	{
		{
			{
				{
					{0,0,1,0},
					{0,0,1,0},
					{0,0,1,0},
					{0,0,1,0}
				},
				{
					{0,0,0,0},
					{1,1,1,1},
					{0,0,0,0},
					{0,0,0,0}
				},
				{
					{0,0,1,0},
					{0,0,1,0},
					{0,0,1,0},
					{0,0,1,0}
				},
				{
					{0,0,0,0},
					{1,1,1,1},
					{0,0,0,0},
					{0,0,0,0}
				}
			}, 0, 0, 0, true
		},
		{
			{
				{
					{0,1,0,0},
					{0,1,0,0},
					{1,1,0,0},
					{0,0,0,0}
				},
				{
					{0,0,0,0},
					{1,1,1,0},
					{0,0,1,0},
					{0,0,0,0}
				},
				{
					{0,1,1,0},
					{0,1,0,0},
					{0,1,0,0},
					{0,0,0,0}
				},
				{
					{1,0,0,0},
					{1,1,1,0},
					{0,0,0,0},
					{0,0,0,0}
				}
			}, 0, 0, 0, true
		},
		{
			{
				{
					{0,0,0,0},
					{0,1,0,0},
					{1,1,1,0},
					{0,0,0,0}
				},
				{
					{0,0,0,0},
					{0,1,0,0},
					{1,1,0,0},
					{0,1,0,0}
				},
				{ 
					{0,0,0,0},
					{0,0,0,0},
					{1,1,1,0},
					{0,1,0,0}
				},
				{
					{0,0,0,0},
					{0,1,0,0},
					{0,1,1,0},
					{0,1,0,0}
				}
			}, 5, 5, 0, true
		},
		{
			{
				{
					{0,0,0,0},
					{0,1,1,0},
					{0,1,1,0},
					{0,0,0,0}
				},
				{
					{0,0,0,0},
					{0,1,1,0},
					{0,1,1,0},
					{0,0,0,0}
				},
				{
					{0,0,0,0},
					{0,1,1,0},
					{0,1,1,0},
					{0,0,0,0}
				},
				{
					{0,0,0,0},
					{0,1,1,0},
					{0,1,1,0},
					{0,0,0,0}
				}
			}, 0, 0, 0, true
		},
		{
			{
				{
					{0,0,0,0},
					{1,1,0,0},
					{0,1,1,0},
					{0,0,0,0}
				},
				{
					{0,0,0,0},
					{0,0,1,0},
					{0,1,1,0},
					{0,1,0,0}
				},
				{
					{0,0,0,0},
					{1,1,0,0},
					{0,1,1,0},
					{0,0,0,0}
				},
				{
					{0,0,0,0},
					{0,0,1,0},
					{0,1,1,0},
					{0,1,0,0}
				}
			}, 0, 0, 0, true
		},
		{
			{
				{
					{0,0,0,0},
					{0,0,1,1},
					{0,1,1,0},
					{0,0,0,0}
				},
				{
					{0,0,0,0},
					{0,1,0,0},
					{0,1,1,0},
					{0,0,1,0}
				},
				{
					{0,0,0,0},
					{0,0,1,1},
					{0,1,1,0},
					{0,0,0,0}
				},
				{
					{0,0,0,0},
					{0,1,0,0},
					{0,1,1,0},
					{0,0,1,0}
				}
			}, 0, 0, 0, true
		}
	};

	for(int i=0;i<no_blocks;i++)
	{
		temp.matrix[i]=temp1[i];
	}
	return temp;
}

struct matrix16 rotate(struct matrix16 first, struct matrix16 second)
{
	struct matrix16 answer;

	for(int y=0;y<4;y++)
	{
		for(int x=0;x<4;x++)
		{
		answer.matrix[x][y] = (first.matrix[x][y]*second.matrix[x][0])+(first.matrix[x+1][y]*second.matrix[x][1])+(first.matrix[x+2][y]*second.matrix[x][2])+(first.matrix[x+3][y]*second.matrix[x][3]);
		}
	}

	return answer;
}

void draw(struct shape temp, int i, int a, int b)
{
	
	if(i==99)
	{
	for(int y=0;y<4;y++)
	{
		for(int x=0;x<4;x++)
		{
			locate(x+a+(screen_width/2)-((width)/2),y+b+(screen_height/2)-((height)/2));cout<<" "<<endl;
		}
	}
	}
	else
	{
	for(int y=0;y<4;y++)
	{
		for(int x=0;x<4;x++)
		{
			if(temp.rotation[i][y][x]==1)
			{
				locate(x+a+(screen_width/2)-((width)/2),y+b+(screen_height/2)-((height)/2));cout<<char(219)<<endl;
			}
			else
			{
				locate(x+a+(screen_width/2)-((width)/2),y+b+(screen_height/2)-((height)/2));cout<<" "<<endl;
			}
		}
	}
	}
}

bool check(struct shape temp, int i, int a, int b, int p)
{
	bool go=false;
	case(p)
	{
case 1:
	for(int y=0;y<4;y++)
	{
		for(int x=0;x<4;x++)
		{
			if(player1.play[x+a][y+b] == 1 && temp.rotation[i][y][x]==1)
			{
				go=false;
				return false;
			}
			else
			{
				go=true;
			}
		}
	}
	return go;
	break;
case 2:
	for(int y=0;y<4;y++)
	{
		for(int x=0;x<4;x++)
		{
			if(player2.play[x+a][y+b] == 1 && temp.rotation[i][y][x]==1)
			{
				go=false;
				return false;
			}
			else
			{
				go=true;
			}
		}
	}
	return go;

	break;
default:
	for(int y=0;y<4;y++)
	{
		for(int x=0;x<4;x++)
		{
			if(player1.play[x+a][y+b] == 1 && temp.rotation[i][y][x]==1)
			{
				go=false;
				return false;
			}
			else
			{
				go=true;
			}
		}
	}
	return go;

	break;
	}
}

void draw_crap()
{
	locate((screen_width/2)-((width+4)/2), (screen_height/2)-((height+2)/2));
	cout<<char(201)<<endl;
	locate((screen_width/2)+((width+4)/2), (screen_height/2)-((height+2)/2));
	cout<<char(187)<<endl;
	locate((screen_width/2)-((width+4)/2), (screen_height/2)+((height+2)/2));
	cout<<char(200)<<endl;
	locate((screen_width/2)+((width+4)/2), (screen_height/2)+((height+2)/2));
	cout<<char(188)<<endl;
	for(int i=0;i<width+3;i++)
	{
		locate(((screen_width/2)-((width+4)/2))+1+i, (screen_height/2)-((height+2)/2));
		cout<<char(205)<<endl;
		locate(((screen_width/2)-((width+4)/2))+1+i, (screen_height/2)+((height+2)/2));
		cout<<char(205)<<endl;
	}
	for(i=0;i<height+1;i++)
	{
		locate(((screen_width/2)-((width+4)/2)), (screen_height/2)-((height+2)/2)+1+i);
		cout<<char(186)<<endl;
		locate(((screen_width/2)+((width+4)/2)), (screen_height/2)-((height+2)/2)+1+i);
		cout<<char(186)<<endl;
	}
}
main()
{
	screensize();
	struct shape blocks[no_blocks];

	for(int b=0;b<height;b++)
	{
		for(int a=0;a<width;a++)
		{
		play[a][b]=0;
		}
	}


/*	for(b=0;b<height;b++)
	{
		for(int a=0;a<width;a++)
		{
			locate(a,b);cout<<play[a][b]<<endl;
		}
		cout<<endl;
	}
*/	
	for(int i=0;i<no_blocks;i++)
	{
		blocks[i] = init_blocks().matrix[i];
	}
	
	draw_crap();
//	if(check(blocks[2], 0, 0, 0))
	int x=0;
	int y=0;
	for(;;)
	{
	y++;
		draw(blocks[2], 99, x, y-1);
		draw(blocks[2], 0, x, y);
		for(double t=0;t<111111111111111;t++){}

	}

//	locate(31,0);
//	locate(5, 16
/*	for(int m=0;m<no_blocks;m++)
	{
		for(i=0;i<4;i++)
		{
			for(int y=0;y<4;y++)
			{
				for(int x=0;x<4;x++)
				{
					if(blocks[m].rotation[i][y][x]==1)
					{
						locate(x+(10*i),y+(5*m));cout<<char(219)<<endl;
					}
					else
					{
						locate(x+(10*i),y+(5*m));cout<<" "<<endl;
					}
				}
			}
		}
	}
*/			
 	return 0;
}
