3

I'm creating text based TicTacToe in C++ and need to create a function that checks for a win.

Right now, I have a vector of all the moves player X has made:

std::vector<int> x_vector = {1, 2, 3, 5, 7};

I also have a 2d vector of win conditions:

std::vector<std::vector> > wins = {{1, 2, 3}, {4, 5, 6}, {7, 8, 8}};

In this case, each element of the wins vector represents a win condition. If player X ever has a combination of inputs in their vector that includes one of the win conditions, I'm trying to get a bool function to return true.

I'm new to C++ and coding in general, so all patience is appreciated, and the simpler the solution you can help me find the better.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Max Waters
  • 33
  • 2
  • Give me two pieces of paper: one with player X's moves, and one with the set of valid win triples. What is the step-by-step algorithm I should do to give you what you want? Don't write a C++ function. Write a plain english algorithm. – JohnFilleau Dec 28 '21 at 02:26
  • 1. check the first element of the first win triple against the first element of player X's moves. 2. If there is a match, this code loops again through each element of the first win triple until... 3. There should be a break from the loop if there is no match. 4. If the loop breaks, then loop from step 2 should run again, but this time on the second win triple, and then the third and so on. – Max Waters Dec 28 '21 at 02:42
  • Code that up and see if it works. – JohnFilleau Dec 28 '21 at 02:44
  • The block i'm running in to is the syntax to access only one win triple at a time. – Max Waters Dec 28 '21 at 02:46
  • You may be better off using `std::set` instead of `std::vector` and checking if any of the known wins is a subset of the user's input. [How do I check if one vector is a subset of another?](https://stackoverflow.com/q/4068141/10871073) – Adrian Mole Dec 28 '21 at 02:55
  • Great, thanks! I'll check it out. – Max Waters Dec 28 '21 at 02:59

1 Answers1

3

You can iterate through your list of known wins, checking each to see if it is a subset of the list of user's moves. The std::includes function will do this test – but note that the two 'lists' need to be sorted.

To avoid having to manually sort the list of user's moves after each input, you can use the std::set container (which is inherently sorted), instead of std::vector.

The following snippet shows a relatively simple implementation of an isWin() function using this approach, along with some rudimentary test cases:

#include <iostream>
#include <vector>
#include <set>
#include <algorithm> // For std::includes

bool isWin(const std::set<int>& test)
{
    static std::vector<std::set<int>> winlist = { 
        {1, 2, 3}, {4, 5, 6}, {7, 8, 9},    // Horizontal lines
        {1, 4, 7}, {2, 5, 8}, {3, 6, 9},    // Vertical lines
        {1, 5, 9}, {3, 5, 7},               // Diagonal lines
    };

    for (auto win : winlist) {
        if (std::includes(test.begin(), test.end(), win.begin(), win.end()))  {
            return true; // Match - Win!
        }
    }
    return false; // Didn't get a match - no win
}

int main()
{
    std::set<int> s1{ 1, 2 }; // Trivial "No win" (only 2 moves)
    std::cout << "s1: " << isWin(s1) << "\n";

    std::set<int> s2{ 1, 2, 3 }; // Trivial "Win" (top row)
    std::cout << "s2: " << isWin(s2) << "\n";

    std::set<int> s3{ 2, 4, 1, 5 }; // " No Win"
    std::cout << "s3: " << isWin(s3) << "\n";

    std::set<int> s4{ 5, 2, 4, 6 }; // "Win" (middle row)
    std::cout << "s4: " << isWin(s4) << "\n";

    std::set<int> s5{ 5, 1, 3, 6, 9 }; // "Win" (diagonal)
    std::cout << "s5: " << isWin(s5) << "\n";

    return 0;
}

Note that this approach may not be the best for checking wins in a Tic-Tac-Toe game; however, if your purpose is to learn about vectors, sets and looking for matching sub-sequences, it may provide a useful starting-point.


For your actual user input, you would declare and initialize an empty set and then add moves using the insert member function of the std::set container class; something like this:

int main()
{
    std::set<int> user{}; // Empty Set
    user.insert(5);
    user.insert(2);
    user.insert(4);
    user.insert(6);
    std::cout << "user (1): " << isWin(user) << "\n";
    user.clear();
    user.insert(2);
    user.insert(4);
    user.insert(1);
    user.insert(5);
    std::cout << "user (2): " << isWin(user) << "\n";
    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83