Submission.
This commit is contained in:
parent
73e5f291c0
commit
24dcec83fe
3 changed files with 47 additions and 44 deletions
BIN
hw2/hw2.zip
Normal file
BIN
hw2/hw2.zip
Normal file
Binary file not shown.
|
@ -214,10 +214,11 @@ int main(int argc, char **argv)
|
||||||
tree = makeTree(input.substr(1), root, 1);
|
tree = makeTree(input.substr(1), root, 1);
|
||||||
root = get<0>(tree);
|
root = get<0>(tree);
|
||||||
|
|
||||||
printTree(root);
|
//printTree(root);
|
||||||
minmax(&root);
|
minmax(&root);
|
||||||
|
|
||||||
printTree(root);
|
//printTree(root);
|
||||||
|
cout << root.getValue() << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,36 +20,37 @@ void minmax(node *n)
|
||||||
if(n->getLevel() % 2 == 0)
|
if(n->getLevel() % 2 == 0)
|
||||||
{
|
{
|
||||||
int max = n->getValue();
|
int max = n->getValue();
|
||||||
|
|
||||||
if(n->getChildren().size() > 0)
|
if(n->getChildren().size() > 0)
|
||||||
{
|
{
|
||||||
for(int c = 0; c < n->getChildren().size(); c++)
|
for(int c = 0; c < n->getChildren().size(); c++)
|
||||||
{
|
{
|
||||||
n->getChildAt(c)->setBeta(n->getBeta());
|
n->getChildAt(c)->setBeta(n->getBeta());
|
||||||
n->getChildAt(c)->setAlpha(n->getAlpha());
|
n->getChildAt(c)->setAlpha(n->getAlpha());
|
||||||
|
|
||||||
cout << "Alpha: " << n->getAlpha() << " Beta: " << n->getBeta() << endl;
|
//cout << "Alpha: " << n->getAlpha() << " Beta: " << n->getBeta() << endl;
|
||||||
|
|
||||||
minmax(n->getChildAt(c));
|
minmax(n->getChildAt(c));
|
||||||
|
|
||||||
if(n->getChildAt(c)->getValue() > max)
|
if(n->getChildAt(c)->getValue() > max)
|
||||||
{
|
{
|
||||||
max = n->getChildAt(c)->getValue();
|
max = n->getChildAt(c)->getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(n->getChildAt(c)->getValue() > n->getAlpha())
|
if(n->getChildAt(c)->getValue() > n->getAlpha())
|
||||||
{
|
{
|
||||||
n->setAlpha(n->getChildAt(c)->getValue());
|
n->setAlpha(n->getChildAt(c)->getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(n->getAlpha() >= n->getBeta())
|
if(n->getAlpha() >= n->getBeta())
|
||||||
{
|
{
|
||||||
cout << "Beta pruning" << endl;
|
cout << "Beta pruning" << endl;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
n->setValue(max);
|
n->setValue(max);
|
||||||
//cout << n->getValue() << endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
|
@ -59,32 +60,32 @@ void minmax(node *n)
|
||||||
{
|
{
|
||||||
for(int c = 0; c < n->getChildren().size(); c++)
|
for(int c = 0; c < n->getChildren().size(); c++)
|
||||||
{
|
{
|
||||||
n->getChildAt(c)->setBeta(n->getBeta());
|
n->getChildAt(c)->setBeta(n->getBeta());
|
||||||
n->getChildAt(c)->setAlpha(n->getAlpha());
|
n->getChildAt(c)->setAlpha(n->getAlpha());
|
||||||
|
|
||||||
cout << "Alpha: " << n->getAlpha() << " Beta: " << n->getBeta() << endl;
|
//cout << "Alpha: " << n->getAlpha() << " Beta: " << n->getBeta() << endl;
|
||||||
|
|
||||||
minmax(n->getChildAt(c));
|
minmax(n->getChildAt(c));
|
||||||
|
|
||||||
if(n->getChildAt(c)->getValue() < min)
|
if(n->getChildAt(c)->getValue() < min)
|
||||||
{
|
{
|
||||||
min = n->getChildAt(c)->getValue();
|
min = n->getChildAt(c)->getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(n->getChildAt(c)->getValue() < n->getBeta())
|
if(n->getChildAt(c)->getValue() < n->getBeta())
|
||||||
{
|
{
|
||||||
n->setBeta(n->getChildAt(c)->getValue());
|
n->setBeta(n->getChildAt(c)->getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(n->getBeta() <= n->getAlpha())
|
if(n->getBeta() <= n->getAlpha())
|
||||||
{
|
{
|
||||||
cout << "Alpha pruning." << endl;
|
cout << "Alpha pruning." << endl;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
n->setValue(min);
|
n->setValue(min);
|
||||||
//cout << n->getValue() << endl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,16 +141,16 @@ tuple<node, string> makeTree(string input, node n, int level)
|
||||||
{
|
{
|
||||||
node tempnode;
|
node tempnode;
|
||||||
tempnode.setLevel(level);
|
tempnode.setLevel(level);
|
||||||
int negatemp = stoi(tempn);
|
int negatemp = stoi(tempn);
|
||||||
if(negate)
|
if(negate)
|
||||||
{
|
{
|
||||||
negatemp = -negatemp;
|
negatemp = -negatemp;
|
||||||
}
|
}
|
||||||
tempnode.setValue(negatemp);
|
tempnode.setValue(negatemp);
|
||||||
tempnode.setParent(&n);
|
tempnode.setParent(&n);
|
||||||
n.addChild(tempnode);
|
n.addChild(tempnode);
|
||||||
tempn = "";
|
tempn = "";
|
||||||
negate = false;
|
negate = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,16 +161,16 @@ tuple<node, string> makeTree(string input, node n, int level)
|
||||||
{
|
{
|
||||||
node tempnode;
|
node tempnode;
|
||||||
tempnode.setLevel(level);
|
tempnode.setLevel(level);
|
||||||
int negatemp = stoi(tempn);
|
int negatemp = stoi(tempn);
|
||||||
if(negate)
|
if(negate)
|
||||||
{
|
{
|
||||||
negatemp = -negatemp;
|
negatemp = -negatemp;
|
||||||
}
|
}
|
||||||
tempnode.setValue(negatemp);
|
tempnode.setValue(negatemp);
|
||||||
tempnode.setParent(&n);
|
tempnode.setParent(&n);
|
||||||
n.addChild(tempnode);
|
n.addChild(tempnode);
|
||||||
tempn = "";
|
tempn = "";
|
||||||
negate = false;
|
negate = false;
|
||||||
}
|
}
|
||||||
tuple<node, string> tempt(n, input);
|
tuple<node, string> tempt(n, input);
|
||||||
return tempt;
|
return tempt;
|
||||||
|
@ -242,10 +243,11 @@ int main(int argc, char **argv)
|
||||||
tree = makeTree(input.substr(1), root, 1);
|
tree = makeTree(input.substr(1), root, 1);
|
||||||
root = get<0>(tree);
|
root = get<0>(tree);
|
||||||
|
|
||||||
printTree(root);
|
//printTree(root);
|
||||||
minmax(&root);
|
minmax(&root);
|
||||||
|
|
||||||
printTree(root);
|
//printTree(root);
|
||||||
|
cout << root.getValue() << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue