Hi guys,
I am doing an assignment that requires me to use tree nodes and minimax algorithm to make a tic tac toe game. It would be player vs the computer. Each nodes should represent each possible boards that can be played. Currently, I have no idea what to do. Can anyone point me to the right direction? Anything helps, thanks.
enum Mark { X, O, N }; // N == empty Mark
enum Status {WON, LOST, DRAW, PROGRESS};
enum Player {A,B};
class Board // represents a Board's status
{
Player curr_player;
Mark m_fields[9];
public:
Board() { for (int i = 0; i <9; ++i) m_field[i] = N; }
Board( Board &o) : curr_player{o.curr_player} { for (int i = 0; i < 9; ++i) m_fields[i] = o.m_fields[i]; }
void setMark( int pos, Mark mark = N) { if (pos >= 0 && pos < 9) m_field[pos] = mark; }
Status status() {...} // checks Board's status
Player player() { return curr_player; }
Mark mark(int pos) { if (pos >= 0 && pos < 9) return m_fields[pos]; }
}
struct Node
{
Board m_board;
Node * succ[9] // each board can have max. 9 successors
}
That's a basic frame and mirrors how I would tackle this challenge.
What's next? Well I think, you could set up the matching Tree in memory and than process the minimax algorithm.
Another approach would be by invoking a function in recursive manner. This gives you a tree on computers stack :)