Connect 4 minimax game problem

Hi, I am trying to write a connect 4 game by trying to improve on tic tac toe algoritm from the book. My game however does not respond to my moves in any sensible way and I am failing to see way. My best guess is that I am missing something that is essential for minimax to work. I would really appreciate any input.

P.S. sorry for the long code

Here is my code:

There are some functions that are not being used.

main.cpp

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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <iostream>
#include "Board.h"
#include <vector>
using namespace std;

int findBestMoveComp(Board &,int &, int &, vector<int>&);
int findBestMovePlayer(Board &,int &, int &, vector<int>&);


const int DRAW = 0;
const int COMP_WIN=5;
const int COMP_LOSS=-5;


int main()
{
   
   
    int playerMove;
    int bestMove=1;
    
    
    
    
    Board b1;
    b1.printBoard();
    
    while (!b1.boardFull())
    {
        cout<<"Please Enter the player move(1-7)";
        cin>>playerMove;
        b1.addToColumn(playerMove, 1);
        b1.printBoard();
    
        if (b1.checkForWin(4, 1))
        {
            cout<<"Player Won!";
            break;
        }
        else
        {
            vector<int> ranMoves;
            int k = 5;
            
            cout<<"Response:"<<findBestMoveComp(b1, bestMove, k, ranMoves);
            cout<<endl<<"Best Move:"<<bestMove<<endl;
            b1.addToColumn(bestMove, 2);
            b1.printBoard();
            if (b1.checkForWin(4,2))
            {
                cout<<"PC Won!";
                break;
            }
        }
 
    }
    
    
    
    
    

    
    
}


int findBestMoveComp(Board &b1,int & bestMove, int &counter, vector<int>&ranMoves)
{
    
    int i, responceValue;
    int dc;
    int value;
    int player = 2;
   
    

    
    if (b1.boardFull() || counter<=0 || b1.checkForWin(4, player))
    {
        
                
                return DRAW;
        
    }
    
    else
    {
        if (b1.checkImideateWin(2, bestMove))
        {
            
             
           return COMP_WIN;
          
        }
        else
        {
            if (b1.check3(2, bestMove))
            {
                value=3;
            }
            else
            {
            
            if (b1.check3(2, bestMove))
            {
                value=3;
            }
            else
            {
                if (b1.check2(2, bestMove))
                {
                    value=2;
                }
                
                
            
        
        else
        {
            value = COMP_LOSS;
            
            
            counter--;
            for (i = 0; i < WIDTH; i++)
            {
                if (b1.addToColumn( i, player))
                {
                    
                    responceValue = findBestMovePlayer(b1, dc, counter,ranMoves);
                    b1.removeFromColumn(i, player);
                    if (responceValue>value)
                    {
                        if(responceValue==0)
                        {
                            ranMoves.push_back(i);
                        }
                        
                        value=responceValue;
                        bestMove=i;
                    }
                }
            }
        }
        }
        }
    }
    }
    return value;

}

int findBestMovePlayer(Board &b1,int & bestMove, int& counter,  vector<int>&ranMoves)
{
    
    int i, responceValue;
    int dc;
    int value;
    int player = 1;
    
    

    
    
    if (b1.boardFull() || counter<=0 || b1.checkForWin(4, player))
    {
        
        return DRAW;
    }
    
    else
    {
        if (b1.checkImideateWin(1, bestMove))
        {
        //    b1.printBoard();
            
            return COMP_LOSS;
            
        }
        else
        {
            if (b1.check3(1, bestMove))
            {
                value=-3;
            }
            else
            {
            
            if (b1.check3(1, bestMove))
            {
                value=-3;
            }
            else
            {
                if (b1.check2(1, bestMove))
                {
                    value=-2;
                }

            
           
                else
    
                {

                    value = COMP_WIN;
                    counter--;
                    for (i = 0; i < WIDTH; i++)
                    {
                if (b1.addToColumn( i, player))
                {
                    responceValue = findBestMoveComp(b1, dc, counter,ranMoves);
                    b1.removeFromColumn(i, player);
                    if (responceValue< value)
                    {
                        value=responceValue;
                        bestMove=i;
                    }
                }
            }
            }
            }
        }
        }
    }
   
    return value;
    
    
}
    
  


Board.h

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

#ifndef __hw6__Board__
#define __hw6__Board__

#include <iostream>
using namespace std;

 
const int WIDTH=7;
const int DEPTH=6;

class Board
{

    
private:
    int arrBoard[DEPTH][WIDTH];
    int depth;
    
public:
    Board();
    void printBoard();
    bool addToColumn(int,int);
    bool removeFromColumn(int,int);
    void tryBoard();
    bool checkForWin(int,int);
    bool checkVerticalWin(int,int);
    bool checkImideateWin(int,int&);
    bool checkHorizontalWin(int,int);
    int evaluateBoard(int,int&);
    bool boardFull();
    bool check2(int,int&);
    bool check3(int,int&);
    bool check4(int,int&);
   
    bool checkNextHorizontal(int,int,int);
    bool checkNextVertical(int,int,int);
    bool checkNext(int,int,int);
    
    int checkVerticalCount(int);
    int checkHorizontalCount(int);



};


Last edited on
Board.cpp

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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342

#include "Board.h"

Board::Board() 
{
    
    for (int i=0; i<DEPTH; i++)
    {
        
        for (int j=0; j<WIDTH; j++)
        {
            arrBoard[i][j]=0;
        }
    }
    
}

bool Board::addToColumn(int column, int player)
{
    
    for (int i = (DEPTH-1); i >= 0 ; i--)
    {
    
        
        if( arrBoard[i][column]==0)
        {
         arrBoard[i][column]=player;
      
            return true;
        
        }
    }
  
    return false;
        
}

bool Board::removeFromColumn(int column, int player)
{
    for (int i = 0; i < DEPTH ; i++)
    {
        if( arrBoard[i][column]==player)
        {
            arrBoard[i][column]=0;
            
            return true;
            
        }
    }
    
    return false;
}


void Board::printBoard()
{
    for (int i=0; i<DEPTH; i++)
    {
        
        for (int j=0; j<WIDTH; j++)
        {
            cout<<arrBoard[i][j];
        }
        cout<<endl;
    }
    
    cout<<"-----------------------------"<<endl;;

}


bool Board::checkForWin(int i,int player)
{
    

    
    if(checkVerticalWin(i, player) || checkHorizontalWin(i, player))
    {
        
  
        return true;
    }
    
    else
    {
  
        return false;
 
    }
    
}

bool Board::checkVerticalWin(int counterLimit, int player)
{
    
    
    for (int j = 0; j<WIDTH; j++)
    {
      int counter=0; 
    for (int i = 0; i < DEPTH; i++)
    {
        if (arrBoard[i][j]==player)
        {
            counter++;
        } else
        {
            counter=0;
        }
        
        if (counter==counterLimit)
        {
           
            return true;
        }
        
    }
    }
    return false;
}

bool Board::checkHorizontalWin(int counterLimit, int player)
{
    
    
    
    for (int i = 0; i < DEPTH; i++)
    {
        int counter=0;
        for (int j = 0; j < WIDTH; j++)
            {
                if (arrBoard[i][j]==player)
                {
                    counter++;
                }
                else
                {
                    counter=0;
                }
           
                if (counter==counterLimit)
                {
                    
                    return true;
                }

            }
        
    }

    return false;
}



bool Board::checkImideateWin(int player, int&bestMove)
{
    for (int i=0; i<WIDTH; i++)
    {
        if(addToColumn(i, player))
        {
        
        
            
        if (checkForWin(4, player))
        {
            bestMove=i;
            removeFromColumn(i, player);
            return true;
        }
            removeFromColumn(i, player);
        }
        
    }
    return false;
}




bool Board::boardFull()
{
    for (int i = 0; i < WIDTH; i++)
    {
        if (arrBoard[0][i]==0)
        {
            return false;
        }
    }
    
    
    return true;
    
}


bool Board::check2(int player, int& bestMove)
{
for (int i=0; i<WIDTH; i++)
{
    if(addToColumn(i, player))
    {
        
        
        
        if (checkNext(2, i, player))
        {
            bestMove=i;
            removeFromColumn(i, player);
            return true;
        }
        removeFromColumn(i, player);
    }
    
}
return false;
}

bool Board::check3(int player, int& bestMove)
{
    for (int i=0; i<WIDTH; i++)
    {
        if(addToColumn(i, player))
        {
            
            
            
            if (checkNext(3, i, player))
            {
                bestMove=i;
                removeFromColumn(i, player);
                return true;
            }
            removeFromColumn(i, player);
        }
        
    }
    return false;
}

bool Board::check4(int player, int& bestMove)
{
    for (int i=0; i<WIDTH; i++)
    {
        if(addToColumn(i, player))
        {
            
            
            
            if (checkNext(4, i, player))
            {
                bestMove=i;
                removeFromColumn(i, player);
                return true;
            }
            removeFromColumn(i, player);
        }
        
    }
    return false;
}


bool Board::checkNextVertical(int size, int column, int player1)
{
    int counter=0;
    int player2;
    
    if (player1==1)
    {
        player2=2;
    }
    else
        player2=1;
    
    for (int i=0 ; i<DEPTH; i++)
    {
        if (arrBoard[i][column]==player1)
        {
            counter++;
        }
        
        if (arrBoard[i][column]==player2)
        {
            return false;
        }
        if (counter==size)
        {
            return true;
        }
  }
    return false;
}

bool Board::checkNextHorizontal(int size, int column, int player1)
{
    int counter=0;
    int player2;
    
    if (player1==1)
    {
        player2=2;
    }
    else
        player2=1;
    
    for (int i=0 ; i<DEPTH; i++)
    {
        if (arrBoard[i][column]==player1)
        {
            
            for (int j = 0; j<WIDTH; j++)
            {
                if (arrBoard[i][j]==player1)
                {
                    counter++;
                }
               
                if (arrBoard[i][j]!=player1)
                {
                    counter=0;
                }
                if (counter==size)
                {
                    return true;
}}
}
    return false;
}
bool Board::checkNext(int size, int column, int player)
{
    if (checkNextHorizontal(size, column, player))
    {
        printBoard();
        return true;
    }
    if (checkNextVertical(size, column, player))
    {
        printBoard();
        return true;
    }
    return false;
}
did you try to debug this?
yeah, bean debugging for past couple of hours. The problem is that because the recursion goes down on many levels and I am still not understanding minimax fully, I don't really see where it goes wrong.

games look like this

0000000
0000000
2000000
2000000
2001110

If is switch the places and do cout<<"Response:"<<findBestMoveHuman(b1, bestMove, k, ranMoves);

on line 46, it will intercept my moves and prevent me from wining, but still won't try to win by itself
read that article like 20 times. by the looks of it I do everything correct.
Topic archived. No new replies allowed.