diff --git a/hw2/hw2pr1/.hw2pr1_data.txt b/hw2/hw2pr1/.hw2pr1_data.txt index d96b905..f5cb9f2 100644 --- a/hw2/hw2pr1/.hw2pr1_data.txt +++ b/hw2/hw2pr1/.hw2pr1_data.txt @@ -1 +1 @@ -((3,12,8),(2,4,6),(14,5,2)) +((3,8,(7,(3,0,7),(8,8,2))),(4,(7,9,8),8),(((3,6,4),2,6),((9,2,9),4,7,(6,4,5)),4,(6,4,5))) diff --git a/hw2/hw2pr1/hw2pr1.cpp b/hw2/hw2pr1/hw2pr1.cpp index 3c1f73a..0812ef2 100644 --- a/hw2/hw2pr1/hw2pr1.cpp +++ b/hw2/hw2pr1/hw2pr1.cpp @@ -16,6 +16,7 @@ using namespace std; // Perform min/max on this tree. void minmax(node *n) { + if(n->getLevel() % 2 == 0) { int max = n->getValue(); @@ -23,16 +24,32 @@ void minmax(node *n) { for(int c = 0; c < n->getChildren().size(); c++) { - minmax(n->getChildAt(c)); + n->getChildAt(c)->setBeta(n->getBeta()); + n->getChildAt(c)->setAlpha(n->getAlpha()); + + cout << "Alpha: " << n->getAlpha() << " Beta: " << n->getBeta() << endl; + + minmax(n->getChildAt(c)); if(n->getChildAt(c)->getValue() > max) { max = n->getChildAt(c)->getValue(); } + + if(n->getChildAt(c)->getValue() > n->getAlpha()) + { + n->setAlpha(n->getChildAt(c)->getValue()); + } + + if(n->getAlpha() >= n->getBeta()) + { + cout << "Beta pruning" << endl; + break; + } } } n->setValue(max); - cout << n->getValue() << endl; + //cout << n->getValue() << endl; } else @@ -42,16 +59,32 @@ void minmax(node *n) { for(int c = 0; c < n->getChildren().size(); c++) { - minmax(n->getChildAt(c)); + n->getChildAt(c)->setBeta(n->getBeta()); + n->getChildAt(c)->setAlpha(n->getAlpha()); + + cout << "Alpha: " << n->getAlpha() << " Beta: " << n->getBeta() << endl; + + minmax(n->getChildAt(c)); if(n->getChildAt(c)->getValue() < min) { min = n->getChildAt(c)->getValue(); } + + if(n->getChildAt(c)->getValue() < n->getBeta()) + { + n->setBeta(n->getChildAt(c)->getValue()); + } + + if(n->getBeta() <= n->getAlpha()) + { + cout << "Alpha pruning." << endl; + break; + } } } n->setValue(min); - cout << n->getValue() << endl; + //cout << n->getValue() << endl; } } @@ -73,6 +106,7 @@ void printTree(node n) tuple makeTree(string input, node n, int level) { + bool negate = false; char tempc; string tempn = ""; @@ -88,12 +122,16 @@ tuple makeTree(string input, node n, int level) input = ""; } - cout << "testing" << endl; // If the char is a number. if(isdigit(tempc)) { tempn += tempc; } + + else if(tempc == '-') + { + negate = true; + } // If the char is a comma. else if(tempc == ',') @@ -102,10 +140,16 @@ tuple makeTree(string input, node n, int level) { node tempnode; tempnode.setLevel(level); - tempnode.setValue(stoi(tempn)); + int negatemp = stoi(tempn); + if(negate) + { + negatemp = -negatemp; + } + tempnode.setValue(negatemp); tempnode.setParent(&n); n.addChild(tempnode); tempn = ""; + negate = false; } } @@ -116,10 +160,16 @@ tuple makeTree(string input, node n, int level) { node tempnode; tempnode.setLevel(level); - tempnode.setValue(stoi(tempn)); + int negatemp = stoi(tempn); + if(negate) + { + negatemp = -negatemp; + } + tempnode.setValue(negatemp); tempnode.setParent(&n); n.addChild(tempnode); tempn = ""; + negate = false; } tuple tempt(n, input); return tempt; @@ -192,6 +242,7 @@ int main(int argc, char **argv) tree = makeTree(input.substr(1), root, 1); root = get<0>(tree); + printTree(root); minmax(&root); printTree(root); diff --git a/hw2/hw2pr1/hw2pr1_data.txt b/hw2/hw2pr1/hw2pr1_data.txt index f5cb9f2..647ea82 100644 --- a/hw2/hw2pr1/hw2pr1_data.txt +++ b/hw2/hw2pr1/hw2pr1_data.txt @@ -1 +1 @@ -((3,8,(7,(3,0,7),(8,8,2))),(4,(7,9,8),8),(((3,6,4),2,6),((9,2,9),4,7,(6,4,5)),4,(6,4,5))) +(((-2,-11,10),(2,15,13),(-1,-16,-16)),((-20,13,20),(-13,7,-19),(7,3,4)),((-20,-9,-14),(-4,-6,-12),(11,18,-3))) diff --git a/hw2/hw2pr1/node.cpp b/hw2/hw2pr1/node.cpp index b43a372..de729d1 100644 --- a/hw2/hw2pr1/node.cpp +++ b/hw2/hw2pr1/node.cpp @@ -7,6 +7,8 @@ node::node() parent = NULL; level = 0; value = 0; + alpha = INT_MIN; + beta = INT_MAX; } node::node(node* n) @@ -15,5 +17,7 @@ node::node(node* n) level = n->getLevel(); value = n->getValue(); children = n->getChildren(); + alpha = n->getAlpha(); + beta = n->getBeta(); } diff --git a/hw2/hw2pr1/node.h b/hw2/hw2pr1/node.h index 9f8e8f4..48cfa65 100644 --- a/hw2/hw2pr1/node.h +++ b/hw2/hw2pr1/node.h @@ -2,6 +2,7 @@ #define NODE_H #include +#include using namespace std; @@ -12,6 +13,8 @@ class node int level; int value; vector children; + int alpha; + int beta; public: node(); node(node *n); @@ -25,6 +28,10 @@ class node void addChild(node n) { children.push_back(n); }; vector getChildren() { return children; }; node* getChildAt(int i) { return &children.at(i); }; + void setAlpha(int a) { alpha = a; }; + int getAlpha() { return alpha; }; + void setBeta(int b) { beta = b; }; + int getBeta() { return beta; }; }; #endif