Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Use of objects and associated performance hit?

Author: Alexander Kure

Date: 06:41:15 12/31/99

Go up one level in this thread


Hi Tom,

An Engine uses a board, it is definitely not a board. If you want your board to
be exclusively used by an engine then make it a member (has a relation) rather
than inherit from board. I think your problem is, that the engine should have
direct access to a board's private data members, ie. the board representation.
This is always an indication for a bad design breaking the rule of data
encapsulation and should be avoided. You have to grant access to private data of
a class through member functions in the first way (set and get methods) which
you can inline. On rare occasion you can declare a class as friend for a second
class, thus enabling the first class to directly access private data of the
second class.

I include a short sample which i think shows how to do it in an object
orientated way.
There a classes for a move, a board, an evaluation and an engine.
Have fun!

class CMove
{
    private:
        int _from;
        int _to;

    public:
        CMove(int from, int to): _from(from), _to(to) {}
        ~CMove() {}
        int GetFrom() const {return _from;}
        int GetTo () const {return _to;}
        void Set(int from, int to) {_from = from; _to = to;}

};

class CBoard
{
    private:
        // board representation
        enum{BOARDSIZE = 64};
        int _board[BOARDSIZE];
        int _color[BOARDSIZE];

    public:
        CBoard() {Init();}
        ~CBoard() {}
        void Init() {/*...*/}
        bool MakeMove(const CMove &)
        {
            bool correct = false;

            // check and moving here ...

            return(correct);
        }
        void Takeback(const CMove &) {/*...*/}
};

class CEvaluation
{
    public:
        int EvaluatePosition(const CBoard &)
        {
	    int eval = 0;

            // eval stuff goes here ...

            return(eval);
        }
};

class CEngine
{
    private:
        CBoard _board;

    public:
        CEngine() {}
        ~CEngine() {}
        int Search(int depth)
        {
            int score;
            if (depth == 0)
            {
                CEvaluation ObEval;
                score = ObEval.EvaluatePosition(_board);
            }
            else
            {
                // e2-e4
                CMove m(0x41, 0x42);
                // ..
                if (_board.MakeMove(m))
                {
                    // do another search at depth - 1 ...
                    _board.Takeback(m);
                }
                // ..
            }

            return(score);
        }
};

int main()
{
    CEngine ObEngine;

    // ...
    ObEngine.Search(0);
    // ...

    return(0);
}


Greetings
Alex



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.