I can not find the error / and. Flies out on the first turn. When you click on the cell, O is set and the function is called:

xo[tree.move(tictac,first_play)]->setText("Х");

move () should return the cell number where to put the cross. It builds a tree that, using the minimax algorithm, selects the best option.

I hope there are noble people who rummage in my terrible code: D

 struct Node { Tictac tictac; Node *nodes[9]; int min; }; class Tree { public: Tree(); Tree(Tictac x); int move(Tictac x, bool y); Node* addNode(Tictac x,int i,bool xo); void del(); //обнулить private: Node node; bool xo; //true - x, false - o - за кого играем }; class Tictac { public: Tictac(); void restart(); //обнуление void add(int i, int xo); //добавить -1 или 1 в клетку xo int checkwin(bool xo); //true - играем за Х, return 5, если выиграл, -5 - проигрыш, в любом другом случае - 0 int empty(); //сколько пустых клеток void print(); //вывод arr int arr[9]; // -1 = X; 1 = O }; 

And the functions themselves:

 int Tree::move(Tictac x, bool y) { xo = y; node.tictac = x; int move, max = -5; for (int i = 0; i < 9; i++) { if (node.tictac.arr[i] == 0) { node.nodes[i]=addNode(node.tictac,i,y); if (node.nodes[i]->min >= max) { max = node.nodes[i]->min; move = i; } qDebug() << endl << "Max: " << max << " Move: " << move << endl; } else node.nodes[i] = 0; } return move; } Node* Tree::addNode(Tictac x, int i, bool play) { if (play) x.add(-1,i); else x.add(1,i); Node temp; temp.min = x.checkwin(xo); //x.print(); int min = 5, max = -5; if ((temp.min == 0) && (x.empty() != 0)) { for (int i = 0; i < 9; i++) { if (x.arr[i] == 0) temp.nodes[i] = addNode(x, i, !play); else temp.nodes[i] = 0; } for (int i = 0; i < 9; i++) { if (temp.nodes[i] != 0) { if (xo != play) if (temp.nodes[i]->min > max) max=temp.nodes[i]->min; else if (temp.nodes[i]->min < min) min=temp.nodes[i]->min; } } if (xo != play) temp.min = max; else temp.min = min; } return &temp; } void Tictac::add(int i, int xo) { arr[xo] = i; } int Tictac::checkwin(bool xo) { if ((((arr[0] == arr[1]) && (arr[1] == arr[2])) || ((arr[0] == arr[3]) && (arr[3] == arr[6])) || ((arr[0] == arr[4]) && (arr[4] == arr[8]))) && (arr[0] != 0)) { if ((xo) && (arr[0] == -1)) return 5; else if ((xo) && (arr[0] == 1)) return -5; else if ((!xo) && (arr[0] == 1)) return 5; else return -5; } else if ((((arr[3] == arr[4]) && (arr[4] == arr[5])) || ((arr[1] == arr[4]) && (arr[4] == arr[7])) || ((arr[2] == arr[4]) && (arr[4] == arr[6]))) && (arr[4 != 0])){ if ((xo) && (arr[4] == -1)) return 5; else if ((xo) && (arr[4] == 1)) return -5; else if ((!xo) && (arr[4] == 1)) return 5; else return -5; } else if ((((arr[6] == arr[7]) && (arr[7] == arr[8])) || ((arr[2] == arr[5]) && (arr[5] == arr[8]))) && (arr[8] != 0)){ if ((xo) && (arr[8] == -1)) return 5; else if ((xo) && (arr[8] == 1)) return -5; else if ((!xo) && (arr[8] == 1)) return 5; else return -5; } else return 0; } 

    1 answer 1

    At a minimum, the Node* Tree::addNode(Tictac x, int i, bool play) returns the address of a local variable that does not exist after exiting the method. Further, this may lead to unpredictable behavior. In general, the compiler should have told you about this in the form of a warning (Warning).

    Because the given code is not complete ( Tictac::empty methods are Tictac::empty , the Tictac constructor is Tictac ) did not try to run, perhaps there are some other problems.