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

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