home *** CD-ROM | disk | FTP | other *** search
/ Ultimate Game Collection / XULTI.ISO / 452 / puiss4.cc < prev    next >
C/C++ Source or Header  |  1993-12-02  |  7KB  |  263 lines

  1. /*
  2.  
  3.     Microsoft Windows 3.1
  4.      Puissance 4  1.00
  5.          Nov. 1993
  6.  
  7.  Author: Didier Barzin     
  8.  
  9.  Send suggestions to dbarzin@is1.ulb.ac.be
  10.  
  11.  */
  12.  
  13. #include <windows.h>
  14. #include "game.h"
  15.  
  16. /*-----------------------------*/
  17.  
  18. long FAR PASCAL MainWndProc (HWND, WORD, WORD, LONG);
  19.  
  20. /*-----------------------------*/
  21.  
  22. LPSTR AppName = "Windows Puissance 4";
  23.  
  24. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow )
  25. {
  26. HWND hwnd;
  27. MSG msg;
  28. WNDCLASS wndclass;
  29.  
  30. if (!hPrevInstance)
  31.     {
  32.     wndclass.lpszClassName="PUISS4";
  33.     wndclass.hInstance=hInstance;
  34.     wndclass.lpfnWndProc=(WNDPROC)MainWndProc;
  35.     wndclass.hCursor=LoadCursor(hInstance,MAKEINTRESOURCE(2));
  36.     wndclass.hIcon=LoadIcon(hInstance,IDI_APPLICATION);
  37.     wndclass.lpszMenuName="#1";
  38.     wndclass.hbrBackground=COLOR_WINDOW+1;
  39.     wndclass.style=NULL;
  40.     wndclass.cbClsExtra=0;
  41.     wndclass.cbWndExtra=0;
  42.  
  43.         RegisterClass (&wndclass);
  44.         }
  45.  
  46. hwnd= CreateWindow (    "PUISS4",
  47.             AppName,
  48.             WS_OVERLAPPEDWINDOW,
  49.             CW_USEDEFAULT,
  50.             0,
  51.             CW_USEDEFAULT,
  52.             0,
  53.             NULL,
  54.             NULL,
  55.             hInstance,
  56.             NULL ) ;
  57.  
  58. ShowWindow(hwnd,nCmdShow);
  59.  
  60. while (GetMessage(&msg, 0, 0, 0))
  61.     {
  62.     TranslateMessage(&msg);
  63.     DispatchMessage(&msg);
  64.     }
  65.                     
  66. return 0;
  67. }
  68.  
  69. /*------------------------------*/
  70.  
  71. int level=3;
  72. int first=PLAYER;
  73. int turn=PLAYER;
  74.  
  75.  
  76. HBRUSH hbr[4];
  77.  
  78. long FAR PASCAL MainWndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  79. {
  80.  
  81. char *str;
  82.     switch (message)
  83.         {
  84.         case WM_LBUTTONDOWN:
  85.                         if ((turn==PLAYER)&&(!finish()))
  86.             {
  87.             RECT WindowSize;
  88.             GetClientRect (hwnd,&WindowSize);
  89.             if (WindowSize.right!=0)
  90.                             {
  91.                 int i;
  92.                 i=(LOWORD(lParam)*7)/WindowSize.right+1;
  93.                 if (good(i))
  94.                     {
  95.                     RECT WindowSize;
  96.                     GetClientRect (hwnd,&WindowSize);
  97.                     put(i,PLAYER);
  98.                     WindowSize.left=WindowSize.right/7*(i-1);
  99.                     WindowSize.top=WindowSize.bottom-WindowSize.bottom/6*(game.lastpos[i]);
  100.                     WindowSize.right=WindowSize.right/7*i;
  101.                     WindowSize.bottom=WindowSize.bottom-WindowSize.bottom/6*(game.lastpos[i]-1);
  102.  
  103.                     InvalidateRect(hwnd,(RECT FAR *)&WindowSize,FALSE);
  104.                     if (win(PLAYER))
  105.                         {
  106.                         MessageBox(hwnd,(LPSTR)"You win !",AppName,MB_OK|MB_ICONEXCLAMATION);
  107.                                                 turn=NOBODY;
  108.                         }
  109.                     else
  110.                         {
  111.                         turn=COMPUTER;
  112.                         SetTimer(hwnd,(UINT)1,(UINT)1,(TIMERPROC)NULL);
  113.                         }
  114.                     }
  115.                 else
  116.                     MessageBox(hwnd,(LPSTR)"Invalid Place",AppName,MB_OK|MB_ICONASTERISK);
  117.                 }
  118.             }
  119.             break;
  120.         case WM_TIMER:
  121.             if ((turn==COMPUTER)&&(!finish()))
  122.                 {
  123.                             int i;
  124.                 RECT WindowSize;
  125.                                 KillTimer(hwnd,(UINT)1);
  126.                 GetClientRect (hwnd,&WindowSize);
  127.                 i=Min_Max(level);
  128.                 put(i,COMPUTER);
  129.                 WindowSize.left=WindowSize.right/7*(i-1);
  130.                 WindowSize.top=WindowSize.bottom-WindowSize.bottom/6*(game.lastpos[i]);
  131.                 WindowSize.right=WindowSize.right/7*i;
  132.                 WindowSize.bottom=WindowSize.bottom-WindowSize.bottom/6*(game.lastpos[i]-1);
  133.                 InvalidateRect(hwnd,(RECT FAR *)&WindowSize,FALSE);
  134.                         if (win(COMPUTER))
  135.                     {
  136.                     MessageBox(hwnd,(LPSTR)"I win !!!",AppName,MB_OK|MB_ICONEXCLAMATION);
  137.                                 turn=NOBODY;
  138.                     }
  139.                 else turn=PLAYER;
  140.             }
  141.                         break;
  142.         case WM_SIZE:
  143.                         {
  144.             InvalidateRect(hwnd,(RECT FAR *)NULL,FALSE);
  145.             }
  146.             break;
  147.         case WM_PAINT:
  148.             {
  149.             PAINTSTRUCT ps;
  150.             RECT WindowSize;
  151.             int i,j;
  152.             BeginPaint (hwnd, &ps);
  153.             SetBkColor(ps.hdc,RGB(0,0,0xff));
  154.             GetClientRect (hwnd,&WindowSize);
  155.             SelectObject(ps.hdc,hbr[0]);
  156.             Rectangle (ps.hdc,0,0,WindowSize.right,WindowSize.bottom);
  157.             for (i=0;i<7;i++)
  158.                 for (j=0;j<6;j++)
  159.                     {
  160.                     if (game.t[i+1][j+1]==PLAYER)
  161.                         SelectObject(ps.hdc,hbr[1]);
  162.                     else
  163.                     if (game.t[i+1][j+1]==COMPUTER)
  164.                         SelectObject(ps.hdc,hbr[2]);
  165.                     else
  166.                         SelectObject(ps.hdc,hbr[3]);
  167.                     Ellipse (ps.hdc,
  168.                         WindowSize.right/56*(i+1)+WindowSize.right/8*i,
  169.                         WindowSize.bottom-(WindowSize.bottom/42*(j+1)+WindowSize.bottom/7*j),
  170.                         WindowSize.right/56*(i+1)+WindowSize.right/8*(i+1),
  171.                         WindowSize.bottom-(WindowSize.bottom/42*(j+1)+WindowSize.bottom/7*(j+1)));
  172.                     }
  173.             EndPaint(hwnd, &ps);
  174.             }
  175.             break;
  176.         case WM_CREATE:
  177.             {
  178.             HMENU hmenu;
  179.             hmenu=GetMenu(hwnd);
  180.             hmenu=GetSubMenu(hmenu,1);
  181.             CheckMenuItem(hmenu,level,MF_BYCOMMAND|MF_CHECKED);
  182.             InitGame();
  183.             hbr[0]=CreateSolidBrush(RGB(0,0,0xff));
  184.             hbr[1]=CreateSolidBrush(RGB(0xBB,0xBB,0x00));
  185.             hbr[2]=CreateSolidBrush(RGB(0xff,0,0));
  186.             hbr[3]=CreateSolidBrush(RGB(0xAA,0xAA,0xAA));
  187.             }
  188.             break;
  189.         case WM_KEYUP:
  190.             {
  191.             if (wParam==VK_F2)
  192.             {
  193.                 InitGame();
  194.                                 turn=first;
  195.                 InvalidateRect(hwnd,(RECT FAR *)NULL,FALSE);
  196.                 if (turn==COMPUTER)
  197.                     SetTimer(hwnd,(UINT)1,(UINT)1,(TIMERPROC)NULL);
  198.             }
  199.             break;
  200.         case WM_COMMAND:
  201.             if ((wParam>0)&&(wParam<10))
  202.                 {
  203.                 HMENU hmenu;
  204.                 hmenu=GetMenu(hwnd);
  205.                 hmenu=GetSubMenu(hmenu,1);
  206.                 CheckMenuItem(hmenu,level,MF_BYCOMMAND|MF_UNCHECKED);
  207.                 CheckMenuItem(hmenu,wParam,MF_BYCOMMAND|MF_CHECKED);
  208.                 level=wParam;
  209.                                 break;
  210.                 }
  211.             if (wParam==20)
  212.                 {
  213.                 InitGame();
  214.                                 turn=first;
  215.                 InvalidateRect(hwnd,(RECT FAR *)NULL,FALSE);
  216.                 if (turn==COMPUTER)
  217.                     SetTimer(hwnd,(UINT)1,(UINT)1,(TIMERPROC)NULL);
  218.                 break;
  219.                 }
  220.             if (wParam==21)        // Message
  221.                 {
  222.                 HMENU hmenu;
  223.                 hmenu=GetMenu(hwnd);
  224.                 hmenu=GetSubMenu(hmenu,0);
  225.                 if (first==COMPUTER)
  226.                     {
  227.                     ModifyMenu(hmenu,21,MF_BYCOMMAND,21,"Player Begin");
  228.                     first=PLAYER;
  229.                     }
  230.                 else
  231.                     {
  232.                     ModifyMenu(hmenu,21,MF_BYCOMMAND,21,"Computer Begin");
  233.                     first=COMPUTER;
  234.                     }
  235.                 break;
  236.                 }
  237.             if (wParam==22)        // Quit
  238.                 {
  239.                 PostQuitMessage(0);
  240.                             break;
  241.                 }
  242.             if (wParam==23)
  243.                 {
  244.                 MessageBox(hwnd,"Microsoft Windows 3.1\n    Puissance 4 Game\t\n         Version 1.00\n\nAuthor: Didier Barzin\n dbarzin@is1.ulb.ac.be",AppName,MB_OK|MB_ICONASTERISK|MB_NOFOCUS);
  245.                 break;
  246.                 }
  247.             }
  248.             break;
  249.         case WM_DESTROY:
  250.                     {
  251.             int i;
  252.             for (i=0;i<4;i++)
  253.                 DeleteObject(hbr[i]);
  254.             PostQuitMessage(0);
  255.             break;
  256.                         }
  257.         default:
  258.             return(DefWindowProc(hwnd,message,wParam,lParam));
  259.         }
  260. return 0L;
  261. }
  262.  
  263.