Pushok

Tic Tac Toe TEST

Mar 24th, 2011
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.87 KB | None | 0 0
  1. #include <windows.h>
  2.  
  3. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  4.  
  5. BOOL tic_tac = false;
  6.  
  7. struct TicTacToe{
  8.     HWND TicTacToeWindows[9];
  9.     WORD TicTacNumeration[9];
  10.     enum TTStatus{ID_TIC = 1, ID_TAC};
  11. } TicTac;
  12.  
  13. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.     LPSTR lpCmdLine, int nCmdShow )
  15. {
  16.     MSG  msg ;    
  17.     WNDCLASS wc = {0};
  18.     wc.lpszClassName = TEXT( "TTT" );
  19.     wc.hInstance     = hInstance ;
  20.     wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  21.     wc.lpfnWndProc   = WndProc ;
  22.     wc.hCursor       = LoadCursor(0, IDC_ARROW);
  23.  
  24.  
  25.     RegisterClass(&wc);
  26.     CreateWindow( wc.lpszClassName, L"TicTacToe",
  27.         WS_VISIBLE | WS_SYSMENU,
  28.         150, 150, 180, 200, 0, 0, hInstance, 0);  
  29.  
  30.     while( GetMessage(&msg, NULL, 0, 0)) {
  31.         TranslateMessage(&msg);
  32.         DispatchMessage(&msg);
  33.     }
  34.     return (int) msg.wParam;
  35. }
  36.  
  37. LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
  38. {
  39.     bool drawline = false;
  40.     POINT pt[2];
  41.     HDC hd;
  42.     PAINTSTRUCT ps;
  43.  
  44.     switch(msg)  
  45.     {
  46.     case WM_CREATE:
  47.         {
  48.             TicTac.TicTacToeWindows[0] = CreateWindow(TEXT("button"), TEXT(""),
  49.                 WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  50.                 20, 20, 40, 40,        
  51.                 hwnd, (HMENU) 1, NULL, NULL);
  52.             TicTac.TicTacToeWindows[1] = CreateWindow(TEXT("button"), TEXT(""),
  53.                 WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  54.                 20+45, 20, 40, 40,        
  55.                 hwnd, (HMENU) 2, NULL, NULL);
  56.             TicTac.TicTacToeWindows[2] = CreateWindow(TEXT("button"), TEXT(""),
  57.                 WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  58.                 20+45+45, 20, 40, 40,        
  59.                 hwnd, (HMENU) 3, NULL, NULL);
  60.             TicTac.TicTacToeWindows[3] = CreateWindow(TEXT("button"), TEXT(""),
  61.                 WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  62.                 20, 20+45, 40, 40,        
  63.                 hwnd, (HMENU) 4, NULL, NULL);
  64.             TicTac.TicTacToeWindows[4] = CreateWindow(TEXT("button"), TEXT(""),
  65.                 WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  66.                 20+45, 20+45, 40, 40,        
  67.                 hwnd, (HMENU) 5, NULL, NULL);
  68.             TicTac.TicTacToeWindows[5] = CreateWindow(TEXT("button"), TEXT(""),
  69.                 WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  70.                 20+45+45, 20+45, 40, 40,        
  71.                 hwnd, (HMENU) 6, NULL, NULL);
  72.             TicTac.TicTacToeWindows[6] = CreateWindow(TEXT("button"), TEXT(""),
  73.                 WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  74.                 20, 20+45+45, 40, 40,        
  75.                 hwnd, (HMENU) 7, NULL, NULL);
  76.             TicTac.TicTacToeWindows[7] = CreateWindow(TEXT("button"), TEXT(""),
  77.                 WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  78.                 20+45, 20+45+45, 40, 40,        
  79.                 hwnd, (HMENU) 8, NULL, NULL);
  80.             TicTac.TicTacToeWindows[8] = CreateWindow(TEXT("button"), TEXT(""),
  81.                 WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  82.                 20+45+45, 20+45+45, 40, 40,        
  83.                 hwnd, (HMENU) 9, NULL, NULL);
  84.             break;
  85.         }
  86.  
  87.     case WM_COMMAND:
  88.         {
  89.             if (HIWORD(wParam) == BN_CLICKED)
  90.             {
  91.                 if(GetWindowTextLength(HWND(lParam)) != 0)
  92.                     return DefWindowProc(hwnd, msg, wParam, lParam);
  93.  
  94.                 SetWindowText((HWND)lParam, tic_tac ? L"()" : L")(");
  95.                 tic_tac = !tic_tac;
  96.  
  97.                 for(int i;i<9;i++)
  98.                 {
  99.                     if((HWND)lParam == TicTac.TicTacToeWindows[i])
  100.                     {
  101.                         TicTac.TicTacNumeration[i] = tic_tac ? TicTac.ID_TAC : TicTac.ID_TIC;
  102.                         break;
  103.                     }
  104.                 }
  105.  
  106.                 for(int i,j;i<3;i++,j+=3)
  107.                 {
  108.                     RECT WinRect;
  109.                     if(TicTac.TicTacNumeration[j] == TicTac.TicTacNumeration[j+1] && TicTac.TicTacNumeration[j+2] == TicTac.TicTacNumeration[j] && TicTac.TicTacNumeration[j])
  110.                     {
  111.                         /*GetWindowRect(TicTac.TicTacToeWindows[j], & WinRect);
  112.                         pt[0].x = WinRect.left;
  113.                         pt[0].y = WinRect.top;
  114.                         GetWindowRect(TicTac.TicTacToeWindows[j+2], & WinRect);
  115.                         pt[1].x = WinRect.right;
  116.                         pt[1].y = WinRect.bottom;
  117.  
  118.                         drawline = true;
  119.                         SendMessage(hwnd, WM_PAINT, wParam, lParam);*/
  120.  
  121.                         drawline = true;
  122.                         MessageBox(hwnd, TicTac.TicTacNumeration[j] == TicTac.ID_TIC ? L"() Win!" : L")( Win!", L"Game message", MB_OK);
  123.                     }
  124.                     if(TicTac.TicTacNumeration[i] == TicTac.TicTacNumeration[i+3] && TicTac.TicTacNumeration[i+6] == TicTac.TicTacNumeration[i] && TicTac.TicTacNumeration[i])
  125.                     {
  126.                         drawline = true;
  127.                         MessageBox(hwnd, TicTac.TicTacNumeration[i] == TicTac.ID_TIC ? L"() Win!" : L")( Win!", L"Game message", MB_OK);
  128.                     }
  129.                     if(TicTac.TicTacNumeration[0] == TicTac.TicTacNumeration[4] && TicTac.TicTacNumeration[8] == TicTac.TicTacNumeration[0] && TicTac.TicTacNumeration[0])
  130.                     {
  131.                         drawline = true;
  132.                         MessageBox(hwnd, TicTac.TicTacNumeration[i] == TicTac.ID_TIC ? L"() Win!" : L")( Win!", L"Game message", MB_OK);
  133.                     }
  134.                     if(TicTac.TicTacNumeration[2] == TicTac.TicTacNumeration[4] && TicTac.TicTacNumeration[2] == TicTac.TicTacNumeration[6] && TicTac.TicTacNumeration[2])
  135.                     {
  136.                         drawline = true;
  137.                         MessageBox(hwnd, TicTac.TicTacNumeration[i+2] == TicTac.ID_TIC ? L"() Win!" : L")( Win!", L"Game message", MB_OK);
  138.                     }
  139.                 }
  140.  
  141.                 WORD IsGameEnded = 0;
  142.  
  143.                 for(int i;i<9;i++)
  144.                     if(GetWindowTextLength(TicTac.TicTacToeWindows[i]) != 0)
  145.                         IsGameEnded++;
  146.  
  147.                 if(IsGameEnded == 9 || drawline){
  148.                     MessageBox(hwnd,L"The End", L"Game Info", MB_OK);
  149.                     SendMessage(hwnd, WM_DESTROY, wParam, lParam);
  150.                 }
  151.             }
  152.             break;
  153.         }
  154.  
  155.     case WM_PAINT:
  156.         {
  157.             /*hd = BeginPaint(hwnd, &ps);
  158.  
  159.             if(drawline)
  160.             {
  161.                 MoveToEx(hd, pt[0].x, pt[0].y, NULL);
  162.                 LineTo(hd,pt[1].x, pt[1].y);
  163.             }
  164.  
  165.             EndPaint(hwnd, &ps);*/
  166.             break;
  167.         }
  168.     case WM_DESTROY:
  169.         {
  170.             PostQuitMessage(0);
  171.             break;
  172.         }
  173.     }
  174.     return DefWindowProc(hwnd, msg, wParam, lParam);
  175. }
Advertisement
Add Comment
Please, Sign In to add comment