### 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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232`` ``````#include #include "Board.h" #include using namespace std; int findBestMoveComp(Board &,int &, int &, vector&); int findBestMovePlayer(Board &,int &, int &, vector&); 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 ranMoves; int k = 5; cout<<"Response:"<&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&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

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` `````` #ifndef __hw6__Board__ #define __hw6__Board__ #include 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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342`` `````` #include "Board.h" Board::Board() { for (int i=0; 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
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.