#include <windows.h>
#include <time.h>
#define Size 110
#define BLOCKS 16
//#define BLOCKS 20
#define WM_USER2 WM_USER+1

time_t t; // from time.h
int x, y, z, i, j, cxScreen, cyScreen, Frame, Title, Width, Height, Number;
int DuplicateXs[BLOCKS];
int FirstX;
BOOL won = FALSE, capson = FALSE, first = TRUE, leftturn = TRUE;
char First, Second, Tries[] = {"00    2    0/0"};
char szAppName[] = "Select Duplicates to Win  (try Caps Loc on)";
//char Names[10][10] = {"  ant   ","  beetle","cockroach","  cricket"," ladybug","millipede","roly-poly","  spider"," termite","  worm"};
char Names[8][10] = {"  ant   ","  beetle","cockroach","  cricket"," ladybug","millipede","roly-poly","  worm"};
//char Names[8][7] = {"  la", "  ba", "  ha","  blah", " loing", " boing", " roing", "shoing"};
char myStrings[BLOCKS][2];
struct {
	int loc;
	int random;
} myArray[BLOCKS], temp;
HWND hwnd, Hwnd, Hwnds[BLOCKS];
HWND hwndButton0, hwndButton1, hwndButton2, hwndButton3, hwndButton4, hwndButton5, hwndButton6, hwndButton7, hwndButton8, hwndButton9, hwndButtonA, hwndButtonB, hwndButtonC, hwndButtonD, hwndButtonE, hwndButtonF, hwndButtonG, hwndButtonH, hwndButtonI, hwndButtonJ;
HINSTANCE hInstance;
RECT rect;
HFONT hFont, hFont2;
LOGFONT lf;
char Arial[] = "Arial";

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	MSG          msg;
	WNDCLASS     wndclass;

	wndclass.style         = CS_HREDRAW|CS_VREDRAW;
	wndclass.lpfnWndProc   = WndProc;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = 0;
	wndclass.hInstance     = hInstance;
	wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName  = NULL;
	wndclass.lpszClassName = szAppName;

	if (!RegisterClass(&wndclass))
		return 0;

	hwnd = CreateWindow(szAppName, szAppName,
		WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,
		0,0,0,0,
		NULL, NULL, hInstance, NULL);

	ShowWindow(hwnd, SW_SHOWNORMAL);
	UpdateWindow(hwnd);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (msg.message == WM_LBUTTONDOWN) {
			for (x = 0; x <= BLOCKS; x++) {
				if (IsDialogMessage(Hwnds[x], &msg)) {
					PostMessage(hwnd, WM_USER, (WPARAM)x, 0);
					break;
				}
			}
		}
		else if ((msg.message == WM_KEYDOWN) && (msg.wParam == VK_CAPITAL))
			PostMessage(hwnd, WM_USER2, 0, 0);
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_CREATE:
		if ((GetKeyState(VK_CAPITAL) & 0x0001)!=0)
			capson = TRUE;
		lf.lfHeight = -37;
		lf.lfWeight = 700;
		lf.lfItalic = 0;
		lf.lfUnderline = 0;
		lf.lfStrikeOut = 0;
		lf.lfCharSet = 0;
		lf.lfOutPrecision = 1;
		lf.lfClipPrecision = 2;
		lf.lfQuality = 1;
		lf.lfPitchAndFamily = 0x31;
		for (x = 0; Arial[x] != 0; x++)
			lf.lfFaceName[x] = Arial[x];
		lf.lfFaceName[x] = 0;
		hFont = CreateFontIndirect (&lf);
		lf.lfHeight = -21;
		hFont2 = CreateFontIndirect (&lf);

		cxScreen = GetSystemMetrics(SM_CXFULLSCREEN);
		cyScreen = GetSystemMetrics(SM_CYFULLSCREEN);
		Frame = GetSystemMetrics(SM_CXSIZEFRAME);
		Title = GetSystemMetrics(SM_CYCAPTION);

		Width = (Size*4)+Frame;
		Height = (Size*4)+Title+Frame;
//		Height = (Size*5)+Title+Frame;
		GetClientRect(hwnd, &rect);
		MoveWindow(hwnd, (cxScreen/2)-(Width/2), (cyScreen/2)-(Height/2), Width, Height, TRUE);

		Hwnds[0] = 0;
		hwndButton0 = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 0,      0,      Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[0] = hwndButton0;
		hwndButton1 = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, Size,   0,      Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[1] = hwndButton1;
		hwndButton2 = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 2*Size, 0,      Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[2] = hwndButton2;
		hwndButton3 = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 3*Size, 0,      Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[3] = hwndButton3;
		hwndButton4 = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 0,      Size,   Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[4] = hwndButton4;
		hwndButton5 = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, Size,   Size,   Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[5] = hwndButton5;
		hwndButton6 = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 2*Size, Size,   Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[6] = hwndButton6;
		hwndButton7 = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 3*Size, Size,   Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[7] = hwndButton7;
		hwndButton8 = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 0,      2*Size, Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[8] = hwndButton8;
		hwndButton9 = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, Size,   2*Size, Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[9] = hwndButton9;
		hwndButtonA = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 2*Size, 2*Size, Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[0xA] = hwndButtonA;
		hwndButtonB = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 3*Size, 2*Size, Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[0xB] = hwndButtonB;
		hwndButtonC = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 0,      3*Size, Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[0xC] = hwndButtonC;
		hwndButtonD = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, Size,   3*Size, Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[0xD] = hwndButtonD;
		hwndButtonE = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 2*Size, 3*Size, Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[0xE] = hwndButtonE;
		hwndButtonF = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 3*Size, 3*Size, Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[0xF] = hwndButtonF;
		hwndButtonG = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 0,      4*Size, Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[0X10] = hwndButtonG;
		hwndButtonH = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, Size,   4*Size, Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[0x11] = hwndButtonH;
		hwndButtonI = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 2*Size, 4*Size, Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[0x12] = hwndButtonI;
		hwndButtonJ = CreateWindow("BUTTON", 0, WS_CHILD | WS_VISIBLE, 3*Size, 4*Size, Size, Size, hwnd, NULL, hInstance, NULL);
		Hwnds[0x13] = hwndButtonJ;

		for (x = 0; x < BLOCKS; x++) {
			myArray[x].loc = x;
			myStrings[x][1] = 0;
		}
		t = time(NULL);
		srand((unsigned)t);
		for (x = 0; x < BLOCKS; ) {
			z = rand();
			for (y = 0; (y < x) && (myArray[y].random != z); y++)
				;
			if (y == x)
				myArray[x++].random = z;
		}
		for (i = 1; i < BLOCKS; i++) { // InsertionSort()
			temp = myArray[i];
			j = i-1;
			while ((temp.random < myArray[j].random) && (j >= 0)) {
				myArray[j+1] = myArray[j];
				j = j-1;
			}
			myArray[j+1] = temp;
		}
		for (x = 0; x < BLOCKS; x++) {
			if (myArray[x].loc > ((BLOCKS/2)-1))
				myArray[x].loc -= (BLOCKS/2); // create unsorted duplicates
		}
		for (x = 0; x < BLOCKS; x++) {
			myStrings[x][0] = myArray[x].loc + '0';
			if (capson)
				SendMessage(Hwnds[x], WM_SETFONT, (UINT)hFont2, TRUE);
			else
				SendMessage(Hwnds[x], WM_SETFONT, (UINT)hFont, TRUE);
		}
		break;

	case WM_USER:
		Number = (int)myStrings[x][0] - '0';
		if (first) {
			first = FALSE;
			if (Tries[6] == '1')
				Tries[6] = '2';
			else
				Tries[6] = '1';
			for (y = 0; y < BLOCKS; y++) {
				if (DuplicateXs[y] != 0xFF)
					SetWindowText(Hwnds[y], 0);
			}
			First = myStrings[x][0];
			FirstX = x;
			if (capson)
				SetWindowText(Hwnds[x], Names[Number]);
			else
				SetWindowText(Hwnds[x], &myStrings[x][0]);
		}
		else {
			if (capson)
				SetWindowText(Hwnds[x], Names[Number]);
			else
				SetWindowText(Hwnds[x], &myStrings[x][0]);
			Second = myStrings[x][0];
			if (First == Second) {
				DuplicateXs[FirstX] = 0xFF;
				DuplicateXs[x] = 0xFF;
				won = TRUE;
			}
			first = TRUE;
			if (leftturn)
				leftturn = FALSE;
			else
				leftturn = TRUE;
		}
		SetFocus(hwnd);
		Tries[1]++;
		if (Tries[1] > '9') {
			Tries[1] = '0';
			Tries[0] += 1;
		}
		if (won) {
			if (leftturn)
				Tries[13] += 1;
			else
				Tries[11] += 1;
			won = FALSE;
		}
		SetWindowText(hwnd, Tries);
		break;

	case WM_USER2:
		capson = TRUE;
		for (x = 0; x < BLOCKS; x++)
			SendMessage(Hwnds[x], WM_SETFONT, (UINT)hFont2, TRUE);
		break;

	case WM_KEYDOWN:
		switch (wParam)
		{
		case VK_SPACE:
			for (x = 0; x < BLOCKS; x++) {
				if (DuplicateXs[x] != 0xFF)
					SetWindowText(Hwnds[x], 0);
			}
			SetFocus(hwnd);
			break;
		case VK_ESCAPE:
			DestroyWindow(hwnd);
			break;
		} // end of switch (wParam)
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	} // end of switch(message)
	return DefWindowProc(hwnd, message, wParam, lParam);
}
