home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame Game Cube 5: Strategy / aztechhalloffamegamecubedisc5- / flip / flipit.cpp < prev    next >
C/C++ Source or Header  |  1994-07-15  |  36KB  |  1,339 lines

  1. //REGEN_FILEHEADING
  2. #include <commdlg.h>
  3. #include <windowsx.h>
  4. #include <fstream.h>
  5. #include <math.h>
  6. //REGEN_FILEHEADING
  7.  
  8.  
  9.      /********************************************************************
  10.       *                                                                  *
  11.       *   Source File: Flipit.cpp                                        *
  12.       *   Description: C++ Source file for Flipit application            *
  13.       *   Date:        Sat Jun 25 15:12:28 1994                          *
  14.       *                                                                  *
  15.       ********************************************************************/
  16.  
  17. #include <owl.h>
  18. #include <edit.h>
  19. #include <listbox.h>
  20. #include <combobox.h>
  21. #include <scrollba.h>
  22. #include <dialog.h>
  23. #include <bwcc.h>
  24. #include "FlipitID.h"
  25. #include "Flipit.h"
  26. #include "FlipiCls.h"
  27.  
  28.  
  29. //REGEN_VARIABLES
  30. int Player = 0,nPass = 0,Player1 = 1, Player2 = 2;
  31. int NumPlayers = 2;
  32. int BoardSize = 8,OldBoardSize = 8;
  33. POINT LastMove;
  34. char SaveFile[255] = "";
  35. int nSize,nSquare,nXmargin,nYmargin;
  36. int Difficulty=MEDIUM, OldDifficulty=MEDIUM;
  37. int nStatusBar;
  38. BOOL bDefault=FALSE, bReFresh=FALSE;
  39.  
  40. class TUndo
  41. { public:
  42.     char UndoArr[12][12];
  43.     int UndoPlayer,nUndoPass;
  44.     int nUndoWhiteScore,nUndoBlackScore;
  45.     POINT UndoLastMove;
  46.     friend istream& operator >> ( istream&, TUndo& );
  47.                                 // reads a Board from a stream.  The
  48.                                 // format used must match the format used
  49.                                 // by operator <<.
  50.  
  51.     friend ostream& operator << ( ostream&, TUndo& );
  52.                                 // writes a Board to a stream.  The
  53.                                 // format used must match the format used
  54.                                 // by operator >>.
  55.     friend istream& operator >> ( istream&, POINT& );
  56.                                 // reads a Point from a stream.  The
  57.                                 // format used must match the format used
  58.                                 // by operator <<.
  59.  
  60.     friend ostream& operator << ( ostream&, POINT& );
  61.                                 // writes a POINT to a stream.  The
  62.                                 // format used must match the format used
  63.                 // by operator >>.
  64. };
  65.  
  66. struct WEIGHTTAG
  67. {
  68.   long double Mult;
  69.   int x,y;
  70. };
  71. class TBoard
  72. { public:
  73.     char BoardArr[12][12];
  74.  
  75.     WEIGHTTAG Weight[100];
  76.     BOOL bFlip,bCheat;
  77.     POINT Vectors[8];
  78.     TBoard();
  79.     int nWhiteScore,nBlackScore;
  80.  
  81.     virtual void TBoard::InitBoard(BOOL bFirst);
  82.     virtual void DrawBoard(HDC PaintDC);
  83.     virtual void FlipBoard(HDC PaintDC);
  84.     virtual BOOL ValidMove(POINT p);
  85.     virtual void MakeMove(POINT p);
  86.     virtual void DrawSquare(HDC PaintDC, RECT r, int Shadow);
  87.     virtual void PlacePiece(HDC PaintDC, RECT r, int nRow, int nCol);
  88.     virtual int  ChkVector(HDC PaintDC, int Col, int Row, int Vector,
  89.                    int Other, int Player, BOOL Flip);
  90.     virtual void UpdScore(HDC PaintDC);
  91.     virtual long double GoComputer(int Player, int Level);
  92.     friend istream& operator >> ( istream&, TBoard& );
  93.                                 // reads a Board from a stream.  The
  94.                                 // format used must match the format used
  95.                                 // by operator <<.
  96.  
  97.     friend ostream& operator << ( ostream&, TBoard& );
  98.                                 // writes a Board to a stream.  The
  99.                                 // format used must match the format used
  100.                                 // by operator >>.
  101.     friend istream& operator >> ( istream&, POINT& );
  102.                                 // reads a Point from a stream.  The
  103.                                 // format used must match the format used
  104.                                 // by operator <<.
  105.  
  106.     friend ostream& operator << ( ostream&, POINT& );
  107.                                 // writes a POINT to a stream.  The
  108.                                 // format used must match the format used
  109.                 // by operator >>.
  110. };
  111.  
  112.  
  113. TBoard Board;
  114. TUndo Undo;
  115.  
  116. void Undo2Board( TUndo& , TBoard& );
  117. void Board2Undo( TBoard& , TUndo& );
  118.  
  119. HINSTANCE ghInstance;
  120. HWND ghWindow;
  121. //REGEN_VARIABLES
  122.  
  123. // Define application class derived from  TApplication
  124. class TFlipit : public TApplication
  125. {
  126. public:
  127.   TFlipit(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  128.                    LPSTR lpCmdLine, int nCmdShow)
  129.     : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
  130.     virtual void InitMainWindow();
  131.     virtual void InitInstance();
  132.     //REGEN_APPCLASS
  133.     //REGEN_APPCLASS
  134. };
  135.  
  136. void TFlipit::InitInstance()
  137. {
  138.    TApplication::InitInstance();
  139.    HAccTable = LoadAccelerators(hInstance, "Flipit");
  140.  
  141. }
  142. // Declare TMainWindow, a TWindow descendant
  143. class TMainWindow : public TWindow
  144. {
  145. public:
  146.    TMainWindow(PTWindowsObject AParent, LPSTR ATitle);
  147.    ~TMainWindow();
  148.    virtual void NEW(RTMessage Msg) = [CM_FIRST + IDM_NEW];
  149.    virtual void OPEN(RTMessage Msg) = [CM_FIRST + IDM_OPEN];
  150.    virtual void SAVEAS(RTMessage Msg) = [CM_FIRST + IDM_SAVEAS];
  151.    virtual void EXIT(RTMessage Msg) = [CM_FIRST + IDM_EXIT];
  152.    virtual void UNDO(RTMessage Msg) = [CM_FIRST + IDM_UNDO];
  153.    virtual void HINT(RTMessage Msg) = [CM_FIRST + IDM_HINT];
  154.    virtual void CHEAT(RTMessage Msg) = [CM_FIRST + IDM_CHEAT];
  155.    virtual void PREFERENCES(RTMessage Msg) = [CM_FIRST + IDM_PREFERENCES];
  156.    virtual void CONTENTS(RTMessage Msg) = [CM_FIRST + IDM_CONTENTS];
  157.    virtual void ABOUT(RTMessage Msg) = [CM_FIRST + IDM_ABOUT];
  158.    //REGEN_MAINCLASS
  159.    virtual void Paint(HDC PaintDC, PAINTSTRUCT &PaintInfo);
  160.    virtual void WMLButtonDown(RTMessage Msg) = [WM_FIRST+WM_LBUTTONDOWN];
  161.    virtual void WMSize(RTMessage Msg) = [WM_FIRST + WM_SIZE];
  162.    virtual void WMMouseMove(RTMessage Msg) = [WM_FIRST + WM_MOUSEMOVE];
  163.    virtual void WMTimer(RTMessage Msg) = [WM_FIRST + WM_TIMER];
  164.    virtual int WMSetFocus(RTMessage Msg) = [WM_FIRST + WM_SETFOCUS];
  165.    virtual int WMKillFocus(RTMessage Msg) = [WM_FIRST + WM_KILLFOCUS];
  166.    virtual void WMInitMenu(RTMessage Msg) = [WM_FIRST + WM_INITMENU];
  167.    virtual int WMMenuSelect(RTMessage Msg) = [WM_FIRST + WM_MENUSELECT];
  168.    virtual void SetupWindow();
  169.    virtual void OpenFiFile( LPSTR lpstrFile );
  170.    virtual void SaveFiFile( LPSTR lpstrFile );
  171. private:
  172.    HBRUSH hMainBrush;
  173.    //REGEN_MAINCLASS
  174.  
  175. protected:
  176.    virtual void GetWindowClass(WNDCLASS _FAR & AWndClass);
  177.    virtual LPSTR GetClassName();
  178. };
  179.  
  180.  
  181. /****************************************************
  182.  * TMainWindow implementations: 
  183.  ****************************************************/
  184.  
  185. // Define TMainWindow, a TWindow constructor
  186. TMainWindow::TMainWindow(PTWindowsObject AParent, LPSTR ATitle)
  187.                          : TWindow(AParent, ATitle)
  188. {
  189.    AssignMenu("Flipit");
  190.    //REGEN_MAINCONSTRUCT
  191.    Attr.W = 400;
  192.    Attr.H = 400;
  193.    NumPlayers = Player = 1;
  194.    OldBoardSize = BoardSize = GetPrivateProfileInt("Preferences", "BoardSize", 8, "FLIPIT.INI");
  195.    nSize = nSquare = nXmargin = nYmargin = 0;
  196.    Board2Undo(Board,Undo);
  197.    //REGEN_MAINCONSTRUCT
  198.  
  199. }
  200.  
  201. // Define TMainWindow destructor
  202. TMainWindow::~TMainWindow()
  203. {
  204.    //REGEN_MAINDESTRUCT
  205.    KillTimer(HWindow,10);
  206.    DeleteBrush(hMainBrush);
  207.    //REGEN_MAINDESTRUCT
  208.  
  209. }
  210.  
  211. LPSTR TMainWindow::GetClassName()
  212. {
  213.    return "MainWindow";
  214. }
  215.  
  216. void TMainWindow::GetWindowClass(WNDCLASS _FAR & AWndClass)
  217. {
  218.    TWindow::GetWindowClass(AWndClass);
  219.    AWndClass.hIcon = LoadIcon(AWndClass.hInstance, "FLIPIT");
  220.    //REGEN_CLASSINFO
  221.    hMainBrush = CreateSolidBrush(RGB(0,128,0));
  222.    AWndClass.hbrBackground = hMainBrush;
  223.    AWndClass.hCursor = NULL;
  224.    //REGEN_CLASSINFO
  225.  
  226. }
  227.  
  228. void TMainWindow::NEW(RTMessage)
  229. {
  230.    //REGEN_NEW_EXEC
  231.    Board.InitBoard(FALSE);
  232.    InvalidateRect(HWindow,NULL,TRUE);
  233.    //REGEN_NEW_EXEC
  234. }
  235.  
  236. void TMainWindow::OPEN(RTMessage Msg)
  237. {
  238.    //REGEN_OPEN_EXEC
  239.    OPENFILENAME Ofn;
  240.    HFILE hFile;
  241.    OFSTRUCT FAR* lpOpenBuff;
  242.    int j;
  243.  
  244.    
  245.    char Filters[] = "Flip It Files (*.fi)\0*.fi\0"
  246.             "All Files (*.*)\0*.*\0";
  247.  
  248.    WMInitMenu(Msg);
  249.  
  250.    memset(&Ofn, 0, sizeof(OPENFILENAME));
  251.    Ofn.lpstrTitle = "Choose a File";
  252.    Ofn.hwndOwner  = HWindow;
  253.    Ofn.lpstrFilter = (LPSTR) Filters;
  254.    Ofn.nFilterIndex = 1;
  255.    Ofn.lpstrFile = (LPSTR)SaveFile;
  256.    Ofn.nMaxFile = sizeof(SaveFile);
  257.    Ofn.Flags = OFN_FILEMUSTEXIST | \
  258.             OFN_HIDEREADONLY | \
  259.             OFN_PATHMUSTEXIST;
  260.    Ofn.lpstrDefExt = "fi";
  261.    Ofn.lStructSize = sizeof(OPENFILENAME);
  262.  
  263.    GetOpenFileName(&Ofn);
  264.    OpenFiFile( Ofn.lpstrFile );
  265.  
  266.    WMSize(Msg);
  267.    WMSetFocus(Msg);
  268.    InvalidateRect(HWindow,NULL,FALSE);
  269.  
  270.    //REGEN_OPEN_EXEC
  271. }
  272.  
  273. void TMainWindow::SAVEAS(RTMessage Msg)
  274. {
  275.    //REGEN_SAVEAS_EXEC
  276.    OPENFILENAME Ofn;
  277.    HFILE hFile;
  278.    OFSTRUCT FAR* lpOpenBuff;
  279.    int j;
  280.  
  281.    char Filters[] = "Flip It Files (*.fi)\0*.fi\0"
  282.             "All Files (*.*)\0*.*\0";
  283.    WMInitMenu(Msg);
  284.    memset(&Ofn, 0, sizeof(OPENFILENAME));
  285.    Ofn.lpstrTitle = "Save File As";
  286.    Ofn.hwndOwner  = HWindow;
  287.    Ofn.lpstrFilter = (LPSTR) Filters;
  288.    Ofn.nFilterIndex = 1;
  289.    Ofn.lpstrFile = (LPSTR)SaveFile;
  290.    Ofn.nMaxFile = sizeof(SaveFile);
  291.    Ofn.Flags = OFN_FILEMUSTEXIST | \
  292.             OFN_HIDEREADONLY | \
  293.             OFN_PATHMUSTEXIST;
  294.    Ofn.lpstrDefExt = "fi";
  295.    Ofn.lStructSize = sizeof(OPENFILENAME);
  296.  
  297.    GetSaveFileName(&Ofn);
  298.    SaveFiFile( Ofn.lpstrFile );
  299.    WMSetFocus(Msg);
  300.    //REGEN_SAVEAS_EXEC
  301.  
  302. }
  303.  
  304. void TMainWindow::EXIT(RTMessage)
  305. {
  306.    //REGEN_EXIT_EXEC
  307.    CloseWindow();
  308.    //REGEN_EXIT_EXEC
  309. }
  310.  
  311. void TMainWindow::UNDO(RTMessage)
  312. {
  313.    //REGEN_UNDO_EXEC
  314.    Undo2Board(Undo,Board);
  315.    InvalidateRect(HWindow,NULL,FALSE);
  316.    //REGEN_UNDO_EXEC
  317. }
  318.  
  319. void TMainWindow::HINT(RTMessage)
  320. {
  321.    //REGEN_HINT_EXEC
  322.     SetCursor(LoadCursor(NULL,IDC_WAIT));
  323.     Board2Undo(Board,Undo);
  324.     Board.GoComputer(Player,1);
  325.     SetCursor(LoadCursor(NULL,IDC_ARROW));
  326.    //REGEN_HINT_EXEC
  327. }
  328.  
  329. void TMainWindow::CHEAT(RTMessage)
  330. {
  331.    //REGEN_CHEAT_EXEC
  332.    Board.bCheat = TRUE;
  333.    //REGEN_CHEAT_EXEC
  334. }
  335.  
  336. void TMainWindow::PREFERENCES(RTMessage Msg)
  337. {
  338.    // Execute modal dialog
  339.    if (GetModule()->ExecDialog(
  340.                new TPREFERDlg(this, "PREFER")) == IDOK )
  341.    {
  342.    //REGEN_PREFERENCES_EXEC
  343.      if (OldBoardSize != BoardSize || OldDifficulty != Difficulty)
  344.      {
  345.        NEW(Msg);
  346.        WMSize(Msg);
  347.        OldBoardSize = BoardSize;
  348.      }
  349.      if (bDefault)
  350.      {
  351.        if (Player1 == 1)
  352.          WritePrivateProfileString("Preferences", "Player1", "1", "FLIPIT.INI");
  353.        else
  354.          WritePrivateProfileString("Preferences", "Player1", "2", "FLIPIT.INI");
  355.        if (Player2 == 1)
  356.          WritePrivateProfileString("Preferences", "Player2", "1", "FLIPIT.INI");
  357.        else
  358.      WritePrivateProfileString("Preferences", "Player2", "2", "FLIPIT.INI");
  359.  
  360.        switch ( BoardSize )
  361.        {
  362.        case 6:
  363.      WritePrivateProfileString("Preferences", "BoardSize", "6", "FLIPIT.INI");
  364.          break;
  365.        case 8:
  366.      WritePrivateProfileString("Preferences", "BoardSize", "8", "FLIPIT.INI");
  367.          break;
  368.        case 10:
  369.      WritePrivateProfileString("Preferences", "BoardSize", "10", "FLIPIT.INI");
  370.          break;
  371.        }
  372.  
  373.        switch ( Difficulty )
  374.        {
  375.        case 1:
  376.      WritePrivateProfileString("Preferences", "Difficulty", "1", "FLIPIT.INI");
  377.          break;
  378.        case 2:
  379.      WritePrivateProfileString("Preferences", "Difficulty", "2", "FLIPIT.INI");
  380.          break;
  381.        case 3:
  382.      WritePrivateProfileString("Preferences", "Difficulty", "3", "FLIPIT.INI");
  383.          break;
  384.        }
  385.      }
  386.  
  387.      InvalidateRect(HWindow,NULL,TRUE);
  388.    //REGEN_PREFERENCES_EXEC
  389.  
  390.    }
  391. }
  392.  
  393. void TMainWindow::CONTENTS(RTMessage)
  394. {
  395.    //REGEN_CONTENTS_EXEC
  396.    WinHelp( ghWindow, "FLIPIT.HLP", HELP_CONTENTS, 0L );
  397.    //REGEN_CONTENTS_EXEC
  398. }
  399.  
  400. void TMainWindow::ABOUT(RTMessage)
  401. {
  402.    // Execute modal dialog
  403.    if (GetModule()->ExecDialog(
  404.                new TDIALOG_1Dlg(this, "DIALOG_1")) == IDOK )
  405.    {
  406.    //REGEN_ABOUT_EXEC
  407.    //REGEN_ABOUT_EXEC
  408.  
  409.    }
  410. }
  411.  
  412.  
  413. /***************************************************
  414.  * TFlipitApp method implementations:
  415.  ***************************************************/
  416.  
  417. // Construct the TFlipit's MainWindow of type TMainWindow
  418. void TFlipit::InitMainWindow()
  419. {
  420.    MainWindow = new TMainWindow(NULL, Name);
  421.    //REGEN_MAINCREATE
  422.    ghInstance = hInstance;
  423.    //REGEN_MAINCREATE
  424. }
  425.  
  426. // Main program
  427. int PASCAL WinMain(HINSTANCE hInstance,
  428.                    HINSTANCE hPrevInstance,
  429.                    LPSTR lpCmdLine,
  430.                    int nCmdShow)
  431. {
  432.    HINSTANCE hBorLibrary;
  433.  
  434.    hBorLibrary = LoadLibrary("bwcc.dll");
  435.  
  436.    if((UINT)hBorLibrary <= 32)
  437.       MessageBox(NULL, "Unable to load Borland Controls", "System Error", MB_OK | MB_ICONHAND);
  438.  
  439.    //REGEN_INIT
  440.    //REGEN_INIT
  441.  
  442.    TFlipit Flipit ("Flip it", hInstance, hPrevInstance,
  443.       lpCmdLine, nCmdShow);
  444.  
  445.    Flipit.Run();
  446.  
  447.    if((UINT)hBorLibrary > 32)
  448.       FreeLibrary(hBorLibrary);
  449.  
  450.    //REGEN_CLEANUP
  451.    //REGEN_CLEANUP
  452.  
  453.    return Flipit.Status;
  454. }
  455.  
  456. //REGEN_CUSTOMCODE
  457. void TMainWindow::SetupWindow()
  458. {
  459.    TWindow::SetupWindow();
  460.    SetTimer(HWindow,10,1500,NULL);
  461. }
  462. #pragma argsused
  463. void TMainWindow::Paint(HDC PaintDC, PAINTSTRUCT &PaintInfo)
  464. {
  465.  
  466.   ghWindow = HWindow;
  467.  
  468.   if (bReFresh)
  469.     Board.UpdScore(PaintDC);
  470.  
  471.   Board.DrawBoard(PaintDC);
  472. }
  473.  
  474. void TMainWindow::WMLButtonDown(RTMessage Msg)
  475. {
  476.   POINT p;
  477.   HCURSOR hCursor;
  478.   p.x = Msg.LP.Lo; p.y = Msg.LP.Hi;
  479.   if ( Board.ValidMove(p) )
  480.   {
  481.     Board2Undo(Board,Undo);
  482.     Board.MakeMove(p);
  483.     InvalidateRect(HWindow,NULL,FALSE);
  484.   }
  485. }
  486.  
  487. void TMainWindow::WMSize(RTMessage)
  488. {
  489.   RECT r;
  490.  
  491.   bReFresh = TRUE;
  492.  
  493.   GetClientRect(HWindow,&r);
  494.   nStatusBar = GetSystemMetrics(SM_CYMENU);  // Height of Horz Scroll Bar
  495.   if ( r.right < r.bottom )
  496.     nSize = r.right;
  497.   else
  498.     nSize = r.bottom;
  499.  
  500.   nSquare = nSize/(BoardSize+2);
  501.  
  502.   nXmargin = (r.right-nSquare*BoardSize)/2;
  503.   nYmargin = (r.bottom-nSquare*BoardSize-nStatusBar)/2;
  504.  
  505. }
  506. void TMainWindow::WMMouseMove(RTMessage Msg)
  507. {
  508.   // determine board coordinates
  509.   POINT p;
  510.   HCURSOR hCursor;
  511.   p.x = Msg.LP.Lo; p.y = Msg.LP.Hi;
  512.   if ( Board.ValidMove(p) )
  513.     SetCursor(LoadCursor(NULL,IDC_CROSS));
  514.   else
  515.     SetCursor(LoadCursor(NULL,IDC_ARROW));
  516.   
  517. }
  518. TBoard::TBoard()
  519. {
  520.   InitBoard(TRUE);
  521. }
  522.  
  523. #pragma argsused
  524. void TBoard::InitBoard(BOOL bFirst)
  525. {
  526.    int nRow,nCol,nLevel,i,j,Max;
  527.    WEIGHTTAG Temp;
  528.  
  529.    if (bFirst)
  530.    {
  531.      BoardSize = GetPrivateProfileInt("Preferences", "BoardSize", 8, "FLIPIT.INI");
  532.      Difficulty = GetPrivateProfileInt("Preferences", "Difficulty", 2, "FLIPIT.INI");
  533.      Player1 = GetPrivateProfileInt("Preferences", "Player1", 1, "FLIPIT.INI");
  534.      Player2 = GetPrivateProfileInt("Preferences", "Player2", 2, "FLIPIT.INI");
  535.    }
  536.  
  537.    Player = 1;
  538.    bCheat = FALSE;
  539.    memset(&BoardArr, 0, sizeof(BoardArr));
  540.  
  541.    BoardArr[BoardSize/2][BoardSize/2] = \
  542.    BoardArr[BoardSize/2+1][BoardSize/2+1] = 1;
  543.    BoardArr[BoardSize/2+1][BoardSize/2] = \
  544.    BoardArr[BoardSize/2][BoardSize/2+1] = 2;
  545.    LastMove.x = BoardSize/2+1; LastMove.y = BoardSize/2;
  546.    nWhiteScore = nBlackScore = 2;
  547.  
  548.    Vectors[0].x =  0; Vectors[0].y = -1;  //North
  549.    Vectors[1].x =  1; Vectors[1].y = -1;  //Northeast
  550.    Vectors[2].x =  1; Vectors[2].y =  0;  //East
  551.    Vectors[3].x =  1; Vectors[3].y =  1;  //Southeast
  552.    Vectors[4].x =  0; Vectors[4].y =  1;  //South
  553.    Vectors[5].x = -1; Vectors[5].y =  1;  //Southwest
  554.    Vectors[6].x = -1; Vectors[6].y =  0;  //West
  555.    Vectors[7].x = -1; Vectors[7].y = -1;  //Northwest
  556.  
  557.    bFlip = FALSE;
  558.  
  559.    for (i=0;i<100;i++)
  560.    {
  561.      Weight[i].Mult= Weight[i].x = Weight[i].y = 0;
  562.    }
  563.  
  564.    if (Difficulty == 0)
  565.      Difficulty = MEDIUM;
  566.  
  567.    for (nCol=1;nCol<=BoardSize;nCol++)
  568.      for (nRow=1;nRow<=BoardSize;nRow++)
  569.      {
  570.        i=(nRow-1)*BoardSize+nCol-1;
  571.        Weight[i].x = nCol;
  572.        Weight[i].y = nRow;
  573.        if ((nCol == 1 || nCol == BoardSize) \
  574.           && (nRow == 1 || nRow == BoardSize))
  575.          Weight[i].Mult = 5000;
  576.        else if (nCol == 1 || nCol == BoardSize \
  577.                || nRow == 1 || nRow == BoardSize)
  578.          Weight[i].Mult = 30;
  579.        else if ((nCol == 3 || nCol == BoardSize-2) \
  580.                && (nRow == 3 || nRow == BoardSize-2))
  581.          Weight[i].Mult = 20;
  582.        else if (((nCol == 3 || nCol == BoardSize-2) \
  583.                && nRow > 3 && nRow < BoardSize-2) \
  584.                || ((nRow == 10 || nRow == BoardSize-2)\
  585.            && nCol > 3 && nCol < BoardSize-2))
  586.      Weight[i].Mult = 5;
  587.        else if ((nCol == 2 || nCol == BoardSize-1) \
  588.            && (nRow == 2 || nRow == BoardSize-1))
  589.      Weight[i].Mult = 1;
  590.        else
  591.      Weight[i].Mult = 3;
  592.      }
  593.    for (i=0;i<BoardSize*BoardSize-1;i++)
  594.    {
  595.      Max=i;
  596.      for (j=i+1;j<BoardSize*BoardSize;j++)
  597.        if (Weight[j].Mult > Weight[Max].Mult)
  598.      Max=j;
  599.        else if (Weight[j].Mult == Weight[Max].Mult)
  600.      if (Weight[j].x > Weight[Max].x)
  601.        Max=j;
  602.      else if (Weight[j].x == Weight[Max].x && Weight[j].y > Weight[Max].y)
  603.        Max=j;
  604.  
  605.      if (Max != i)
  606.      {
  607.        Temp = Weight[i];
  608.        Weight[i] = Weight[Max];
  609.        Weight[Max] = Temp;
  610.      }
  611.    }
  612.  
  613.    OldDifficulty = Difficulty;
  614. }
  615. void TBoard::DrawBoard(HDC PaintDC)
  616. {
  617.   RECT r,rSquare,rBoard;
  618.   int nRow,nCol,nOther,nVect;
  619.   BOOL bGameOver;
  620.  
  621.   rBoard.left = nXmargin-2;
  622.   rBoard.top = nYmargin-2;
  623.   rBoard.right = nXmargin+nSquare*BoardSize+2;
  624.   rBoard.bottom = nYmargin+nSquare*BoardSize+2;
  625.  
  626.   if (!bReFresh && bFlip)
  627.   {
  628.     rSquare.left   = nXmargin+(LastMove.x-1)*nSquare+1;
  629.     rSquare.top    = nYmargin+(LastMove.y-1)*nSquare+1;
  630.     rSquare.right  = rSquare.left+nSquare-2;
  631.     rSquare.bottom = rSquare.top+nSquare-2;
  632.     PlacePiece(PaintDC,rSquare,LastMove.x,LastMove.y);
  633.     FlipBoard(PaintDC);
  634.     UpdScore(PaintDC);
  635.  
  636.     // See if we have any valid moves left.
  637.     nOther = (Player == 1) ? 2 : 1;
  638.     bGameOver = TRUE;
  639.     nPass=0;
  640.  
  641.     for (nCol=1;nCol<=BoardSize;nCol++)
  642.       for (nRow=1;nRow<=BoardSize;nRow++)
  643.       {
  644.     if (BoardArr[nCol][nRow] == 0)
  645.     {
  646.       for ( nVect=0;nVect<8;nVect++ )
  647.         if (ChkVector(NULL,nCol,nRow,nVect,nOther,Player,FALSE) > 0)
  648.             {
  649.           bGameOver = FALSE;
  650.         }
  651.         }
  652.       }
  653.  
  654.     if (bGameOver == TRUE)
  655.     {
  656.       for (nCol=1;nCol<=BoardSize;nCol++)
  657.         for (nRow=1;nRow<=BoardSize;nRow++)
  658.         {
  659.        if (BoardArr[nCol][nRow] == 0)
  660.       {
  661.         for ( nVect=0;nVect<8;nVect++ )
  662.           if (ChkVector(NULL,nCol,nRow,nVect,Player,nOther,FALSE) > 0)
  663.               {
  664.             bGameOver = FALSE;
  665.           }
  666.           }
  667.         }
  668.  
  669.       if (bGameOver == TRUE)
  670.         BWCCMessageBox(NULL,"Game Over.","Flip It Message",MB_OK | MB_ICONEXCLAMATION );
  671.       else if (Player == 1)
  672.       BWCCMessageBox(NULL,"White must pass, Black's turn.","Flip It Message",MB_OK | MB_ICONEXCLAMATION );
  673.       else
  674.     BWCCMessageBox(NULL,"Black must pass, White's turn.","Flip It Message",MB_OK | MB_ICONEXCLAMATION );
  675.  
  676.       Player = nOther;
  677.     }
  678.   }
  679.   else
  680.   {
  681.     bReFresh = FALSE;
  682.     Rectangle(PaintDC,rBoard.left-1,rBoard.top-1,
  683.               rBoard.right+1,rBoard.bottom+1);
  684.     DrawSquare(PaintDC,rBoard,1);
  685.  
  686.     MoveTo(PaintDC,nXmargin-1,nYmargin+nSquare*BoardSize+1);
  687.  
  688.  
  689.     for (nCol=1;nCol<=BoardSize;nCol++)
  690.       for (nRow=1;nRow<=BoardSize;nRow++)
  691.       {
  692.     rSquare.left   = nXmargin+(nCol-1)*nSquare+1;
  693.     rSquare.top    = nYmargin+(nRow-1)*nSquare+1;
  694.         rSquare.right  = rSquare.left+nSquare-2;
  695.         rSquare.bottom = rSquare.top+nSquare-2;
  696.         DrawSquare(PaintDC,rSquare,-1);
  697.     PlacePiece(PaintDC,rSquare,nCol,nRow);
  698.       }
  699.     if (bFlip==TRUE)
  700.       InvalidateRect(ghWindow,NULL,FALSE);
  701.  
  702.   }
  703. }
  704.  
  705. void TBoard::DrawSquare(HDC PaintDC,RECT r,int Shadow)
  706. {
  707.   HPEN hWhitePen, hGrayPen, hOldPen;
  708.   HBRUSH hGrayBrush,hOldBrush;
  709.  
  710.   hGrayPen = CreatePen(PS_SOLID, 1, RGB(96,96,96));
  711.   hWhitePen = CreatePen(PS_SOLID, 1, RGB(255,255,255));
  712.   hOldPen = SelectPen(PaintDC,hGrayPen);
  713.  
  714.   hGrayBrush = GetStockBrush(LTGRAY_BRUSH);
  715.   hOldBrush = SelectBrush(PaintDC,hGrayBrush);
  716.  
  717.   Rectangle(PaintDC,r.left,r.top,r.right,r.bottom);
  718.   SelectPen(PaintDC,hWhitePen);
  719.   if ( Shadow > 0 )
  720.   { 
  721.     MoveTo(PaintDC,r.left,r.bottom-1);
  722.     LineTo(PaintDC,r.left,r.top);
  723.     LineTo(PaintDC,r.right-1,r.top);
  724.   }
  725.   else
  726.   {
  727.     MoveTo(PaintDC,r.left,r.bottom-1);
  728.     LineTo(PaintDC,r.right-1,r.bottom-1);
  729.     LineTo(PaintDC,r.right-1,r.top);
  730.   }
  731.  
  732.   SelectPen(PaintDC,hOldPen);
  733.   SelectBrush(PaintDC,hOldBrush);
  734.  
  735.   DeletePen(hWhitePen);
  736.   DeletePen(hGrayPen);
  737. }
  738.  
  739. void TBoard::PlacePiece(HDC PaintDC, RECT r, int nCol, int nRow)
  740. {
  741.   HDC hCompatDC;
  742.   HBITMAP      hBitmap, hOldBitmap;
  743.   BITMAP       BM;
  744.   int i;
  745.  
  746.   // Get handle to the bitmap
  747.   switch (BoardArr[nCol][nRow])
  748.   {
  749.     case 1:
  750.     case 2:
  751.       HBRUSH hBrush,hOldBrush;
  752.  
  753.       if (BoardArr[nCol][nRow] == 1)
  754.     hBrush = GetStockBrush(WHITE_BRUSH);
  755.       else
  756.     hBrush = GetStockBrush(BLACK_BRUSH);
  757.  
  758.       hOldBrush = SelectBrush(PaintDC,hBrush);
  759.  
  760.       Ellipse(PaintDC,r.left+1,r.top+1,r.right-1,r.bottom-1);
  761.  
  762.       SelectBrush(PaintDC,hOldBrush);
  763.  
  764.       return;
  765.  
  766.     case 3: hBitmap = LoadBitmap(ghInstance, "BFLIP"); break;
  767.     case 4: hBitmap = LoadBitmap(ghInstance, "VERT" ); break;
  768.     case 5: hBitmap = LoadBitmap(ghInstance, "WFLIP"); break;
  769.     default: return;
  770.   }
  771.   // Get dimensions of bitmap
  772.   GetObject(hBitmap, sizeof(BM), &BM);
  773.  
  774.   // Create compatible display context
  775.   hCompatDC = CreateCompatibleDC(PaintDC);
  776.  
  777.   // Select bitmap into the compataible DC
  778.   hOldBitmap = SelectBitmap(hCompatDC, hBitmap);
  779.  
  780.   // Display bitmap in client area
  781.   StretchBlt(PaintDC, r.left+1, r.top+1, r.right-r.left-2, r.bottom-r.top-2,
  782.            hCompatDC, 0, 0, BM.bmWidth, BM.bmHeight,
  783.            SRCCOPY);
  784.  
  785.   // De-select the bitmap
  786.   SelectBitmap(hCompatDC, hOldBitmap);
  787.  
  788.   // Clean up after we are done
  789.   DeleteDC(hCompatDC);
  790.   DeleteBitmap(hBitmap);
  791.      
  792. }
  793.  
  794. BOOL TBoard::ValidMove(POINT p)
  795. {
  796.   int x,y,nRow,nCol,nVect, nOther;
  797.  
  798.   if ( p.x <= nXmargin | p.y <= nYmargin ) return FALSE;
  799.   if ( p.x >= nXmargin + nSquare*BoardSize |
  800.        p.y >= nYmargin + nSquare*BoardSize ) return FALSE;
  801.  
  802.   // see if current square is occupied
  803.   x=p.x-nXmargin;
  804.   y=p.y-nYmargin;
  805.   nCol = x/nSquare+1;
  806.   nRow = y/nSquare+1;
  807.   if ( bCheat )
  808.   {
  809.     if (BoardArr[nCol][nRow] == Player ) return FALSE;
  810.   }
  811.   else
  812.     if ( BoardArr[nCol][nRow] != 0 ) return FALSE;
  813.  
  814.   // check board for adjacent pieces of the opposite color
  815.   // if opposite color found, see if the current color is
  816.   // on the other side
  817.   nOther = (Player==1) ? 2 : 1;
  818.   for ( nVect=0;nVect<8;nVect++ )
  819.   {
  820.     if (ChkVector(NULL,nCol,nRow,nVect,nOther,Player,FALSE) > 0)
  821.       return TRUE;
  822.   }
  823.   
  824.   // if no opposite colors are found, then return false
  825.   return FALSE;
  826. }
  827.  
  828. void TBoard::MakeMove(POINT p)
  829. {
  830.   int x,y,nCol,nRow;
  831.   x=p.x-nXmargin;
  832.   y=p.y-nYmargin;
  833.   nCol = x/nSquare+1;
  834.   nRow = y/nSquare+1;
  835.   BoardArr[nCol][nRow] = Player;
  836.   LastMove.x = nCol; LastMove.y = nRow;
  837.   bFlip = TRUE;
  838.   bCheat = FALSE;
  839. }
  840.  
  841. void TBoard::FlipBoard(HDC PaintDC)
  842. {
  843.   int nOther,nVect;
  844.   nOther = (Player==1) ? 2 : 1;
  845.   for ( nVect=0;nVect<8;nVect++ )
  846.     ChkVector(PaintDC,LastMove.x,LastMove.y,nVect,nOther,Player,TRUE);
  847.  
  848.   bFlip = FALSE;
  849.   Player = nOther;
  850. }
  851.  
  852. int TBoard::ChkVector(HDC PaintDC,int NewCol, int NewRow, int nVect,int nChkPiece,\
  853.                       int nFlipto, BOOL Flipit)
  854. { // This routine uses recursion to count the number of pieces that would be
  855.   // flipped if a piece were placed at the specified row & col.
  856.   // To see how this works, try walking through this code with a
  857.   // piece of paper and make a sample move.
  858.   int nFlipCount=0;
  859.   RECT rSquare;
  860.   NewCol += Vectors[nVect].x; NewRow += Vectors[nVect].y;
  861.   if (BoardArr[NewCol][NewRow] == nChkPiece)
  862.   {
  863.     nFlipCount++; 
  864.     nFlipCount += ChkVector(PaintDC,NewCol,NewRow,nVect,nChkPiece,nFlipto,Flipit);
  865.   }
  866.   else if (BoardArr[NewCol][NewRow] == nFlipto) return 0;
  867.   else return -10;
  868.  
  869.   if (Flipit && nFlipCount > 0)
  870.   {
  871.     if (PaintDC == NULL)  // Flip piece but don't display it...
  872.       BoardArr[NewCol][NewRow] = Player;
  873.     else
  874.     {
  875.       rSquare.left   = nXmargin+(NewCol-1)*nSquare+1;
  876.       rSquare.top    = nYmargin+(NewRow-1)*nSquare+1;
  877.       rSquare.right  = rSquare.left+nSquare-2;
  878.       rSquare.bottom = rSquare.top+nSquare-2;
  879.       switch (Player)
  880.       {
  881.     case 1: //White
  882.             BoardArr[NewCol][NewRow]=3;
  883.       PlacePiece(PaintDC,rSquare,NewCol,NewRow);
  884.       BoardArr[NewCol][NewRow]=4;
  885.       PlacePiece(PaintDC,rSquare,NewCol,NewRow);
  886.       BoardArr[NewCol][NewRow]=5;
  887.       PlacePiece(PaintDC,rSquare,NewCol,NewRow);
  888.       BoardArr[NewCol][NewRow]=1;
  889.       PlacePiece(PaintDC,rSquare,NewCol,NewRow);
  890.           break;
  891.         case 2: //Black
  892.       BoardArr[NewCol][NewRow]=5;
  893.       PlacePiece(PaintDC,rSquare,NewCol,NewRow);
  894.       BoardArr[NewCol][NewRow]=4;
  895.       PlacePiece(PaintDC,rSquare,NewCol,NewRow);
  896.       BoardArr[NewCol][NewRow]=3;
  897.       PlacePiece(PaintDC,rSquare,NewCol,NewRow);
  898.       BoardArr[NewCol][NewRow]=2;
  899.       PlacePiece(PaintDC,rSquare,NewCol,NewRow);
  900.       break;
  901.       }
  902.     }
  903.   }
  904.   return nFlipCount;
  905. }
  906.  
  907. void TBoard::UpdScore(HDC PaintDC)
  908. {
  909.   RECT r,rMessage,rWhiteScoreBmp,rWhiteScore,rBlackScoreBmp,rBlackScore;
  910.   HPEN hWhitePen, hGrayPen, hOldPen;
  911.   HDC hCompatDC;
  912.   HBITMAP      hBitmap, hOldBitmap;
  913.   BITMAP       BM;
  914.   WORD wScoreWidth;
  915.   char cszWhite[6],cszBlack[6];
  916.   int nOrigWhite;
  917.  
  918.   nOrigWhite = nWhiteScore;
  919.  
  920.   nWhiteScore = nBlackScore = 0;
  921.   int nCol,nRow;
  922.   for (nCol=1;nCol<=BoardSize;nCol++)
  923.     for (nRow=1;nRow<=BoardSize;nRow++)
  924.       switch (BoardArr[nCol][nRow])
  925.       {
  926.       case 1: nWhiteScore++; break;
  927.       case 2: nBlackScore++; break;
  928.       }
  929.  
  930.   hGrayPen = CreatePen(PS_SOLID, 1, RGB(96,96,96));
  931.   hWhitePen = CreatePen(PS_SOLID, 1, RGB(255,255,255));
  932.  
  933.   memset(cszWhite,0,sizeof(cszWhite));
  934.   memset(cszBlack,0,sizeof(cszBlack));
  935.  
  936.  
  937.   GetClientRect(ghWindow,&r);
  938.   r.top    = r.bottom-nStatusBar;
  939.  
  940.   if ( bReFresh || nOrigWhite == Board.nWhiteScore )
  941.     DrawSquare(PaintDC,r,1);
  942.  
  943.   wScoreWidth = LOWORD((GetTextExtent(PaintDC,"999",3)+nStatusBar+15)*2);
  944.  
  945.   rMessage.left   = 1;
  946.   rMessage.top    = r.top+1;
  947.   rMessage.right  = r.right-wScoreWidth;
  948.   rMessage.bottom = r.bottom-1;
  949.  
  950.   rWhiteScoreBmp.left   = r.right-wScoreWidth+10;
  951.   rWhiteScoreBmp.top    = r.top+2;
  952.   rWhiteScoreBmp.bottom = r.bottom-2;
  953.   rWhiteScoreBmp.right  = rWhiteScoreBmp.left+r.bottom-r.top-4;
  954.   rWhiteScore.left = rWhiteScoreBmp.right+2;
  955.   rWhiteScore.top  = rMessage.top;
  956.   rWhiteScore.bottom = rMessage.bottom;
  957.   rWhiteScore.right = rWhiteScore.left
  958.               + LOWORD(GetTextExtent(PaintDC,"999",3));
  959.  
  960.   rBlackScore.right = r.right-2;
  961.   rBlackScore.left = rBlackScore.right
  962.                  -LOWORD(GetTextExtent(PaintDC,"999",3))-2;
  963.   rBlackScore.top  = rMessage.top;
  964.   rBlackScore.bottom = rMessage.bottom;
  965.   rBlackScoreBmp.right   = rBlackScore.left-2;
  966.   rBlackScoreBmp.top    = r.top+2;
  967.   rBlackScoreBmp.bottom = r.bottom-2;
  968.   rBlackScoreBmp.left  = rBlackScoreBmp.right-r.bottom+r.top+4;
  969.  
  970.   SetBkColor(PaintDC,RGB(192,192,192));
  971.   if ( bReFresh || nOrigWhite == Board.nWhiteScore )
  972.   {
  973.     hOldPen = SelectPen(PaintDC,hGrayPen);
  974.  
  975.     MoveTo(PaintDC,rMessage.right,rMessage.top);
  976.     LineTo(PaintDC,rMessage.right,rMessage.bottom+1);
  977.  
  978.     SelectPen(PaintDC,hWhitePen);
  979.  
  980.     MoveTo(PaintDC,rMessage.right+1,rMessage.top);
  981.     LineTo(PaintDC,rMessage.right+1,rMessage.bottom+1);
  982.  
  983.     SelectPen(PaintDC,hOldPen);
  984.  
  985.     hBitmap = LoadBitmap(ghInstance, "WHITE");
  986.     // Get dimensions of bitmap
  987.     GetObject(hBitmap, sizeof(BM), &BM);
  988.  
  989.     // Create compatible display context
  990.     hCompatDC = CreateCompatibleDC(PaintDC);
  991.  
  992.     // Select bitmap into the compataible DC
  993.     hOldBitmap = SelectBitmap(hCompatDC, hBitmap);
  994.  
  995.     // Display bitmap in client area
  996.     StretchBlt(PaintDC, rWhiteScoreBmp.left, rWhiteScoreBmp.top,
  997.                rWhiteScoreBmp.right-rWhiteScoreBmp.left,
  998.            rWhiteScoreBmp.bottom-rWhiteScoreBmp.top,
  999.                hCompatDC, 0, 0, BM.bmWidth, BM.bmHeight,
  1000.                SRCCOPY);
  1001.     SelectBitmap(hCompatDC, hOldBitmap);
  1002.     DeleteDC(hCompatDC);
  1003.     DeleteBitmap(hBitmap);
  1004.  
  1005.     hBitmap = LoadBitmap(ghInstance, "BLACK");
  1006.     GetObject(hBitmap, sizeof(BM), &BM);
  1007.  
  1008.     hCompatDC = CreateCompatibleDC(PaintDC);
  1009.     // Select bitmap into the compataible DC
  1010.     hOldBitmap = SelectBitmap(hCompatDC, hBitmap);
  1011.  
  1012.     // Display bitmap in client area
  1013.     StretchBlt(PaintDC, rBlackScoreBmp.left, rBlackScoreBmp.top,
  1014.            rBlackScoreBmp.right-rBlackScoreBmp.left,
  1015.            rBlackScoreBmp.bottom-rBlackScoreBmp.top,
  1016.                hCompatDC, 0, 0, BM.bmWidth, BM.bmHeight,
  1017.                SRCCOPY);
  1018.     // De-select the bitmap
  1019.     SelectBitmap(hCompatDC, hOldBitmap);
  1020.  
  1021.     // Clean up after we are done
  1022.     DeleteDC(hCompatDC);
  1023.     DeleteBitmap(hBitmap);
  1024.   }
  1025.   if (Player == 1)
  1026.     DrawText(PaintDC,"White's Turn  ",14,&rMessage,DT_SINGLELINE | DT_BOTTOM | DT_LEFT);
  1027.   else
  1028.     DrawText(PaintDC,"Black's Turn  ",14,&rMessage,DT_SINGLELINE | DT_BOTTOM | DT_LEFT); 
  1029.  
  1030.   DrawSquare(PaintDC,rWhiteScore,-1);
  1031.   DrawSquare(PaintDC,rBlackScore,-1);
  1032.   SetBkMode(PaintDC,TRANSPARENT);
  1033.   itoa(Board.nWhiteScore,cszWhite,10);
  1034.   DrawText(PaintDC,cszWhite,strlen(cszWhite),
  1035.        &rWhiteScore,DT_SINGLELINE | DT_BOTTOM | DT_CENTER);
  1036.  
  1037.   itoa(Board.nBlackScore,cszBlack,10);
  1038.   DrawText(PaintDC,cszBlack,strlen(cszBlack),
  1039.        &rBlackScore,DT_SINGLELINE | DT_BOTTOM | DT_CENTER);
  1040.   SetBkMode(PaintDC,OPAQUE);
  1041.   DeletePen(hWhitePen);
  1042.   DeletePen(hGrayPen);
  1043. }
  1044.  
  1045. void TMainWindow::WMTimer(RTMessage)
  1046. {
  1047.   if ( (Player == 1 && Player1 == 2) || (Player == 2 && Player2 == 2) )
  1048.   {
  1049.     SetCursor(LoadCursor(NULL,IDC_WAIT));
  1050.     Board.GoComputer(Player,1);
  1051.     SetCursor(LoadCursor(NULL,IDC_ARROW));
  1052.   }
  1053. }
  1054.  
  1055. #pragma argsused
  1056. long double TBoard::GoComputer(int Player, int Level)
  1057. {
  1058.   int nCol, nRow;
  1059.   int nOther,nVect,j,k,nVectCnt;
  1060.   long double MoveVal, MoveCnt, nOldMult, MaxVal=-100000000;
  1061.   BOOL ValMove=FALSE;
  1062.   TUndo SaveBoard,MoveBoard;
  1063.  
  1064.   POINT MaxMove; 
  1065.  
  1066.   Board2Undo(Board,SaveBoard);
  1067.   nOther = (Player==1) ? 2 : 1;
  1068.   nOldMult = 0;
  1069.  
  1070.   j=0;
  1071.   do
  1072.   {
  1073.     if ( Weight[j].Mult==0 ) { j++; continue; }
  1074.  
  1075.     if ( MaxVal > 10*Weight[j].Mult
  1076.      && ValMove==TRUE && nOldMult != Weight[j].Mult )
  1077.        break;
  1078.  
  1079.     nOldMult = Weight[j].Mult;
  1080.  
  1081.     do
  1082.     {
  1083.       nCol = Weight[j].x;
  1084.       nRow = Weight[j].y;
  1085.  
  1086.       if ( BoardArr[nCol][nRow] == 0 )
  1087.       {
  1088.         MoveCnt = 0;
  1089.         MoveVal = 0;
  1090.  
  1091.         for ( nVect=0;nVect<8;nVect++ )
  1092.         {
  1093.           nVectCnt = ChkVector(NULL,nCol,nRow,nVect,nOther,Player,TRUE);
  1094.           MoveCnt += (nVectCnt > 0) ? nVectCnt : 0 ;
  1095.         }
  1096.  
  1097.  
  1098.         if ( MoveCnt > 0 )
  1099.         {
  1100.           MoveVal = MoveCnt*Weight[j].Mult;
  1101.       //MoveVal = powl(MoveVal,Weight[j].Mult);
  1102.       BoardArr[nCol][nRow] = Player;
  1103.       if (Level < Difficulty*2-2 )
  1104.         MoveVal -= GoComputer(nOther,Level+1);
  1105.  
  1106.       if ( MoveVal > MaxVal || (MoveVal == MaxVal && random(10) > 4 ))
  1107.       {
  1108.         MaxVal = MoveVal;
  1109.         MaxMove.x = nCol; MaxMove.y = nRow;
  1110.         ValMove=TRUE;
  1111.       }
  1112.  
  1113.     }
  1114.       }
  1115.       Undo2Board(SaveBoard,Board);
  1116.       j++;
  1117.     } while (j<BoardSize*BoardSize && nOldMult == Weight[j].Mult);
  1118.   } while (j<BoardSize*BoardSize);
  1119.   //See if the computer could move...
  1120.   if ( ValMove == FALSE )
  1121.   {
  1122.     MaxVal = 0;
  1123.     Player = nOther;
  1124.     bFlip = FALSE;
  1125.   }
  1126.   else
  1127.     if (Level == 1)
  1128.     {
  1129.       BoardArr[MaxMove.x][MaxMove.y] = Player;
  1130.       LastMove.x = MaxMove.x ; LastMove.y = MaxMove.y;
  1131.       bFlip = TRUE;
  1132.       nPass = 0;
  1133.       InvalidateRect(ghWindow,NULL,FALSE);
  1134.     }
  1135.   return MaxVal;
  1136. }
  1137.  
  1138. void Board2Undo( TBoard& b, TUndo& u)
  1139. {
  1140.   int i,j;
  1141.  
  1142.   memcpy(u.UndoArr,b.BoardArr,sizeof(b.BoardArr));
  1143.  
  1144.   u.UndoPlayer = Player;
  1145.   u.nUndoPass = nPass;
  1146.   u.nUndoWhiteScore = b.nWhiteScore;
  1147.   u.nUndoBlackScore = b.nBlackScore;
  1148.   u.UndoLastMove.x = LastMove.x;
  1149.   u.UndoLastMove.y = LastMove.y;
  1150. }
  1151.  
  1152. void Undo2Board( TUndo& u, TBoard& b)
  1153. {
  1154.   int i,j;
  1155.  
  1156.   memcpy(b.BoardArr,u.UndoArr,sizeof(b.BoardArr));
  1157.  
  1158.   Player = u.UndoPlayer;
  1159.   nPass = u.nUndoPass;
  1160.   b.nWhiteScore = u.nUndoWhiteScore;
  1161.   b.nBlackScore = u.nUndoBlackScore;
  1162.   LastMove.x = u.UndoLastMove.x;
  1163.   LastMove.y = u.UndoLastMove.y;
  1164. }
  1165.  
  1166. istream& operator >> ( istream& is, TBoard& bd )
  1167. {
  1168.   int i,j;
  1169.  
  1170.   for (i=0;i<12;i++)
  1171.     for (j=0;j<12;j++)
  1172.      is >> bd.BoardArr[i][j];
  1173.  
  1174.   is >> bd.bFlip;
  1175.  
  1176.   for (i=0;i<8;i++)
  1177.     is >> bd.Vectors[i];
  1178.  
  1179.   for (j=0;j<100;j++)
  1180.   {
  1181.      is >> bd.Weight[j].Mult;
  1182.      is >> bd.Weight[j].x;
  1183.      is >> bd.Weight[j].y;
  1184.   }
  1185.   is >> bd.nWhiteScore;
  1186.   is >> bd.nBlackScore;
  1187.   return is;
  1188. }
  1189.  
  1190. ostream& operator << ( ostream& os, TBoard& bd )
  1191. {
  1192.   int i,j;
  1193.   for (i=0;i<12;i++)
  1194.     for (j=0;j<12;j++)
  1195.      os << bd.BoardArr[i][j];
  1196.  
  1197.   os << bd.bFlip;
  1198.   os << ' ';
  1199.  
  1200.   for (i=0;i<8;i++)
  1201.     os << bd.Vectors[i];
  1202.  
  1203.   for (j=0;j<100;j++)
  1204.   {
  1205.      os << bd.Weight[j].Mult << ' ';
  1206.      os << bd.Weight[j].x << ' ';
  1207.      os << bd.Weight[j].y << ' ';
  1208.   }
  1209.   os << bd.nWhiteScore;
  1210.   os << ' ';
  1211.   os << bd.nBlackScore;
  1212.   os << ' ';
  1213.   return os;
  1214. }
  1215.  
  1216. istream& operator >> ( istream& is, TUndo& u )
  1217. {
  1218.   int i,j;
  1219.  
  1220.   for (i=0;i<12;i++)
  1221.     for (j=0;j<12;j++)
  1222.      is >> u.UndoArr[i][j];
  1223.  
  1224.   is >> u.UndoPlayer;
  1225.   is >> u.nUndoPass;
  1226.   is >> u.nUndoWhiteScore;
  1227.   is >> u.nUndoBlackScore;
  1228.   is >> u.UndoLastMove;
  1229.  
  1230.   return is;
  1231. }
  1232.  
  1233. ostream& operator << ( ostream& os, TUndo& u )
  1234. {
  1235.   int i,j;
  1236.   for (i=0;i<12;i++)
  1237.     for (j=0;j<12;j++)
  1238.      os << u.UndoArr[i][j];
  1239.  
  1240.   os << u.UndoPlayer;
  1241.   os << ' ';
  1242.   os << u.nUndoPass;
  1243.   os << ' ';
  1244.   os << u.nUndoWhiteScore;
  1245.   os << ' ';
  1246.   os << u.nUndoBlackScore;
  1247.   os << ' ';
  1248.   os << u.UndoLastMove;
  1249.  
  1250.   return os;
  1251. }
  1252. istream& operator >> ( istream& is, POINT& pt )
  1253. {
  1254.   int x,y;
  1255.  
  1256.   is >> x;
  1257.   is >> y;
  1258.  
  1259.   pt.x = x;
  1260.   pt.y = y;
  1261.  
  1262.   return is;
  1263. }
  1264.  
  1265. ostream& operator << ( ostream& os, POINT& pt )
  1266. {
  1267.   int x,y;
  1268.  
  1269.   x = pt.x;
  1270.   y = pt.y;
  1271.  
  1272.   os << x;
  1273.   os << ' ';
  1274.   os << y;
  1275.   os << ' ';
  1276.  
  1277.   return os;
  1278. }
  1279.  
  1280. int TMainWindow::WMSetFocus(RTMessage)
  1281. {
  1282.   KillTimer(ghWindow,10);
  1283.   SetTimer(ghWindow,10,1500,NULL);
  1284.   bReFresh=TRUE;
  1285.   return 0;
  1286. }
  1287.  
  1288. int TMainWindow::WMKillFocus(RTMessage Msg)
  1289. {
  1290.   WMInitMenu(Msg);
  1291.   return 0;
  1292. }
  1293.  
  1294. void TMainWindow::WMInitMenu(RTMessage)
  1295. {                  
  1296.   KillTimer(ghWindow,10);
  1297. }
  1298.  
  1299. int TMainWindow::WMMenuSelect(RTMessage Msg)
  1300. {
  1301.   if (Msg.LP.Hi == 0)
  1302.     WMSetFocus(Msg);
  1303.  
  1304.   return 0;
  1305. }
  1306.  
  1307. void TMainWindow::OpenFiFile( LPSTR lpstrFile )
  1308. {
  1309.    ifstream in( lpstrFile );    // open the input file
  1310.    in >> Board;                  // read the Todo list
  1311.    in >> Undo;
  1312.    in >> Player;
  1313.    in >> Player1;
  1314.    in >> Player2;
  1315.    in >> nPass;
  1316.    in >> NumPlayers;
  1317.    in >> BoardSize;
  1318.    in >> LastMove;
  1319.    in >> Difficulty;
  1320.    in.close();
  1321. }
  1322.  
  1323. void TMainWindow::SaveFiFile( LPSTR lpstrFile )
  1324. {
  1325.    ofstream out( lpstrFile );
  1326.    out << Board;
  1327.    out << Undo;
  1328.    out << Player << ' ';
  1329.    out << Player1 << ' ';
  1330.    out << Player2 << ' ';
  1331.    out << nPass << ' ';
  1332.    out << NumPlayers << ' ';
  1333.    out << BoardSize << ' ';
  1334.    out << LastMove;
  1335.    out << Difficulty;
  1336.    out.close();
  1337. }
  1338. //REGEN_CUSTOMCODE
  1339.