home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / os2 / npipe / svrpgame.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  7KB  |  259 lines

  1. /*==============================================================*\
  2.  *  Svrpgame.c - game processing routine: processing of client
  3.  *               messages and game algorithm.
  4.  *      Copyright 1992, IBM Corp.
  5.  *
  6.  *  DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  7.  *  sample code created by IBM Corporation. This sample code is not
  8.  *  part of any standard or IBM product and is provided to you solely
  9.  *  for  the purpose of assisting you in the development of your
  10.  *  applications.  The code is provided "AS IS", without
  11.  *  warranty of any kind.  IBM shall not be liable for any damages
  12.  *  arising out of your use of the sample code, even if they have been
  13.  *  advised of the possibility of   such damages.                                                     *
  14.  *
  15.  *
  16. \*==============================================================*/
  17.  
  18.  
  19. #define INCL_DOSPROCESS
  20. #define INCL_DOSNMPIPES
  21. #define INCL_DOSMEMMGR
  22. #define INCL_WINMESSAGEMGR
  23. #define INCL_DOSERRORS
  24. #define INCL_DOSFILEMGR
  25.  
  26. #include <os2.h>
  27.  
  28. #include <stddef.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31.  
  32. #include "namepipe.h"
  33. #include "svrpglbl.h"
  34. #include "svrpmain.h"
  35. #include "svrpxtrn.h"
  36.  
  37.  
  38. VOID GameThread(VOID *hpTempPipe)
  39. {
  40.    HPIPE   hpGamePipe;
  41.    BOOL    fQuit=FALSE;
  42.    PPIB    dummy;
  43.    PTIB    ptibThrdInfo;
  44.    ULONG   ulID;
  45.    USHORT  usMsg, usData, GameBoard[DIVISIONS][DIVISIONS], usTotalMoves=0;
  46.  
  47.    /* save pipe handle in own memory */
  48.    hpGamePipe = *(HPIPE *)hpTempPipe;
  49.  
  50.    /* get thread ID for use in displaying messages */
  51.    if((BOOL)DosGetInfoBlocks(&ptibThrdInfo, &dummy))
  52.    {
  53.       ulID = 0;
  54.    }
  55.  
  56.    else
  57.    {
  58.       ulID = ptibThrdInfo->tib_ptib2->tib2_ultid;
  59.    }
  60.  
  61.    InitBoard(GameBoard);
  62.  
  63.    /* initialize random number generator */
  64.    srand((unsigned) ulID);
  65.  
  66.    if(!(BOOL)DosConnectNPipe(hpGamePipe))
  67.    {
  68.       while (!fAppExit &&
  69.              !fQuit &&
  70.              !(BOOL)(Pipe_IO(CLIENT, hpGamePipe, &usMsg, &usData, ulID)) &&
  71.              !fAppExit)
  72.       {
  73.          switch (usMsg)
  74.          {
  75.             case CLIENT_MOVE:
  76.                /* enter move from message */
  77.                *((USHORT *)GameBoard + usData) = CLIENT_NUM;
  78.                usTotalMoves++;
  79.  
  80.                /* test for win if total moves >= DIVISIONS*2-1 */
  81.                if (usTotalMoves >= DIVISIONS*2-1 &&
  82.                    ((BOOL)(usData=WinTest(GameBoard)) ||
  83.                     usTotalMoves == DIVISIONS*DIVISIONS))
  84.                {
  85.                   /* notify of win or draw, and break (switch) on return */
  86.                   if (!usData)
  87.                   {
  88.                      usData = DIVISIONS*DIVISIONS;
  89.                      usMsg = WIN_DRAW;
  90.                   }
  91.  
  92.                   else
  93.                   {
  94.                      usMsg = WIN_CLIENT;
  95.                   }
  96.  
  97.                   if ((BOOL)Pipe_IO(SERVER, hpGamePipe, &usMsg, &usData, ulID))
  98.                   {
  99.                      WinPostMsg(hwndMain, WM_MSG, MPFROMLONG(IDMSG_PIPE_WRITE_FAILED), MPVOID);
  100.                      fQuit = TRUE;
  101.                      break;
  102.                   }
  103.  
  104.                   /* call InitBoard on win */
  105.                   InitBoard(GameBoard);
  106.                   usTotalMoves = 0;
  107.  
  108.                   break;  /* switch */
  109.                }
  110.  
  111.                /* NO BREAK */
  112.  
  113.             case YOU_FIRST:
  114.                /* get move if there are moves left */
  115.                usData = GetMove(GameBoard, usTotalMoves);
  116.                usTotalMoves++;
  117.  
  118.                /* test for win if total moves >= DIVISIONS*2-1 */
  119.                if (usTotalMoves >= DIVISIONS*2-1 &&
  120.                    ((BOOL)(usMsg=WinTest(GameBoard)) ||
  121.                     usTotalMoves == DIVISIONS*DIVISIONS))
  122.                {
  123.                   /* write game_won message with winning move */
  124.                   if (!usMsg)
  125.                   {
  126.                      usMsg = WIN_DRAW;
  127.                   }
  128.  
  129.                   else
  130.                   {
  131.                      usMsg = WIN_SERVER;
  132.                   }
  133.  
  134.                   if ((BOOL)Pipe_IO(SERVER, hpGamePipe, &usMsg, &usData, ulID))
  135.                   {
  136.                      WinPostMsg(hwndMain, WM_MSG, MPFROMLONG(IDMSG_PIPE_WRITE_FAILED), MPVOID);
  137.                      fQuit = TRUE;
  138.                      break;
  139.                   }
  140.  
  141.                   /* call InitBoard on win */
  142.                   InitBoard(GameBoard);
  143.                   usTotalMoves = 0;
  144.                }
  145.  
  146.                /* else */
  147.                else
  148.                {
  149.                   /* write move to client */
  150.                   usMsg = SERVER_MOVE;
  151.                   if ((BOOL)Pipe_IO(SERVER, hpGamePipe, &usMsg, &usData, ulID))
  152.                   {
  153.                      WinPostMsg(hwndMain, WM_MSG, MPFROMLONG(IDMSG_PIPE_WRITE_FAILED), MPVOID);
  154.                      fQuit = TRUE;
  155.                   }
  156.                }
  157.                break;
  158.  
  159.             case ERROR_MSG:
  160.                /* post the error to message queue */
  161.                WinPostMsg(hwndMain, WM_MSG, MPFROMSHORT(usData), MPVOID);
  162.  
  163.             case CLIENT_QUIT:
  164.                /* quit while */
  165.                fQuit = TRUE;
  166.          }
  167.       }
  168.  
  169.       DosDisConnectNPipe(hpGamePipe);
  170.    }
  171.  
  172.    DosClose(hpGamePipe);
  173. }
  174.  
  175.  
  176. /*
  177.  *  Check for pieces owning entire row, column, or diagonal.
  178.  */
  179. USHORT WinTest(USHORT GameBoard[DIVISIONS][DIVISIONS])
  180. {
  181.    USHORT  usWinner=0, usTotal1=0, usTotal2=0, usTotal3=0, usTotal4=0;
  182.    int     i, j;
  183.  
  184.    for (i = 0; i < DIVISIONS && !(BOOL)usWinner; i++)
  185.    {
  186.       for (j = 0; j < DIVISIONS && !(BOOL)usWinner; j++)
  187.       {
  188.          usTotal1 += GameBoard[i][j];
  189.          usTotal2 += GameBoard[j][i];
  190.       }
  191.  
  192.       if (SERVER_NUM * DIVISIONS == usTotal1 || CLIENT_NUM * DIVISIONS == usTotal1)
  193.       {
  194.          usWinner = usTotal1/(USHORT)DIVISIONS;
  195.       }
  196.  
  197.       else if (SERVER_NUM * DIVISIONS == usTotal2 || CLIENT_NUM * DIVISIONS == usTotal2)
  198.       {
  199.          usWinner = usTotal2/(USHORT)DIVISIONS;
  200.       }
  201.  
  202.       else
  203.       {
  204.          usTotal1 = usTotal2 = 0;
  205.          usTotal3 += GameBoard[i][i];
  206.          usTotal4 += GameBoard[i][DIVISIONS-i-1];
  207.       }
  208.    }
  209.  
  210.    if (SERVER_NUM * DIVISIONS == usTotal3 || CLIENT_NUM * DIVISIONS == usTotal3)
  211.    {
  212.       usWinner = usTotal3/(USHORT)DIVISIONS;
  213.    }
  214.  
  215.    else if (SERVER_NUM * DIVISIONS == usTotal4 || CLIENT_NUM * DIVISIONS == usTotal4)
  216.    {
  217.       usWinner = usTotal4/(USHORT)DIVISIONS;
  218.    }
  219.  
  220.    return(usWinner);
  221. }
  222.  
  223.  
  224. USHORT GetMove(USHORT GameBoard[DIVISIONS][DIVISIONS], USHORT usTotalMoves)
  225. {
  226.    USHORT  usMovesLeft;
  227.    int     i, j;
  228.  
  229.    usMovesLeft = (USHORT)((USHORT)DIVISIONS * (USHORT)DIVISIONS - usTotalMoves);
  230.  
  231.    /* a poor man's AI */
  232.    usMovesLeft = (USHORT)((usMovesLeft * rand()) / (RAND_MAX + 1));
  233.  
  234.    for (i = 0; i < DIVISIONS; i++)
  235.    {
  236.       for (j = 0; j < DIVISIONS; j++)
  237.       {
  238.          if (0 == GameBoard[i][j] && 0 == usMovesLeft--)
  239.          {
  240.             GameBoard[i][j] = SERVER_NUM;
  241.             return((USHORT)(i * DIVISIONS + j));
  242.          }
  243.       }
  244.    }
  245. }
  246.  
  247.  
  248. /*
  249.  *  Reset game board.
  250.  */
  251. VOID InitBoard(USHORT GameBoard[DIVISIONS][DIVISIONS])
  252. {
  253.    int i, j;
  254.  
  255.    for (i = 0; i < DIVISIONS; i++)
  256.       for (j = 0; j < DIVISIONS; j++)
  257.          GameBoard[i][j] = 0;
  258. }
  259.