I wrote a program to determine the game tree for tic-tac-toe.
I believe most of the code is in good order.
I wrote a function that compares elements of a vector to determine if any elements are duplicates.
Duplicate items can either be truly identical or they can be symmetrical.
Duplicate elements are deleted from the vector.
My compare function appears to have a problem where it incorrectly eliminates elements.
Please take a look at how I iterate through the vector and see if the syntax/logic seems reasonable.
My guess is that using < > operators may be part of the problem.
The basic logic of the function is to compare the first element with the last element, then the next to last element and so on. After comparing the first element with all elements you start again comparing the second element to the other elements and so on…
void compareAllGames(move& aMove) { /* aMove is a vector of games. games are a struct of data */
vector<game>:: iterator frontIter = aMove.begin();
vector<game>:: iterator rearIter = aMove.end() - 1;
vector<game>:: iterator compIter;
for (; frontIter < rearIter; frontIter++) { /* move along the games from first to last */
for (compIter = aMove.end(); compIter > frontIter; ) { /* move along the games from last to first */
/* checkForSymmetry compares *frontIter to all symmetries of *compIter */
if (checkForSymmetry(*frontIter, *compIter)) {
compIter--;
aMove.erase(compIter + 1);
}
else {
compIter--;
}
} /* reset iterators for next loop */
compIter = aMove.end();
rearIter = aMove.end();
}
}
Thanks for all the comments. In the end I simplified my function that had the two confusing loops and determined that instead of adding all games to a move and then check for duplicates/symmetry, I could add a single game and compare it to the current list. This has two advantages: simpler coding that is easy to understand and a much lower number of comparisons. It doesn’t hurt that this code actually works too. These changes led me to find a couple of bugs in my symmetry functions.
Here is the final function that I used:
Now I just need to add my code back in that accounts for winners and I will have a proper adjusted count for the tic tac toe game tree. The unadjusted count (ignoring winners) by move is: 1, 2, 12, 38, 108, 174, 228, 174, 89, and 23.