#include #include #include "resource.h" #define LIGHTGRAY 0xE0E0E0 #define ROWS 8 #define COLS 4 #define WIDTH 550 #define HEIGHT 550 time_t t; // from time.h int x, y, cxScreen, cyScreen, CurrentX, CurrentY, Count = 0; DWORD TickCount; char Answer[5]; char tempAnswer[5]; char Line[5]; char tempLine[5]; char MastMind[] = " MastMind"; char ABCDEF[] = "A B C D E F"; char Locations[] = "0 Locations"; char Letters[] = "0 Letters"; char Blank[] = " "; char W[] = "W"; char Letter[1]; char Help[] = "\ Enter any of the 6 letters in the boxes.\n\ Get the correct letters in the correct order."; char SansSerif[] = "Microsoft Sans Serif"; BOOL first = TRUE; HWND hwnd, hwndChild[ROWS][COLS], hwndExit, hwndHelp, hwndRetry, hwndNext; HINSTANCE hInst; HDC hdc, hdcMem, hdcBox; HBITMAP hBitmap; PAINTSTRUCT ps; RECT rect; SIZE Size, Size2, Size3, Size4; HFONT hFont, hFont2; LOGFONT lf; HMENU hMenu; LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MSG msg; WNDCLASS wndclass; hInst = hInstance; 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)CreateSolidBrush(LIGHTGRAY); wndclass.lpszMenuName = (LPCSTR)MASTMIND; wndclass.lpszClassName = MastMind; if (!RegisterClass (&wndclass)) return 0; hwnd = CreateWindow(MastMind, MastMind, WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX, 0,0,0,0, NULL, NULL, hInstance, NULL); hwndNext = GetWindow(hwnd, GW_HWNDNEXT); ShowWindow (hwnd, iCmdShow); UpdateWindow (hwnd); while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } return msg.wParam; } void GetRandomLetters(void) { // t = time(NULL); // srand((unsigned)t); // for (x = 0; x < 4; x++) { // y = rand() % 4; // Answer[x] = y + 'A'; // } TickCount = GetTickCount(); __asm { PUSHA MOV CL,6 MOV ESI,OFFSET Answer MOV EDX,TickCount SUB EAX,EAX SUB EBX,EBX MOV BX,4 ;for four loop FOUR: MOV CH,8 ;for random loop RANDOM: ;from Marco Ruiz's routine MOV AL,DH RCR AL,1 XOR AL,DH RCR AL,1 RCR AL,1 XOR AL,DH RCR AL,1 XOR AL,DL RCR AL,1 RCR AL,1 RCR AL,1 AND AL,1 ADD DX,DX ADD AL,DL MOV DL,AL DEC CH JNZ RANDOM AND EAX,0xF DIV CL ADD AH,0x41 MOV Answer[EBX-1],AH INC SI DEC BX JNZ FOUR POPA } } void ShowLetter(void) { Count++; if (Count <= 28) SendMessage(hwndChild[CurrentY][CurrentX], WM_SETTEXT, 0, (LPARAM)Letter); // letter entered BitBlt(hdcMem, 0, 0, rect.right, rect.bottom, hdcBox, 0, 0, SRCCOPY); // to tell this program which letter entered if (CurrentY == 0) return; Line[CurrentX] = Letter[0]; CurrentX++; if (CurrentX == 4) { if (0 == strcmp(Line, Answer)) {//got it! for (x = 0; x < 4; x++) { SendMessage(hwndChild[0][x], WM_SETFONT, (WPARAM)hFont2, TRUE); // hFont3 Letter[0] = Answer[x]; SendMessage(hwndChild[0][x], WM_SETTEXT, 0, (LPARAM)Letter); } SetFocus(hwnd); } else { Locations[0] = '0'; Letters[0] = '0'; for (x = 0; x < 4; x++) tempAnswer[x] = Answer[x]; for (x = 0; x < 4; x++) tempLine[x] = Line[x]; for (x = 0; x < 4; x++) { if (Line[x] == Answer[x]) Locations[0]++; for (y = 0; y < 4; y++) { if ((tempAnswer[y]) && (tempLine[x]) && (tempLine[x] == tempAnswer[y])) { Letters[0]++; tempAnswer[y] = 0; tempLine[x] = 0; } } } if (Letters[0] == '1') Letters[8] = ' '; else Letters[8] = 's'; if (Locations[0] == '1') Locations[10] = ' '; else Locations[10] = 's'; SetBkColor(hdcMem, LIGHTGRAY); TextOut(hdcMem, 48, 48*(CurrentY+1)-CurrentY, Letters, 9); TextOut(hdcMem, 250+(24*5), 48*(CurrentY+1)-CurrentY, Locations, 11); InvalidateRect(hwnd, &rect, FALSE); UpdateWindow(hwnd); CurrentX = 0; Line[0] = Line[1] = Line[2] = Line[3] = 0; CurrentY--; if (CurrentY == 0) { for (x = 0; x < 4; x++) { SendMessage(hwndChild[0][x], WM_SETFONT, (WPARAM)hFont2, TRUE); // hFont3 Letter[0] = Answer[x]; SendMessage(hwndChild[0][x], WM_SETTEXT, 0, (LPARAM)Letter); } } } } } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_SIZE: MoveWindow(hwnd, (cxScreen/2)-(WIDTH/2), (cyScreen/2)-(HEIGHT/2), WIDTH, HEIGHT, TRUE); GetClientRect(hwnd, &rect); return 0; case WM_CREATE: lf.lfHeight = -27; lf.lfWeight = 400; lf.lfItalic = 0; lf.lfUnderline = 0; lf.lfStrikeOut = 0; lf.lfCharSet = 0; lf.lfOutPrecision = 3; lf.lfClipPrecision = 2; lf.lfQuality = 1; lf.lfPitchAndFamily = 0x22; for (x = 0; SansSerif[x] != 0; x++) lf.lfFaceName[x] = SansSerif[x]; lf.lfFaceName[x] = 0; hFont = CreateFontIndirect(&lf); lf.lfHeight = -37; lf.lfWeight = 700; hFont2 = CreateFontIndirect(&lf); cxScreen = GetSystemMetrics(SM_CXFULLSCREEN); cyScreen = GetSystemMetrics(SM_CYFULLSCREEN); GetRandomLetters(); hMenu = GetMenu(hwnd); return 0; case WM_COMMAND: switch (LOWORD(wParam)) { case ID_EXIT: SendMessage(hwnd, WM_DESTROY, 0, 0); break; case ID_PLAYAGAIN: Count = 0; for (y = 0; y < 4; y++) Answer[y] = 0; for (y = CurrentY; y < 8; y++) { if (CurrentX == 0) { CurrentX = 4;//to not skip line again continue; } for (x = 0; x < 4; x++) SetWindowText(hwndChild[y][x], " "); } GetRandomLetters(); for (x = 0; x < 4; x++) SetWindowText(hwndChild[0][x], "?"); CurrentX = 0; CurrentY = 7; SetBkColor(hdcMem, LIGHTGRAY); first = TRUE; InvalidateRect(hwnd, &rect, FALSE); SetFocus(hwnd); break; case ID_HELP40002: MessageBox(hwnd, Help, "Help", MB_OK); break; } break; case WM_KEYDOWN: switch(wParam) { case 'A': Letter[0] = 'A'; ShowLetter(); break; case 'B': Letter[0] = 'B'; ShowLetter(); break; case 'C': Letter[0] = 'C'; ShowLetter(); break; case 'D': Letter[0] = 'D'; ShowLetter(); break; case 'E': Letter[0] = 'E'; ShowLetter(); break; case 'F': Letter[0] = 'F'; ShowLetter(); break; case 'P': SendMessage(hwnd, WM_COMMAND, ID_PLAYAGAIN, 0); break; case VK_ESCAPE: DestroyWindow(hwnd); } break; case WM_PAINT: hdc = BeginPaint (hwnd, &ps); if (first) { first = FALSE; FillRect(hdc, &rect, (HBRUSH)CreateSolidBrush(LIGHTGRAY)); hdcMem = CreateCompatibleDC(hdc); hBitmap = CreateCompatibleBitmap(hdc, rect.right, rect.bottom); SelectObject(hdcMem, hBitmap); SelectObject(hdcMem, hFont); SelectObject(hdc, hFont); GetTextExtentPoint32(hdc, Locations, 11, &Size);//get Size.cx & Size.cy GetTextExtentPoint32(hdc, Letters, 9, &Size2);//get Size2.cx GetTextExtentPoint32(hdc, ABCDEF, 16, &Size3);//get Size3.cx GetTextExtentPoint32(hdc, W, 1, &Size4); SetBkColor(hdc, LIGHTGRAY); TextOut(hdc, (WIDTH/2)-(Size3.cx/2)-10, 420, ABCDEF, 16); for (y = 0; y < ROWS; y++) { for (x = 0; x < COLS; x++) { hwndChild[y][x] = CreateWindow("EDIT", "", WS_CHILD | WS_VISIBLE | WS_DLGFRAME, (x*50)+164, (y*50)+20, 50, 50, hwnd, (HMENU)1, hInst, NULL); SendMessage(hwndChild[y][x], WM_SETFONT, (WPARAM)hFont2, TRUE); SendMessage(hwndChild[y][x], EM_SETREADONLY, 1, 0); } } for (x = 0; x < 4; x++) SetWindowText(hwndChild[0][x], "?"); CurrentX = 0; CurrentY = 7; BitBlt(hdcMem, 0, 0, rect.right, rect.bottom, hdc, 0, 0, SRCCOPY); } else BitBlt(hdc, 0, 0, rect.right, rect.bottom, hdcMem, 0, 0, SRCCOPY); EndPaint(hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage (0); SetForegroundWindow(hwndNext); return 0; } return DefWindowProc (hwnd, message, wParam, lParam); }