// Slots
// By Steve Mack
// March 2, 2001 (02.03.01)

#include <windows.h>
#include <time.h>
#include <stdlib.h>

HWND ghWnd_Main; // this is the main window 
  //  }
HWND ghWnd_ExitButton;
HWND ghWnd_Slot1;
HWND ghWnd_Slot2;
HWND ghWnd_Slot3;
HWND ghWnd_Pull;
HWND ghWnd_Cash;
HWND ghWnd_YouHave;

int num1 = 1;
int num2 = 2;
int num3 = 3;
int cash = 100;

inline void setcash(int amount);
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch(uMsg) {
		case WM_CLOSE:
			DestroyWindow(ghWnd_Main); // Destroy the main window
			return 0;

		case WM_DESTROY:
			PostQuitMessage(0); // Quit the application
			return 0;

		case WM_COMMAND:
			if(LOWORD(wParam) == BN_CLICKED && (HWND)lParam == ghWnd_ExitButton) {
				MessageBox(hWnd, "Thank You For Playing Steve's Slots!", "Thank You!", MB_OK);
				PostQuitMessage(0);
			}
			if(LOWORD(wParam) == BN_CLICKED && (HWND)lParam == ghWnd_Pull) {
				
				
				srand(time(NULL));
				num1 = 1 + rand() % 6;
				num2 = 1 + rand() % 6;
				num3 = 1 + rand() % 6;
				
				int thenumber = num1;
				char buff[40];
				itoa(thenumber, buff, 10);
				SendMessage(ghWnd_Slot1, WM_SETTEXT, 0, (LPARAM) (LPCSTR) itoa(thenumber, buff, 10));
				
				thenumber = num2;
				SendMessage(ghWnd_Slot2, WM_SETTEXT, 0, (LPARAM) (LPCSTR) itoa(thenumber, buff, 10));
				
				thenumber = num3;
				SendMessage(ghWnd_Slot3, WM_SETTEXT, 0, (LPARAM) (LPCSTR) itoa(thenumber, buff, 10));

				if(num1==num2 || num2==num3 || num1==num3) {
					cash = cash + 10;
				}
				if(num1==num2 && num2==num3) {
					cash = cash + 30;
				}
				if(num1!=num2 && num2!=num3 && num1!=num3) {
					cash = cash -10;
				}
				setcash(cash);
			}
			return 0;
	}
	return DefWindowProc(hWnd, uMsg, wParam, lParam); // Not handled - let the system deal with it
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	WNDCLASSEX wcxMyClass;

	wcxMyClass.cbSize = sizeof(WNDCLASSEX); // Size of structure in bytes
	wcxMyClass.style = 0; // Extra style information
	wcxMyClass.lpfnWndProc = WndProc; // Pointer to window procedure
	wcxMyClass.cbClsExtra = 0; // Extra bytes in class definition
	wcxMyClass.cbWndExtra = 0; // Extra bytes for each window
	wcxMyClass.hInstance = hInstance; // Instance handle for the class
	wcxMyClass.hIcon = NULL; // Default icon - use system
	wcxMyClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Load the system arrow
	wcxMyClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); // Background brush
	wcxMyClass.lpszMenuName = NULL; // Menu name [MAKEINTRESOURCE(IDM_MAINMENU)] - none here
	wcxMyClass.lpszClassName = "TESTCLASS"; // Class name
	wcxMyClass.hIconSm = NULL; // Small icon
	
	RegisterClassEx(&wcxMyClass); // Register the class

	ghWnd_Main = CreateWindow("TESTCLASS", "Steve's Slots", \
		                      WS_OVERLAPPEDWINDOW, 100, 100, 225, 115, \
							  NULL, NULL, hInstance, NULL);
	if(ghWnd_Main) {
		ghWnd_ExitButton = CreateWindow("Button", "E&xit", WS_CHILD | WS_VISIBLE, 110, 60, 100, 25, ghWnd_Main, NULL, hInstance, NULL);
		ghWnd_Pull = CreateWindow("Button", "&Pull", WS_CHILD | WS_VISIBLE, 10, 60, 100, 25, ghWnd_Main, NULL, hInstance, NULL);
		
		ghWnd_Slot1 = CreateWindow("Static", "1", WS_CHILD | WS_VISIBLE, 10, 30, 30, 30, ghWnd_Main, NULL, hInstance, NULL);
		ghWnd_Slot2 = CreateWindow("Static", "2", WS_CHILD | WS_VISIBLE, 97, 30, 200, 30, ghWnd_Main, NULL, hInstance, NULL);
		ghWnd_Slot3 = CreateWindow("Static", "3", WS_CHILD | WS_VISIBLE, 185, 30, 200, 30, ghWnd_Main, NULL, hInstance, NULL);

		ghWnd_YouHave = CreateWindow("Static", "You Have: $", WS_CHILD | WS_VISIBLE, 10, 0, 670, 30, ghWnd_Main, NULL, hInstance, NULL);
		ghWnd_Cash = CreateWindow("Static", "100", WS_CHILD | WS_VISIBLE, 90, 0, 30, 30, ghWnd_Main, NULL, hInstance, NULL);

//		ghWnd_Hello = CreateWindow("Button", "Say Hello!", WS_CHILD | WS_VISIBLE, 0, 50, 100, 30, ghWnd_Main, NULL, hInstance, NULL);

		ShowWindow(ghWnd_Main, nCmdShow); // Show using the specific show style
	}

	/////////////////////////
	// Handle message loop //
	/////////////////////////
	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg); // Translate keycode messages
		DispatchMessage(&msg); // Send to the window
	}

	UnregisterClass("TESTCLASS", hInstance); // Unregister our window class
	return msg.wParam; // msg.wParam contains the code passed to PostQuitMessage()
}

inline void setcash(int amount) {
	char buffer[40];
	SendMessage(ghWnd_Cash, WM_SETTEXT, 0, (LPARAM) (LPCSTR) itoa(amount, buffer, 10));

}