void expandTree(binaryTreePtr tree) /* This procedure expands a unique tree for the current game state * Pre-condition: the tree has been allocated in memory * Post-condition: a tree has been created for the given game state */ { if (tree != NULL) { //get the current gamestate for the level in tree we are on infoPtr gameInfo = retrieveNodeElm(tree); //store the adress of the current node in the tree treeNodePtr current; current = getCurrentNode(tree); //get the root level's game state nextNode(tree,TOROOT); infoPtr rootInfo = retrieveNodeElm(tree); //go back to current level setCurrentNode(tree,current); //we only extend 2 levels, this counts how many we have done int cmpSquares = squaresAvailable(rootInfo) - squaresAvailable(gameInfo); //is there any sqaures available? if ( (cmpSquares < 3) && (squaresAvailable(gameInfo) != 0) ) { //generate the next level for the tree generateSquares(tree); //go back to the node we started with setCurrentNode(tree, current); //go to the first node on the next level nextNode(tree, TOLEFT); //set this as the current node current = getCurrentNode(tree); //call this procedure again, to expand the next level expandTree(tree); //go back to first node of level we are working with setCurrentNode(tree, current); //does it have a next node on this level? while (existNode(tree, TORIGHT) == 1) { //then go to the next node nextNode(tree, TORIGHT); //get node on this level we are working with current = getCurrentNode(tree); //generate a next level for this node expandTree(tree); //back to working level setCurrentNode(tree, current); } } } }
void Board::init(int board_width, int board_height) { width = board_width; height = board_height; generateSquares(); }