2

I'm trying to debug an issue within this c++ code that plays tic-tac-toe on a given game state using minimax to find best moves between the min (o) and maxer (x). I'm able to get the position of the move from the minimax function but when I try to run make_move to set the position on the board, i run into this issue: "The object has type qualifiers that are not compatible with the member function State::make_move object type is: const State"

void solve(const State &st)
{
    while (!st.full() && !st.gameEnded())
    {
        auto value = minimax(st); // Find the optimal move for the current player
        st.make_move(get<1>(value), get<2>(value));
        if (st.max_value() == 1 || st.max_value() == -1) // Either x or o won
        {
            st.set_game_ended(true);
        }
    }
    st.print(); // Prints the completed board state
    if (st.max_value() == 0) // Not +=1 and the board is full, game ends in a draw
    {
        cout << "draw" << endl;
    }
}
public:
    void make_move(int x, int y)
    {
        sq[x][y] = to_move; // Represents the position on the board
        // Switch turns depending on the current player
        to_move = (to_move == MAX ? MIN : MAX);
        // Increment the number of filled tiles on the board
        filled += 1;
    }

This issue also happens similarly with the set_game_ended method. If there are any ideas on how to access these methods through a const reference to the state, it would be greatly appreciated.

Proxied
  • 31
  • 1
  • 3
  • 4
    If the methods modify the object, you can't. Why do you use `const` references for? – Quimby Nov 21 '21 at 10:19
  • 1
    The `const` is a contract that you sign to promise not to change the object you're taking in. If you use a setter method that's violating your contract, that's why the compiler complained. If you need to change it, you have to either remove that `const` qualifier or create a copy of that object and work on that copy instead. – stanle Nov 21 '21 at 10:26
  • Hi Quimby, this was part of a question where we are given the function signature and have to implement the rest, although you could drop the const reference it wouldn't be ideal in this case. – Proxied Nov 21 '21 at 10:27
  • Thanks for the explanations, working off a copy of the object in this case would be a good idea and then returning the outcome from it. Appreciate the help! – Proxied Nov 21 '21 at 10:37

0 Answers0