home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / BCPPOWL1.ZIP / CHECKERS.ZIP / BOARD.H < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  8.6 KB  |  340 lines

  1. // ObjectWindows - (C) Copyright 1991 by Borland International
  2.  
  3. #ifndef _BOARD_H
  4. #define _BOARD_H
  5.  
  6. #include <stack.h>
  7.  
  8. #define  MAXINT        32000
  9. #define  WIN           20000
  10. #define  LOSS          -20000
  11.  
  12. #define  CON( s )      (Board[ (s).GetRow() ][ (s).GetCol() ].What)
  13. #define  OPLAYER       ((player == Red) ? Black : Red)
  14.  
  15. #define SQUARE_SIZE  36
  16. #define PIECE_OFFSET  2
  17. #define BLACK  0x0L
  18. #define RED    0x00000080L
  19.  
  20. #define  REDMAN        0      
  21. #define  REDKING       1
  22. #define  BLACKMAN      2
  23. #define  BLACKKING     3
  24. #define  EMPTY         4
  25. #define  OFFBOARD      5
  26.  
  27.  
  28. #define  MAXBDSIZE     8           // maximum board size 
  29. #define  MAXMOVES      65          // maximum number of moves to consider 
  30. #define  MAXPLY        33          // maximum depth of tree search 
  31.  
  32. #define BORDERSIZE   20          // offset from upper left corner of window
  33.  
  34. HBITMAP   RedManBmp, BlackManBmp, RedKingBmp, BlackKingBmp;
  35.  
  36.  
  37.  
  38. _CLASSDEF(Square)
  39.  
  40. class Square
  41. {
  42.    int Row;              // Row position on board
  43.    int Col;              // column position on board
  44.    POINT UpperLeftPos;   // actual point on the screen
  45.    HBRUSH hBrush;        // this squares color
  46.    int Value;            // this squares value
  47.   public:
  48.    void SetColor(DWORD clr)
  49.       {
  50.       hBrush = CreateSolidBrush(clr);
  51.       }
  52.    void ClearSquare(HDC hDC);
  53.    void SetValue(int val)
  54.    {
  55.       Value = val;
  56.    }
  57.    int GetValue()
  58.    {
  59.       return Value;
  60.    }
  61.    POINT& GetUpperLeftPos()
  62.       {
  63.       return UpperLeftPos;
  64.       }
  65.    void SetRow(int i) { Row = i; }
  66.    void SetCol(int j) { Col = j; }
  67.    GetRow() {
  68.       return Row;
  69.       }
  70.    GetCol() {
  71.       return Col;
  72.    }
  73.    void SetUpperLeftPos(POINT& p)
  74.    {
  75.          UpperLeftPos.x = p.x + BORDERSIZE;
  76.          UpperLeftPos.y = p.y + BORDERSIZE;
  77.    }
  78.    BOOL HasPoint(POINT& p)
  79.    {
  80.       if (p.x >= UpperLeftPos.x && p.x <= UpperLeftPos.x + SQUARE_SIZE
  81.          && p.y >= UpperLeftPos.y && p.y <= UpperLeftPos.y + SQUARE_SIZE)
  82.          return TRUE;
  83.       return FALSE;
  84.    }
  85.    void FreeBrush()
  86.     {
  87.       DeleteObject(hBrush);
  88.    }
  89. };
  90.  
  91. _CLASSDEF(Piece)
  92.  
  93. class Piece
  94. {
  95.    SIDE Side;               // which side is this piece on
  96.    int Value;               // whats the value of this piece
  97.    HBITMAP hBitmap;         // handle to a bitmap for this piece
  98.    BITMAP Bitmap;           // the bitmap that that handle handles
  99.   public:
  100.    Piece()
  101.    {
  102.       Side = Unknown;
  103.       hBitmap = 0;
  104.    }
  105.    void SetSide(SIDE s)
  106.       {
  107.       Side = s;
  108.       }
  109.    SIDE GetSide()
  110.    {
  111.       return Side;
  112.    }
  113.    void SetValue(int val) { Value = val; }
  114.    GetValue() { return Value; }
  115.    void SetBitmap(HBITMAP hbp);
  116.    void DrawPiece(HDC hDC, POINT& p);
  117.    void operator --() { Value--; }
  118. };
  119.  
  120.  
  121. struct MOVE          
  122. {
  123.    Square org, dest, capt;  // origin, destination, capture squares
  124.    BYTE Capture;   
  125.          // Has someone been captured?
  126.    BYTE Crown;              // the king is dead, long live the king
  127.    int Value;               // value of this move
  128.     inline BOOL CompMoves( MOVE &m2 );
  129.    void Stoa(char *str, Square &squ)
  130.       {
  131.       sprintf(str, "%c%d%c", (char)(squ.GetCol() + 64), squ.GetRow(), (char) 0);
  132.       }
  133.    void MoveToStr(char *str)
  134.    {
  135.       Stoa(str, org);
  136.       if (Capture != EMPTY )
  137.          strcat(str, "x");
  138.       else
  139.          strcat(str, "-");
  140.  
  141.       Stoa(str + strlen(str), dest);
  142.  
  143.       if (Crown)
  144.          strcat(str, "!");
  145.    }
  146.    int Quiescent(int limit, int num_moves, int ply)
  147.    {
  148.       if( ply == MAXPLY - 1 )
  149.          return( TRUE );
  150.       else
  151.          {
  152.          if( limit > 0 || Capture != EMPTY || num_moves == 1 )  
  153.             return( FALSE );
  154.          else
  155.             return( TRUE );
  156.          }
  157.    }
  158. };
  159.  
  160. _CLASSDEF(UndoObject)
  161. class UndoObject : public Object
  162. {
  163.    MOVE move;
  164.   public:
  165.    UndoObject(MOVE &m)
  166.    {
  167.       move = m;
  168.    }
  169.    MOVE GetMove()
  170.    {
  171.        return move;
  172.    }
  173.    virtual classType isA() const
  174.    {
  175.         return __firstUserClass;
  176.    }
  177.    virtual Pchar nameOf() const
  178.    {
  179.        return "UndoObject";
  180.    }
  181.    virtual int isEqual(const Object& m) const
  182.    {
  183.       return memcmp(this, &m, sizeof(UndoObject)) == 0;
  184.    }
  185.    virtual hashValueType hashValue() const 
  186.    { return 0; }
  187.    virtual void printOn(Rostream) const
  188.    { }
  189. };
  190.  
  191. struct POSTYPE
  192. {
  193.    Square Sq;
  194.    int What; 
  195. };
  196.  
  197. typedef POSTYPE *PPOSTYPE;
  198.  
  199. struct REDRAWLIST;
  200. typedef REDRAWLIST *PREDRAWLIST;
  201.  
  202.  
  203. struct REDRAWLIST
  204. {
  205.    POINT p;
  206.    PREDRAWLIST next;
  207. };
  208.  
  209.  
  210. _CLASSDEF(BOARD)
  211.  
  212. class BOARD
  213. {
  214.     friend MOVE;         
  215.     PTInfoWindow TInfo;     // handle to information window 
  216.    PREDRAWLIST Redraw;     // linked list of square to be redrawn    
  217.    int  SearchDepth;       // user-selected depth of search          
  218.    int  IterFlag;          // flag, TRUE for iterative deepening     
  219.    int  KillerFlag;        // flag, TRUE for Killer move table       
  220.    int  BoardSize;         // usually 8x8                            
  221.    int  Material[ 2 ];     // value of each players material        
  222.    int  JumpAgain;         // flag, TRUE if human must jump again    
  223.    long Nodes;             // number of nodes in the search          
  224.    Square LastDest;        // destination square of humans last move 
  225.    time_t start_t, end_t;  // start and end times for the search     
  226.    time_t ComputerTotalTime;  // total time used by computer to select move 
  227.    time_t UserTotalTime;   // total time used by user to select move 
  228.    PPiece Man;             // Pointer to array of class Piece        
  229.    POSTYPE Board[MAXBDSIZE+2][MAXBDSIZE+2];  // What each square holds and  
  230.                                          // its screen location         
  231.    int SValue[MAXBDSIZE+1][MAXBDSIZE+1][4];  // Square Values;              
  232.    POSTYPE SavedBoard[MAXBDSIZE+2][MAXBDSIZE+2]; // Last position before computers
  233.                                              // search began
  234.     RECT BoardRect;                       // Dimensions of checker board 
  235.    BOOL StopSearch;                      // user requests stop of search
  236.    int NumBlackPieces;  // any black pieces left?
  237.    int NumRedPieces;    // any red pieces left?
  238.    BOOL Logging;        // Log moves?
  239.    ofstream *olog;      // log stream
  240.    Stack UndoStack;
  241.    Stack RedoStack;
  242.    int TotalMoves;
  243.     int Lmg(MOVE *list, SIDE player, int row, int col, int ply);
  244.     int GenJumps(MOVE *list, int player, int row, int col);
  245.     void UnMakeMove( MOVE &m );
  246.     int GenMoves( MOVE *list, int row, int col );
  247.     BOOL CanJump( SIDE player, int row, int col );
  248.    BOOL OnlyOneMove( SIDE player, int *row, int *col );
  249.    void PreEvaluate();
  250.     void MakeMove(MOVE&);
  251.    void MakeActualMove(MOVE&);
  252.    void UnMakeActualMove(MOVE&);
  253.    int Evaluate( int alpha, int beta, SIDE player, int row, int col, int limit, int ply );
  254.    inline void MessageScan();
  255.    void Log(MOVE&, BOOL);
  256.    void Logon();
  257.    void Logoff();
  258.    void ClearRedoStack();
  259.    void ClearUndoStack();
  260.   public:
  261.    BOARD();
  262.    ~BOARD();
  263.     void SetInfoPtr(PTInfoWindow TI)
  264.         {
  265.         TInfo = TI;
  266.         }
  267.    void SetupBoard();
  268.    void DrawBoard(HDC);
  269.    void DrawLastBoard(HDC);
  270.     void DisplaySearchStats(int, int, long, double);
  271.    void DrawPiece(HDC hDC, int what, POINT& p)
  272.    {
  273.    /*
  274.     *  Point p is the i, j point on the board, not
  275.     *  the screen coordinates
  276.     */
  277.       Man[what].DrawPiece(hDC, Board[p.x][p.y].Sq.GetUpperLeftPos());
  278.    }
  279.  
  280.    void ClearSquare(HDC hDC, POINT& Where)
  281.    {
  282.       Board[ Where.x ][ Where.y ].Sq.ClearSquare(hDC);
  283.    }
  284.    BOOL IsValidSquare(POINT&);
  285.    int GetPieceType(POINT& Where)
  286.    {
  287.       return Board[ Where.x ][ Where.y ].What;
  288.    }
  289.    POINT GetValidSquare(POINT& p, SIDE s);
  290.    POINT GetEmptySquare(POINT& p);
  291.     void DrawCheckersFrame(HDC hDC);
  292.    void RedrawBoard(HDC);
  293.     BOOL UserMove(POINT& from, POINT& to);
  294.    void InsertRedrawPoint(POINT& p);
  295.    void DeleteRedrawPoint(POINT& p);
  296.    void ReleaseRedraw();
  297.    BOOL GetNextRedrawPoint(POINT *);
  298.    BOOL ComputersTurn();
  299.     void DrawAlphaNum(HDC);
  300.    BOOL AnotherJump()
  301.    {
  302.       return JumpAgain;
  303.    }
  304.    void StartUsersTime();
  305.    void EndUsersTime();
  306.    void SaveGame(char *);
  307.    void LoadGame(char *);
  308.    void ToggleIter()
  309.    {
  310.       IterFlag = !IterFlag;
  311.    }
  312.    void ToggleKiller()
  313.    {
  314.       KillerFlag = !KillerFlag;
  315.    }
  316.    void SetSearchDepth(int);
  317.    BOOL NoMoreBlack()
  318.    {
  319.       return (!NumBlackPieces);
  320.    }
  321.    BOOL NoMoreRed()
  322.    {
  323.       return (!NumRedPieces);
  324.    }
  325.    void ToggleLogging()
  326.    {
  327.       if (Logging)
  328.          Logoff();
  329.       else
  330.          Logon();
  331.    }
  332.    BOOL RedoMove();
  333.    BOOL UndoMove();
  334. };
  335.  
  336. extern void DrawFrame(HDC hDC, RECT& iRect);
  337.  
  338. #endif  // _BOARD_H
  339.  
  340.