home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CHECKERS.ZIP / CHECKERS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  13.5 KB  |  468 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. #include <static.h>
  4. #include <filedial.h>
  5. #include <inputdia.h>
  6. #include <bwcc.h>
  7.  
  8. #include "checkers.h"
  9. #include "info.h"
  10. #include "board.h"
  11.  
  12. short LINESIZE;
  13. short CHARSIZE;
  14. int CAPTIONY;
  15. int BORDERSIZE;
  16. const BORDERYEXTRA = 4; // 4 for spacing
  17. const MYFRAMESIZE = 3;
  18. short INFOXSIZE, INFOYSIZE;
  19.  
  20. #undef MAXPATH
  21. #define MAXPATH  160
  22.  
  23. HBITMAP RedManBmp, BlackManBmp, RedKingBmp, BlackKingBmp;
  24.  
  25.  
  26. // Must overload TDialog to handle IDYES and IDNO messages
  27. class TEndDialog : public TDialog
  28. {
  29.   public:
  30.    TEndDialog(PTWindowsObject AParent, LPSTR AName)
  31.       : TDialog(AParent, AName)
  32.       { }
  33.  
  34.    virtual void Yes(RTMessage) = [ID_FIRST+IDYES]
  35.    {
  36.       CloseWindow(IDYES);
  37.    }
  38.    virtual void No(RTMessage) = [ID_FIRST+IDNO]
  39.    {
  40.       ShutDownWindow();
  41.    }
  42. };
  43.  
  44. class TCheckers: public TApplication
  45. {
  46.   public:
  47.    TCheckers(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  48.       LPSTR lpCmdLine, int nCmdShow) : TApplication(AName, hInstance,
  49.       hPrevInstance, lpCmdLine, nCmdShow)
  50.       {
  51.       }
  52.    virtual void InitMainWindow();
  53.    void InitInstance()
  54.    {
  55.       TApplication::InitInstance();
  56.       HAccTable = LoadAccelerators(hInstance, "CheckerCommands");
  57.       BWCCGetVersion();
  58.    }
  59. };
  60.  
  61.  
  62.  
  63. _CLASSDEF(TCheckersWindow)
  64. class TCheckersWindow : public TWindow
  65. {
  66.    PTInfoWindow TInfo;
  67.    PBOARD bd;
  68.    HCURSOR CursorHand, CursorPiece;
  69.    BOOL HoldingPiece;
  70.    SIDE WhoseTurn;
  71.    int MovingPieceType;
  72.    POINT MoveStartPoint, MoveEndPoint;
  73.    RECT MainWndRect;
  74.    RECT InfoAreaRect;
  75.    BOOL NewGame;
  76.    char *FileName;
  77.    HMENU hMenu;
  78.   public :
  79.    TCheckersWindow(PTWindowsObject AParent, LPSTR ATitle);
  80.    ~TCheckersWindow()
  81.    {
  82.       if (bd)
  83.          delete bd;
  84.       delete FileName;
  85.       delete TInfo;
  86.       DeleteObject(RedManBmp);
  87.       DeleteObject(BlackManBmp);
  88.       DeleteObject(RedKingBmp);
  89.       DeleteObject(BlackKingBmp);
  90.    }
  91.    virtual void SetupWindow()
  92.    {
  93.       TWindow::SetupWindow();
  94.       CursorPiece = LoadCursor( GetApplication()->hInstance, "HandWPiece");
  95.       RedManBmp = LoadBitmap(GetApplication()->hInstance, "RedManBitmap");
  96.       BlackManBmp = LoadBitmap(GetApplication()->hInstance, "BlackManBitmap");
  97.       RedKingBmp = LoadBitmap(GetApplication()->hInstance, "RedKingBitmap");
  98.       BlackKingBmp = LoadBitmap(GetApplication()->hInstance, "BlackKingBitmap");
  99.       GetClientRect(HWindow, &MainWndRect);
  100.       InfoAreaRect = MainWndRect;
  101.       InfoAreaRect.left = (MainWndRect.right -= ((BORDERSIZE * 2) + INFOXSIZE));
  102.       hMenu = GetMenu(HWindow);
  103.       bd = new BOARD;
  104.       bd->SetInfoPtr(TInfo);
  105.       bd->SetupBoard();
  106.       WhoseTurn = Red;
  107.       HoldingPiece = FALSE;
  108.       TInfo->SetTurnText("Red");
  109.       NewGame = TRUE;
  110.    }
  111.    virtual void GetWindowClass(WNDCLASS& WndClass)
  112.    {
  113.       TWindow::GetWindowClass( WndClass );
  114.       CursorHand = LoadCursor( GetApplication()->hInstance, "Hand");
  115.       WndClass.hCursor = CursorHand;
  116.       WndClass.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
  117.       WndClass.lpszMenuName = "Checkers";
  118.       WndClass.hIcon = LoadIcon( GetApplication()->hInstance, "CheckersIcon");
  119.    }
  120.    virtual void Paint( HDC PaintDC, PAINTSTRUCT& PaintInfo );
  121.    virtual void WMLButtonDown( TMessage& Message ) = [ WM_FIRST + WM_LBUTTONDOWN ];
  122.    virtual void WMLButtonUp( TMessage& Message ) = [ WM_FIRST + WM_LBUTTONUP ];
  123.    virtual void CMNewGame(RTMessage Msg) = [CM_FIRST + CM_FILENEW];
  124.    virtual void CMRestoreGame(RTMessage Msg) = [CM_FIRST + CM_FILEOPEN];
  125.    virtual void CMSaveGame(RTMessage Msg) = [CM_FIRST + CM_FILESAVE];
  126.    virtual void CMSaveGameAs(RTMessage Msg) = [CM_FIRST + CM_FILESAVEAS];
  127.    virtual void UndoMove(RTMessage Msg) = [CM_FIRST + CM_UNDO];
  128.    virtual void RedoUndo(RTMessage Msg) = [CM_FIRST + CM_REDO];
  129.    virtual void SetSearchDepth(RTMessage Msg) = [CM_FIRST + CM_SEARCHDEPTH];
  130.    virtual void ToggleAutoPlay(RTMessage Msg) = [CM_FIRST + CM_AUTO];
  131.    virtual void ToggleIteration(RTMessage Msg) = [CM_FIRST + CM_ITERATE];
  132.    virtual void ToggleKillerTable(RTMessage Msg) = [CM_FIRST + CM_KILLER];
  133.    virtual void ComputersMove(RTMessage) = [CM_FIRST + CM_MOVE];
  134.    virtual void About(RTMessage Msg) = [CM_FIRST + CM_ABOUT];
  135.    virtual void Logging(RTMessage) = [CM_FIRST + CM_LOG];
  136.    virtual void Exit(RTMessage) = [CM_FIRST + IDM_EXIT]
  137.       {
  138.       CloseWindow();
  139.       }
  140.    void SaveGameAs();
  141.    virtual BOOL CanClose()
  142.    {
  143.       if (WhoseTurn == Black)
  144.          return FALSE;
  145.       return TRUE;
  146.    }
  147. };
  148.  
  149. //->TWindow member functions<----------------------------------------
  150.  
  151. void TCheckers::InitMainWindow()
  152. {
  153.    MainWindow = new TCheckersWindow(NULL, "OWL Checkers");
  154. }
  155.  
  156.  
  157. //->TCheckersWindow member functions<--------------------------------
  158.  
  159. TCheckersWindow::TCheckersWindow(PTWindowsObject AParent, LPSTR ATitle) :
  160.    TWindow(AParent, ATitle)
  161. {
  162.    HDC hDC;
  163.    TEXTMETRIC tm;
  164.  
  165.    hDC = GetDC(HWindow);
  166.    GetTextMetrics(hDC, &tm);
  167.    CHARSIZE = tm.tmAveCharWidth;
  168.    LINESIZE = tm.tmHeight + tm.tmExternalLeading;
  169.    ReleaseDC(HWindow, hDC);
  170.    CAPTIONY = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYMENU);
  171.    BORDERSIZE = LINESIZE + MYFRAMESIZE;
  172.    TInfo = new TInfoWindow(this, "InfoWindow");
  173.    Attr.Style = WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX;
  174.    Attr.X = 5;
  175.    Attr.Y = 5;
  176.    Attr.H = CAPTIONY + (BORDERSIZE * 2) + INFOYSIZE + BORDERYEXTRA +
  177.       (2 * GetSystemMetrics(SM_CYBORDER));
  178.    Attr.W = (BORDERSIZE * 4) + (MAXBDSIZE * SQUARE_SIZE) + INFOXSIZE +
  179.       (2 * GetSystemMetrics(SM_CXBORDER)) + (2*MYFRAMESIZE);
  180.    FileName = new char[MAXPATH];
  181.    bd = NULL;
  182. }
  183.  
  184. void TCheckersWindow::WMLButtonDown( TMessage& )
  185. {
  186.    POINT Point;
  187.    if (WhoseTurn == Black)
  188.       return;
  189.    SetCapture(HWindow);
  190.    GetCursorPos( &Point );
  191.    ScreenToClient(HWindow, &Point);
  192.  
  193.    MoveStartPoint = bd->GetValidSquare(Point, WhoseTurn);
  194.    if (MoveStartPoint.x)
  195.       {
  196.       MovingPieceType = bd->GetPieceType(MoveStartPoint);
  197.       HDC hDC = GetDC(HWindow);
  198.       SetClassWord( HWindow, GCW_HCURSOR, (WORD)CursorPiece);
  199.       SetCursor(CursorPiece);
  200.       bd->ClearSquare(hDC, MoveStartPoint);
  201.       HoldingPiece = TRUE;
  202.       ReleaseDC(HWindow, hDC);
  203.       }
  204. }
  205.  
  206. void TCheckersWindow::WMLButtonUp( TMessage& )
  207. {
  208.    POINT Point;
  209.  
  210.    ReleaseCapture();
  211.    if (!HoldingPiece || WhoseTurn == Black)
  212.       return;
  213.  
  214.    GetCursorPos(&Point);
  215.    ScreenToClient(HWindow, &Point);
  216.    TInfo->SetMessageText("");
  217.    MoveEndPoint = bd->GetEmptySquare(Point);
  218.    HDC hDC = GetDC(HWindow);
  219.    SetClassWord( HWindow, GCW_HCURSOR, (WORD)CursorHand);
  220.    SetCursor(CursorHand);
  221.    if (MoveEndPoint.x && bd->UserMove(MoveStartPoint, MoveEndPoint))
  222.       {
  223.       bd->RedrawBoard(hDC);
  224.       EnableMenuItem( hMenu, CM_UNDO, MF_BYCOMMAND | MF_ENABLED );
  225.       EnableMenuItem( hMenu, CM_REDO, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  226.       if (!bd->AnotherJump())
  227.          {
  228.          if (bd->NoMoreBlack())
  229.             {
  230.          if (GetApplication()->ExecDialog(new TEndDialog(this, "UserWonDlg"))
  231.             == IDYES)
  232.                {
  233.                PostMessage(HWindow, WM_COMMAND, CM_FILENEW, 0L);
  234.                ReleaseDC(HWindow, hDC);
  235.                return;
  236.                }
  237.             else
  238.                {
  239.                PostMessage(HWindow, WM_COMMAND, CM_EXIT, 0L);
  240.                ReleaseDC(HWindow, hDC);
  241.                return;
  242.                }
  243.             }
  244.          PostMessage(HWindow, WM_COMMAND, CM_MOVE, 0L);
  245.          }
  246.       else
  247.          TInfo->SetMessageText("Another jump required");
  248.       }
  249.    else
  250.       {
  251.       TInfo->SetMessageText("Not a legal move!");
  252.       bd->DrawPiece(hDC, MovingPieceType, MoveStartPoint);
  253.       }
  254.    HoldingPiece = FALSE;
  255.    ReleaseDC(HWindow, hDC);
  256. }
  257.  
  258.  
  259.  
  260. void TCheckersWindow::Paint(HDC PaintDC, PAINTSTRUCT&)
  261. {
  262.    DrawFrame(PaintDC, MainWndRect);
  263.    DrawFrame(PaintDC, InfoAreaRect);
  264.    bd->DrawCheckersFrame(PaintDC);
  265.    if (WhoseTurn == Black)  // computer is thinking
  266.       bd->DrawLastBoard(PaintDC);
  267.    else
  268.       bd->DrawBoard(PaintDC);
  269.    bd->DrawAlphaNum(PaintDC);
  270. }
  271.  
  272. void TCheckersWindow::ComputersMove(RTMessage)
  273. {
  274.    bd->EndUsersTime();
  275.    TInfo->SetMessageText("Thinking...");
  276.    HCURSOR hCurTemp;
  277.    SetClassWord(HWindow, GCW_HCURSOR, WORD(hCurTemp =
  278.                                            LoadCursor(0, IDC_WAIT)));
  279.    SetCursor(hCurTemp);
  280.    WhoseTurn = Black;
  281.    TInfo->SetTurnText("Black");
  282.    EnableMenuItem(hMenu, 0, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);
  283.    EnableMenuItem(hMenu, 1, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);
  284.    EnableMenuItem(hMenu, 3, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);
  285.    ModifyMenu( hMenu, CM_MOVE, MF_BYCOMMAND | MF_ENABLED |
  286.            MF_STRING, CM_STOP, "&Stop");
  287.    DrawMenuBar(HWindow);
  288.    bd->ComputersTurn();
  289.    HDC hDC = GetDC(HWindow);
  290.    bd->RedrawBoard(hDC);
  291.    ReleaseDC(HWindow, hDC);
  292.    WhoseTurn = Red;
  293.    TInfo->SetTurnText("Red");
  294.    SetClassWord( HWindow, GCW_HCURSOR, (WORD)CursorHand);
  295.    POINT CursorPoint;
  296.    GetCursorPos(&CursorPoint);
  297.    ScreenToClient(HWindow, &CursorPoint);
  298. #pragma warn -stv
  299.    if (PtInRect(&MainWndRect, CursorPoint))
  300.       SetCursor( CursorHand );
  301. #pragma warn +stv
  302.    ModifyMenu( hMenu, CM_STOP, MF_BYCOMMAND | MF_ENABLED |
  303.            MF_STRING, CM_MOVE, "&Pass");
  304.    EnableMenuItem(hMenu, 0, MF_BYPOSITION | MF_ENABLED);
  305.    EnableMenuItem(hMenu, 1, MF_BYPOSITION | MF_ENABLED);
  306.    EnableMenuItem(hMenu, 3, MF_BYPOSITION | MF_ENABLED);
  307.    DrawMenuBar(HWindow);
  308.  
  309.    if (bd->NoMoreRed())
  310.       {
  311.    if (GetApplication()->ExecDialog(new TEndDialog(this, "GameWonDlg"))
  312.       == IDYES)
  313.           {
  314.           PostMessage(HWindow, WM_COMMAND, CM_FILENEW, 0L);
  315.           return;
  316.           }
  317.        else
  318.           {
  319.           PostMessage(HWindow, WM_COMMAND, CM_EXIT, 0L);
  320.           return;
  321.           }
  322.        }
  323.    TInfo->SetMessageText("");
  324.    bd->StartUsersTime();
  325. }
  326.  
  327. void TCheckersWindow::CMNewGame(RTMessage)
  328. {
  329.    TInfo->Reset();
  330.    bd->SetupBoard();
  331.    EnableMenuItem(hMenu, CM_UNDO, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  332.    EnableMenuItem(hMenu, CM_REDO, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  333.    HoldingPiece = FALSE;
  334.    TInfo->SetTurnText("Red");
  335.    WhoseTurn = Red;
  336.    NewGame = TRUE;
  337.    HDC hDC = GetDC(HWindow);
  338.    bd->DrawBoard(hDC);
  339.    ReleaseDC(HWindow, hDC);
  340. }
  341.  
  342. void TCheckersWindow::CMRestoreGame(RTMessage)
  343. {
  344.    if (GetApplication()->ExecDialog(new TFileDialog(this,
  345.       SD_FILEOPEN, strcpy(FileName, "*.CHK"))) == IDOK)
  346.       {
  347.       bd->LoadGame(FileName);
  348.       NewGame = FALSE;
  349.       EnableMenuItem(hMenu, CM_REDO, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  350.       EnableMenuItem(hMenu, CM_UNDO, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  351.       HDC hDC = GetDC(HWindow);
  352.       bd->DrawBoard(hDC);
  353.       ReleaseDC(HWindow, hDC);
  354.       }
  355. }
  356.  
  357. void TCheckersWindow::SaveGameAs()
  358. {
  359.    if (GetApplication()->ExecDialog(new TFileDialog(this,
  360.       SD_FILESAVE, strcpy(FileName, "*.CHK")))
  361.          == IDOK)
  362.       {
  363.       bd->SaveGame(FileName);
  364.       NewGame = FALSE;
  365.       }
  366. }
  367.  
  368. void TCheckersWindow::CMSaveGame(RTMessage)
  369. {
  370.    if (NewGame == TRUE)
  371.       SaveGameAs();
  372.    else
  373.       bd->SaveGame(FileName);
  374. }
  375.  
  376. void TCheckersWindow::CMSaveGameAs(RTMessage)
  377. {
  378.    SaveGameAs();
  379. }
  380.  
  381. void TCheckersWindow::UndoMove(RTMessage)
  382. {
  383.    EnableMenuItem(hMenu, CM_REDO, MF_BYCOMMAND | MF_ENABLED);
  384.    if (bd->UndoMove())
  385.       EnableMenuItem(hMenu, CM_UNDO, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  386.    HDC hDC = GetDC(HWindow);
  387.    bd->RedrawBoard(hDC);
  388.    ReleaseDC(HWindow, hDC);
  389. }
  390.  
  391. void TCheckersWindow::RedoUndo(RTMessage)
  392. {
  393.    EnableMenuItem(hMenu, CM_UNDO, MF_BYCOMMAND | MF_ENABLED);
  394.    if (bd->RedoMove())
  395.       EnableMenuItem(hMenu, CM_REDO, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  396.    HDC hDC = GetDC(HWindow);
  397.    bd->RedrawBoard(hDC);
  398.    ReleaseDC(HWindow, hDC);
  399. }
  400.  
  401. void TCheckersWindow::SetSearchDepth(RTMessage)
  402. {
  403.  
  404.    char *SearchDepthText;
  405.  
  406.    SearchDepthText = new char[48];
  407.    strcpy(SearchDepthText, "3");
  408.    if (GetApplication()->ExecDialog(new TInputDialog(this, "Set Search Depth", "Enter new search depth:",
  409.       SearchDepthText, 47)) == IDOK)
  410.       {
  411.       int NewSearchDepth = atoi(SearchDepthText);
  412.       bd->SetSearchDepth(NewSearchDepth);
  413.       }
  414.    delete SearchDepthText;
  415. }
  416.  
  417. void TCheckersWindow::ToggleAutoPlay(RTMessage)
  418. {
  419.    // Left as an exersize
  420. }
  421.  
  422. void TCheckersWindow::ToggleIteration(RTMessage)
  423. {
  424.    if (GetMenuState(hMenu, CM_ITERATE, MF_BYCOMMAND) & MF_CHECKED)
  425.       CheckMenuItem(hMenu, CM_ITERATE, MF_UNCHECKED);
  426.    else
  427.       CheckMenuItem(hMenu, CM_ITERATE, MF_CHECKED);
  428.    bd->ToggleIter();
  429.    TInfo->IterReset();
  430. }
  431.  
  432.  
  433. void TCheckersWindow::ToggleKillerTable(RTMessage)
  434. {
  435.    if (GetMenuState(hMenu, CM_KILLER, MF_BYCOMMAND) & MF_CHECKED)
  436.       CheckMenuItem(hMenu, CM_KILLER, MF_UNCHECKED);
  437.    else
  438.       CheckMenuItem(hMenu, CM_KILLER, MF_CHECKED);
  439.    bd->ToggleKiller();
  440.    TInfo->IterReset();
  441. }
  442.  
  443. void TCheckersWindow::Logging(RTMessage)
  444. {
  445.    if (GetMenuState(hMenu, CM_LOG, MF_BYCOMMAND) & MF_CHECKED)
  446.       CheckMenuItem(hMenu, CM_LOG, MF_UNCHECKED);
  447.    else
  448.       CheckMenuItem(hMenu, CM_LOG, MF_CHECKED);
  449.    bd->ToggleLogging();
  450. }
  451.  
  452. void TCheckersWindow::About(RTMessage)
  453. {
  454.    GetApplication()->ExecDialog(new TDialog(this, "About"));
  455. }
  456.  
  457.  
  458. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
  459.    int nCmdShow)
  460. {
  461.    TCheckers CheckersApp("CheckersApp", hInstance, hPrevInstance, lpCmdLine,
  462.       nCmdShow);
  463.    CheckersApp.Run();
  464.    return CheckersApp.Status;
  465. }
  466. 
  467. 
  468.