Problems with Connect Four.

Hi to everyone. I'm writing a connect four game by alpha-beta pruning (modified minimax) algorithm. But it does not work correctly. Throught 3-4 steps AI gets a chaotic result - prints many many turns in one and I don't understand why the checking of column fullnes doesn't work. It overflowes by three steps. Sorry for the long code. I have no idea why it's not working. And thank you.

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
    while(!gamewon)
    {
        if(hold2 != -1)
        {
            if(player == 15)        //PLAYER
            {
                std::cout<<"player  drop where?";
                player = 254;
            }
            else                    //COMPUTER
            {
                player = 15;
            }
        }
        while(true)
        {
            if(charsPlaced == 42) break;//if draw
                if (player == 15)
                {
                    moves.clear();
                    hold=0;
                    value=0;
                    double prevValue=-MAX_RECURDEPTH-1;

                    for(int x=0; x<WIDTH; x++)
                    {
                        if(colH[x]>=HEIGHT)
                            continue;
                        if (move==0)
                        {
                            moves.push_back(3);
                            break;
                        }
                        if(player==15 && move==1)
                        {
                            moves.push_back(3);
                            break;
                        }

                        value=AB_minmax(player, recurDepth,x, a,b); 

                        if(value>prevValue)
                        {
                            moves.clear();
                            prevValue=value;
                        }
                        if(value==prevValue)
                        {
                            moves.push_back(x);
                        }
                    }
                    move++;
                    std::cout<<"VAlue:";
                    std::cout<<(prevValue++);
                    if(moves.size()>0)
                    {
                        std::random_shuffle( moves.begin(), moves.end() );
                        hold=moves.at(0);
                    }
                    if(moves.size()==0)
                    {
                        cout<<"Its a draw.";
                        break;
                    }
                }
                else
                std::cin>>hold;//get user input

                hold--;//take off 1 to account for arrays starting at 0 not 1

                if(hold <=6 && hold>= 0) break;//if within valid range stop loop
                else
                std::cout<< "\nplease enter a value between 1 and 7 :";//ask for input and loop again
                if (cin.fail()) //catch a non number
                {                       //
                    cin.clear();        //Stops cin trying to put its value in to hold
                    char c;         //Try entering a non number without this, 2 see what this does
                    cin>>c;           //
                }                       //Catch a non number
        }
        if(charsPlaced == 42) break;//if draw

        hold2 = drop(hold,player);

        if(hold2 == -1)
        std::cout<<"Column is full\nPlease enter another number between 1 and 7:";//if error -1 row is full
        else
        {
            gamewon = won(place,hold2,hold);
            charsPlaced ++;
            system("cls");
            display();
        }
    }
    system("cls");
    if(charsPlaced == 42)
        {
            std::cout<<"No winner, Game was draw\n";
            system("pause");
            return 0;
        }
    if(player == 15)//if won by player 2
        std::cout<<"gamewon by : computer\n";
    else
        std::cout<<"gamewon by : player\n";//Else won by player 1
    system("pause");//pauses before exit so players can see who won, works with windows

    return 0;//Exit application
}

void display()
{
    cout<<" 0   1   2   3   4   5   6\n";
    for(int i=0; i<= 5; i++)
    {
        for(int j=0; j<=6; j++) cout<<char(218)<<char(196)<<char(191)<<" ";
        cout<<'\n';
        for(int j=0; j<= 6; j++) cout<<char(179)<<place[i][j]<<char(179)<<" ";
        cout<<'\n';
        for(int j=0; j<= 6; j++) cout<<char(192)<<char(196)<<char(217)<<" ";
        cout<<'\n';
    }
}

int drop(int b, char player) {
    if(b >=0 && b<= 6)
    {
        if(place[0][b] == ' ')
        {
            int i;
            for(i = 0;place[i][b] == ' ';i++)
                if(i == 5)
                {
                    place[i][b] = player;
                    return i;
                }
                i--;
                place[i][b] =player;
                return i;
        }
        else
        {
            return -1;
        }
    }
    else
    {
        return -1;
    }
}

double AB_minmax(int player, int hold,int recurDepth,double a,double b)
{
    int pp;
    double value=0,val=0;
    if(colH[hold]>=HEIGHT)
        return 0;
    recurDepth++;
    place[hold][colH[hold]]=player;
    colH[hold]++;
    if(colH[hold]>=HEIGHT)
        return 0;
    if(won(place,hold,colH[hold])>0)
    {
        if(player==15)
            {value=MAX_RECURDEPTH+1-recurDepth;}
        else {value=-MAX_RECURDEPTH-1+recurDepth;}
    }
    if(player==15)
    {
        for (int i=0, r=recurDepth; i < WIDTH && r <= MAX_RECURDEPTH; i++,r++)
        {
            if (player==15) (pp=254);
            else (pp=15);

            val=AB_minmax(pp,i,(r+1),a,b);
            if(val<b)
            {
                b=val;
                break;
            }
        }
        return b;
 
        int r=recurDepth;
        while((r<=MAX_RECURDEPTH) && (i<WIDTH)
        {
            val=AB_minmax(3-player,i,(r+1),a,b);
            if(val<b)
            {
                b=val;
                break;
            }
            i++;
            r++;
        }
        return b;*/
    }
    if(player==254)
    {
        for(int x=0; (x<WIDTH && recurDepth<=MAX_RECURDEPTH);hold++,recurDepth++)
        {
            if (player==15) (pp=254);
            else (pp=15);

            val=AB_minmax(3-player,hold,(recurDepth+1),a,b);
            if(val>a)
            {
                a=val;
                break;
            }

        }
        return a;
    }
    value=val;

    recurDepth--;
    colH[hold]--;
    place[hold][colH[hold]]=' ';
    return value;
}
Last edited on
Please edit your code to be in code tags

Topic archived. No new replies allowed.