TicTacToe with Minimax Algorithm

Hey Guys !! I was just working on the Tic Tac Toe Problem and was trying to use the Minimax Algorithm. Though i admit that my code doesn't exactly use templates and other stuff but its simple and easy to understand. Although i don't understand the problem right now. The code isn't working properly. Can anyone suggest me any improvements ?

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include<iostream>
#include<ctime>
using namespace std;

class MinMax
{
public:
    char arr[9];
    char dup[9];
    int VALUE;
    int TURN;
    int DEPTH;
    MinMax()
    {
        int i;
        for(i=1;i<=9;i++)
        {
            arr[i-1]=' ';
            dup[i-1]=' ';
        }
    }
    void MaxMove();
    void MinMove();
    void display();
    char evaluate();
    void randomtoss();
    char staticevaluate(char *);
    void process();
};
void MinMax::randomtoss()  // Toss to decide which will move first
{
    int a,num;
    srand(time(NULL));
    cout << "It's the toss ! Enter 1 or 2" << endl;
    cin >> num;
    a=(rand()%2)+1;
    if(num=a)
    {
        cout << "You won the Toss ! Make your move" << endl;
        TURN=1;
    }
    else
    {
        cout << "You lose the toss ! I will move first :P" << endl;
        TURN=2;
    }
}
char MinMax::staticevaluate(char *a)  // The static evaluate function
{
    int i,VAL;
    for(i=0;i<9;i++)
    {
        if(*(a+i)=='X')
        {
            if(i==4)
            {
                VAL+=10;
            }
            if((i==0)||(i==2)||(i==6)||(i==8))
            {
                VAL+=8;
            }
            if((i==1)||(i==3)||(i==5)||(i==7))
            {
                VAL+=6;
            }
        }
        if(*(a+i)=='O')
        {
            if(i==4)
            {
                VAL-=10;
            }
            if((i==0)||(i==2)||(i==6)||(i==8))
            {
                VAL-=8;
            }
            if((i==1)||(i==3)||(i==5)||(i==7))
            {
                VAL-=6;
            }
        }
    }
    return VAL;
}
void MinMax::MaxMove()  // Player will make his move via this function
{
    int x;
    cout << " Enter your move " << endl << endl;
    cin >> x;
    x--;
    if(arr[x]!=' ')
    {
        cout << "Invalid move, Try again" << endl;
        MaxMove();
    }
    else 
       arr[x]='X';
    TURN=2;
}
void MinMax::MinMove()  // The PC will make his move via this function
{
    int i,POS;
    for(i=0;i<9;i++)
    {
        if(arr[i]==' ')
        {
            dup[i]='O';
            if(staticevaluate(dup)<VALUE)
            {
                VALUE=staticevaluate(dup);
                POS=i;
            }
            dup[i]=' ';
        }
    }
    arr[POS]='O';
    TURN=1;
}    
void MinMax::display()  // Displays the matrix
{
    int t;
    cout << endl;
    for(t=0;t<9;t++)
    {
        cout << arr[t] <<"  |" << arr[t+1] << "  |" << arr[t+2];
        t+=2;
        if(t!=8)
            cout << endl << "---|---|---" << endl;
    }
    cout << endl;
}
char MinMax::evaluate()  // Checks for a win,loss or draw
{
    int i,c=0;
    for(i=0; i<7; i++)   /* Check Rows */
    {
        if(arr[i]==arr[i+1] && arr[i+1]==arr[i+2])
            return arr[i];
        i+=2;
    }
    for(i=0; i<3; i++)  /* Check Columns */
    {
        if(arr[i]==arr[i+3] && arr[i+3]==arr[i+6]) 
            return arr[i];
    }
    if(arr[0]==arr[4] && arr[4]==arr[8])  /* Test Diagonals */
         return arr[0];

    if(arr[2]==arr[4] && arr[4]==arr[6])  /* Test Diagonals */
         return arr[2];
    
    for(i=0;i<9;i++)
    {
         if(arr[i]!=' ')
             c++;
    }
    if(c==9)
        return 'D';
    return ' ';
}
void MinMax::process()
{
    char ch;
    randomtoss();
    do
    {
        if(TURN==1)
        {
            MaxMove();
            TURN=2;
        }   
        else
        {
            MinMove();
            TURN=1;
        }    
        display();
        cout << endl;
        ch=evaluate();
        if(ch=='X')
        {
            display();
            cout << endl;
            cout <<"YOU WON !!"<< endl;
            exit(0);
        }
        if(ch=='O')
        {
            display();
            cout << endl;
            cout <<"I WON !!"<< endl;
            exit(0);
        }
        if(ch=='D')
        {
            display();
            cout << endl;
            cout <<"IT'S A DRAW !!"<< endl;
            exit(0);
        }
    }
    while(ch==' ');
}
int main()
{
    MinMax play;
    play.process();
    return 0;
}

Sorry, but analyzing 210 lines without a description of the problem is not something that many people will do.

What exactly is the behavioral problem that you see? Or is it perhaps a compilation error? Can you narrow it down to a few lines of code?
Okay let me explain the code a little bit. The function void MaxMove() and void MinMove() are responsible for making the moves of human and the computer respectively. The function char evaluate() is responsible for deciding the win, loss or draw during the match. The function void randomtoss() is responsible for deciding who will move first ; The player or the computer. The function char staticevaluate(char *) is the heuristic i have used but i think this function has some fault.....I'm not exactly sure.....and finally the function void process() makes the problem run.
I have used the variable arr[9] to store the single dimensional array. The variable VALUE is the heuristic value of the program.
I think the main problem is in the heuristic function and the MinMove() function. Can you please suggest anything now ? Thanks for your previous reply. :)
Should VALUE be initialised to something for this comparison first time through MinMove()?
if(staticevaluate(dup)<VALUE)

Should VAL be initialised to something in staticevaluate(char *)?
Topic archived. No new replies allowed.