Pushok icon

Tic Tac Toe TEST

Pushok | PRO | 03/24/11 11:37:57 PM UTC | 0 ⭐ | 465 👁️ | Never ⏰ | []
C++ |

6.87 KB

|

None

|

0 👍

/

0 👎

#include <windows.h>
 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 
BOOL tic_tac = false;
 
struct TicTacToe{
    HWND TicTacToeWindows[9];
    WORD TicTacNumeration[9];
    enum TTStatus{ID_TIC = 1, ID_TAC};
} TicTac;
 
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow )
{
    MSG  msg ;    
    WNDCLASS wc = {0};
    wc.lpszClassName = TEXT( "TTT" );
    wc.hInstance     = hInstance ;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc   = WndProc ;
    wc.hCursor       = LoadCursor(0, IDC_ARROW);
 
 
    RegisterClass(&wc);
    CreateWindow( wc.lpszClassName, L"TicTacToe",
        WS_VISIBLE | WS_SYSMENU,
        150, 150, 180, 200, 0, 0, hInstance, 0);  
 
    while( GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int) msg.wParam;
}
 
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    bool drawline = false;
    POINT pt[2];
    HDC hd;
    PAINTSTRUCT ps;
 
    switch(msg)  
    {
    case WM_CREATE:
        {
            TicTac.TicTacToeWindows[0] = CreateWindow(TEXT("button"), TEXT(""),
                WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                20, 20, 40, 40,        
                hwnd, (HMENU) 1, NULL, NULL);
            TicTac.TicTacToeWindows[1] = CreateWindow(TEXT("button"), TEXT(""),
                WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                20+45, 20, 40, 40,        
                hwnd, (HMENU) 2, NULL, NULL);
            TicTac.TicTacToeWindows[2] = CreateWindow(TEXT("button"), TEXT(""),
                WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                20+45+45, 20, 40, 40,        
                hwnd, (HMENU) 3, NULL, NULL);
            TicTac.TicTacToeWindows[3] = CreateWindow(TEXT("button"), TEXT(""),
                WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                20, 20+45, 40, 40,        
                hwnd, (HMENU) 4, NULL, NULL);
            TicTac.TicTacToeWindows[4] = CreateWindow(TEXT("button"), TEXT(""),
                WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                20+45, 20+45, 40, 40,        
                hwnd, (HMENU) 5, NULL, NULL);
            TicTac.TicTacToeWindows[5] = CreateWindow(TEXT("button"), TEXT(""),
                WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                20+45+45, 20+45, 40, 40,        
                hwnd, (HMENU) 6, NULL, NULL);
            TicTac.TicTacToeWindows[6] = CreateWindow(TEXT("button"), TEXT(""),
                WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                20, 20+45+45, 40, 40,        
                hwnd, (HMENU) 7, NULL, NULL);
            TicTac.TicTacToeWindows[7] = CreateWindow(TEXT("button"), TEXT(""),
                WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                20+45, 20+45+45, 40, 40,        
                hwnd, (HMENU) 8, NULL, NULL);
            TicTac.TicTacToeWindows[8] = CreateWindow(TEXT("button"), TEXT(""),
                WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                20+45+45, 20+45+45, 40, 40,        
                hwnd, (HMENU) 9, NULL, NULL);
            break;
        }
 
    case WM_COMMAND:
        {
            if (HIWORD(wParam) == BN_CLICKED) 
            {
                if(GetWindowTextLength(HWND(lParam)) != 0)
                    return DefWindowProc(hwnd, msg, wParam, lParam);
 
                SetWindowText((HWND)lParam, tic_tac ? L"()" : L")(");
                tic_tac = !tic_tac;
 
                for(int i;i<9;i++)
                {
                    if((HWND)lParam == TicTac.TicTacToeWindows[i])
                    {
                        TicTac.TicTacNumeration[i] = tic_tac ? TicTac.ID_TAC : TicTac.ID_TIC;
                        break;
                    }
                }
 
                for(int i,j;i<3;i++,j+=3)
                {
                    RECT WinRect;
                    if(TicTac.TicTacNumeration[j] == TicTac.TicTacNumeration[j+1] && TicTac.TicTacNumeration[j+2] == TicTac.TicTacNumeration[j] && TicTac.TicTacNumeration[j])
                    {
                        /*GetWindowRect(TicTac.TicTacToeWindows[j], & WinRect);
                        pt[0].x = WinRect.left;
                        pt[0].y = WinRect.top;
                        GetWindowRect(TicTac.TicTacToeWindows[j+2], & WinRect);
                        pt[1].x = WinRect.right;
                        pt[1].y = WinRect.bottom;
 
                        drawline = true;
                        SendMessage(hwnd, WM_PAINT, wParam, lParam);*/
 
                        drawline = true;
                        MessageBox(hwnd, TicTac.TicTacNumeration[j] == TicTac.ID_TIC ? L"() Win!" : L")( Win!", L"Game message", MB_OK);
                    }
                    if(TicTac.TicTacNumeration[i] == TicTac.TicTacNumeration[i+3] && TicTac.TicTacNumeration[i+6] == TicTac.TicTacNumeration[i] && TicTac.TicTacNumeration[i])
                    {
                        drawline = true;
                        MessageBox(hwnd, TicTac.TicTacNumeration[i] == TicTac.ID_TIC ? L"() Win!" : L")( Win!", L"Game message", MB_OK);
                    }
                    if(TicTac.TicTacNumeration[0] == TicTac.TicTacNumeration[4] && TicTac.TicTacNumeration[8] == TicTac.TicTacNumeration[0] && TicTac.TicTacNumeration[0])
                    {
                        drawline = true;
                        MessageBox(hwnd, TicTac.TicTacNumeration[i] == TicTac.ID_TIC ? L"() Win!" : L")( Win!", L"Game message", MB_OK);
                    }
                    if(TicTac.TicTacNumeration[2] == TicTac.TicTacNumeration[4] && TicTac.TicTacNumeration[2] == TicTac.TicTacNumeration[6] && TicTac.TicTacNumeration[2])
                    {
                        drawline = true;
                        MessageBox(hwnd, TicTac.TicTacNumeration[i+2] == TicTac.ID_TIC ? L"() Win!" : L")( Win!", L"Game message", MB_OK);
                    }
                }
 
                WORD IsGameEnded = 0;
 
                for(int i;i<9;i++)
                    if(GetWindowTextLength(TicTac.TicTacToeWindows[i]) != 0)
                        IsGameEnded++;
 
                if(IsGameEnded == 9 || drawline){
                    MessageBox(hwnd,L"The End", L"Game Info", MB_OK);
                    SendMessage(hwnd, WM_DESTROY, wParam, lParam);
                }
            }
            break;
        }
 
    case WM_PAINT:
        {
            /*hd = BeginPaint(hwnd, &ps);
 
            if(drawline)
            {
                MoveToEx(hd, pt[0].x, pt[0].y, NULL);
                LineTo(hd,pt[1].x, pt[1].y);
            }
 
            EndPaint(hwnd, &ps);*/
            break;
        }
    case WM_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

Comments