Tic Tac Toe Using Tree-Based AI (Minimax)

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;


struct GameNode
{
	char board[3][3] = { {'0', '1' ,'2' }, {'3', '4', '5'}, {'6', '7', '8'} };

	vector<GameNode *> succ;

	GameNode(char bo[3][3])
	{
		for (int i = 0; i < 3; i++)
		{
			for (int k = 0; k < 3; k++)
			{
				board[i][k] = bo[i][k];
			}
		}
	}
};

int main()
{

	cout << "Welcome to Tic-Tac-Toe!\n-----------------------" << endl;


    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 :)
Last edited on
Topic archived. No new replies allowed.