c++ MinMax Implementation in tictactoe
Recently I started out learning on c++. I have written a program for tictactoe and Ive tried to implement the MinMax Algorithm to get the AI working. The trouble is I haven't got it right as the AI seems to be taking wrong decisions

Here are the relevant classes

I implemented my own List class to store in a list of available moves, I had to do this because I am compiling this in a very old compiler (Turbo c++ 3.0) ... please don't as why. I know there are much better and newer compilers out there :(

    class Node{
    public:
	Node();
	int value;
	Node *next;
    };

    class List{
    public:
	List();
	Node *start_node;
	void push(int i);
	int pop();
	bool empty();
    };

The Player class where the MinMax algorithm is implemented

    class Player{
    private:
        piece playerChar;
        int playerPosition;
    public:
        Player(int i);
        bool setPlayerChar(char key);
        piece getPlayerChar();
        int MinMax(Board currentBoard);
        int MaxMove(Board currentBoard, piece curChar);
        int MinMove(Board currentBoard, piece curChar);
        void genrateMoveList(piece board[9], List &moveList);
    };

The Board class to handle the board.

    class Board{
    private:
        piece board[9];
        Player player1;
        Player player2;
        int currentPlayer;
    public:
        Board();
        void drawBoard();
        piece getCharAtPosition(int pos);
        piece* getBoard();
        bool setCharAtPosition(int pos);
        int checkForWin(); //**** 0 = not finished, 1 = player 1 wins, 2 = player 2 wins, 3 = draw;
        bool setCurrentPlayerChar(char x);
        void resetBoard();
        void setCurrentPlayer(int i);
        int getCurrentPlayer();
        void autoMove();
};

Min Max Implementation in Player class


    void Player::genrateMoveList(piece board[9], List &moveList){
    int i =0;
    for(int k =0; k<9; k++){
		if(board[k] == FREESPACE){
			moveList.push(i);
		}
        i++;
    }
    }

    int Player::MinMax(Board board){
    piece *currentBoard = board.getBoard();
    List moveList;
    this->genrateMoveList(currentBoard, moveList);
    int bestVal = -INFINITY;
    int bestMoves[9] = {0};
    int index =0;
	
	while(moveList.empty()==false){
		int front = moveList.pop();
		currentBoard[front] = this->playerChar;
		int val = this->MinMove(board, this->playerChar);
		//cout << "\n index:"<< front+1 <<" value:" <<val;
		if(val > bestVal){
			bestVal = val;
			index=0;
			bestMoves[index] = front+1;
		}else  if(val == bestVal){
			bestMoves[++index] = front+1;
		}
		currentBoard[front] = FREESPACE;
	}
    if(index > 0) {
        index = rand() % index;
    }
    return bestMoves[index];
    }

    int Player::MinMove(Board board, piece curChar){
    piece *currentBoard = board.getBoard();
    int posValue=0;
    int chkWin = board.checkForWin();
    if(chkWin != 0){
        if(chkWin == 2){
			posValue = +INFINITY;
		}else if(chkWin == 1){
			posValue = -INFINITY;
		}
        return posValue;
    }
	
	List moveList;
	this->genrateMoveList(currentBoard, moveList);
	int bestVal = +INFINITY;
	while(moveList.empty()==false){
		piece newChar;
		if(curChar == X){
			newChar = O;
		}else{
			newChar = X;
		}
		int front = moveList.pop();
		currentBoard[front] = curChar;
		int val = this->MaxMove(board, newChar);
		if(val < bestVal){ 
			bestVal = val;
		}
		currentBoard[front] = FREESPACE;
	}
   
    return bestVal;
    }

    int Player::MaxMove(Board board, piece curChar){
    piece *currentBoard = board.getBoard();
    int posValue=0;
    int chkWin = board.checkForWin();
    if(chkWin != 0){
        if(chkWin == 2){
			posValue = +INFINITY;
		}else if(chkWin == 1){
			posValue = -INFINITY;
		}
        return posValue;
    }
	
	List moveList;
	this->genrateMoveList(currentBoard, moveList);
	int bestVal = -INFINITY;
	while(moveList.empty()==false){
		piece newChar;
		if(curChar == X){
			newChar = O;
		}else{
			newChar = X;
		}
		int front = moveList.pop();
		currentBoard[front] = curChar;
		int val = this->MinMove(board, newChar);
		if(val > bestVal){
			bestVal = val;
		}
		currentBoard[front] = FREESPACE;
	}
   
    return bestVal;
    }
c++ MinMax Implementation in tictactoe
===

Recently I started out learning on c++. I have written a program for tictactoe and Ive tried to implement the MinMax Algorithm to get the AI working. The trouble is I haven't got it right as the AI seems to be taking wrong decisions

Here are the relevant classes

I implemented my own List class to store in a list of available moves, I had to do this because I am compiling this in a very old compiler (Turbo c++ 3.0) ... please don't as why. I know there are much better and newer compilers out there :(

    class Node{
    public:
	Node();
	int value;
	Node *next;
    };

    class List{
    public:
	List();
	Node *start_node;
	void push(int i);
	int pop();
	bool empty();
    };

The Player class where the MinMax algorithm is implemented

    class Player{
    private:
        piece playerChar;
        int playerPosition;
    public:
        Player(int i);
        bool setPlayerChar(char key);
        piece getPlayerChar();
        int MinMax(Board currentBoard);
        int MaxMove(Board currentBoard, piece curChar);
        int MinMove(Board currentBoard, piece curChar);
        void genrateMoveList(piece board[9], List &moveList);
    };

The Board class to handle the board.

    class Board{
    private:
        piece board[9];
        Player player1;
        Player player2;
        int currentPlayer;
    public:
        Board();
        void drawBoard();
        piece getCharAtPosition(int pos);
        piece* getBoard();
        bool setCharAtPosition(int pos);
        int checkForWin(); //**** 0 = not finished, 1 = player 1 wins, 2 = player 2 wins, 3 = draw;
        bool setCurrentPlayerChar(char x);
        void resetBoard();
        void setCurrentPlayer(int i);
        int getCurrentPlayer();
        void autoMove();
};

Min Max Implementation in Player class


    void Player::genrateMoveList(piece board[9], List &moveList){
    int i =0;
    for(int k =0; k<9; k++){
		if(board[k] == FREESPACE){
			moveList.push(i);
		}
        i++;
    }
    }

    int Player::MinMax(Board board){
    piece *currentBoard = board.getBoard();
    List moveList;
    this->genrateMoveList(currentBoard, moveList);
    int bestVal = -INFINITY;
    int bestMoves[9] = {0};
    int index =0;
	
	while(moveList.empty()==false){
		int front = moveList.pop();
		currentBoard[front] = this->playerChar;
		int val = this->MinMove(board, this->playerChar);
		//cout << "\n index:"<< front+1 <<" value:" <<val;
		if(val > bestVal){
			bestVal = val;
			index=0;
			bestMoves[index] = front+1;
		}else  if(val == bestVal){
			bestMoves[++index] = front+1;
		}
		currentBoard[front] = FREESPACE;
	}
    if(index > 0) {
        index = rand() % index;
    }
    return bestMoves[index];
    }

    int Player::MinMove(Board board, piece curChar){
    piece *currentBoard = board.getBoard();
    int posValue=0;
    int chkWin = board.checkForWin();
    if(chkWin != 0){
        if(chkWin == 2){
			posValue = +INFINITY;
		}else if(chkWin == 1){
			posValue = -INFINITY;
		}
        return posValue;
    }
	
	List moveList;
	this->genrateMoveList(currentBoard, moveList);
	int bestVal = +INFINITY;
	while(moveList.empty()==false){
		piece newChar;
		if(curChar == X){
			newChar = O;
		}else{
			newChar = X;
		}
		int front = moveList.pop();
		currentBoard[front] = curChar;
		int val = this->MaxMove(board, newChar);
		if(val < bestVal){ 
			bestVal = val;
		}
		currentBoard[front] = FREESPACE;
	}
   
    return bestVal;
    }

    int Player::MaxMove(Board board, piece curChar){
    piece *currentBoard = board.getBoard();
    int posValue=0;
    int chkWin = board.checkForWin();
    if(chkWin != 0){
        if(chkWin == 2){
			posValue = +INFINITY;
		}else if(chkWin == 1){
			posValue = -INFINITY;
		}
        return posValue;
    }
	
	List moveList;
	this->genrateMoveList(currentBoard, moveList);
	int bestVal = -INFINITY;
	while(moveList.empty()==false){
		piece newChar;
		if(curChar == X){
			newChar = O;
		}else{
			newChar = X;
		}
		int front = moveList.pop();
		currentBoard[front] = curChar;
		int val = this->MinMove(board, newChar);
		if(val > bestVal){
			bestVal = val;
		}
		currentBoard[front] = FREESPACE;
	}
   
    return bestVal;
    }