1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
class AbstractNode {
protected:
int value, x, y;
AbstractNode (int X, int Y): x (X), y (Y) {}
void applyChanges() {establishParents(); updateValue(); createChildren();} // Facade Pattern Method
public:
int Value() const {return value;}
private:
virtual void establishParents() = 0;
virtual void updateValue() = 0;
virtual void createChildren() = 0;
};
class AbstractCheckerBoard {
protected:
const int dimension_x, dimension_y, start_x, start_y;
AbstractCheckerBoard (int X, int Y, int startX, int startY): dimension_x (X), dimension_y (Y), start_x (startX), start_y (startY) {}
public:
int Dimension_x() const {return dimension_x;}
int Dimension_y() const {return dimension_y;}
virtual AbstractNode* NodesFound (int i, int j) const = 0;
virtual void display (int space);
};
class CheckerBoard: public AbstractCheckerBoard {
private:
class Node: public AbstractNode {
private:
CheckerBoard& checkerBoard; // reference data member because a CheckerBoard::Node always belongs to a CheckerBoard
Node *leftChild, *rightChild, *leftParent, *rightParent;
friend class CheckerBoard;
Node (CheckerBoard& cb, int X, int Y); // Many private functions, including the constructor.
~Node () {} virtual inline void establishParents();
virtual inline void updateValue();
virtual inline void checkLeftChild();
virtual inline void checkRightChild();
virtual inline void createChildren();
};
public:
CheckerBoard (int X, int Y, int startX, int startY);
// ~CheckerBoard();
Node*** NodesFound() const {return nodesFound;}
virtual AbstractNode* NodesFound (int i, int j) const {return nodesFound[i][j];}
private:
Node *startingPoint;
Node ***nodesFound; /* This 2-dimensional array of Node*'s is to search all Nodes found so that the latest constructed Node with the same coordinates as some Node already found
would be made equal to that Node. */
};
class CheckerBoardUpAllowed: public AbstractCheckerBoard {
private:
class Node: public AbstractNode {
private:
CheckerBoardUpAllowed& checkerBoard; // reference data member because a CheckerBoardUpAllowed::Node always belongs to a CheckerBoardUpAllowed
Node *leftChild, *rightChild, *topChild, *leftParent, *rightParent, *bottomParent;
friend class CheckerBoardUpAllowed;
Node (CheckerBoardUpAllowed& cb, int X, int Y); // Many private functions, including the constructor.
~Node () {}
virtual inline void establishParents();
virtual inline void updateValue();
virtual inline void checkLeftChild();
virtual inline void checkRightChild();
virtual inline void checkTopChild();
virtual inline void createChildren();
};
public:
CheckerBoardUpAllowed (int X, int Y, int startX, int startY);
// ~CheckerBoardUpAllowed();
Node*** NodesFound() const {return nodesFound;}
virtual AbstractNode* NodesFound (int i, int j) const {return nodesFound[i][j];}
private:
Node *startingPoint;
Node ***nodesFound;
};
|