<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'minimax algorithm not working properly' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'minimax algorithm not working properly' posted on the 'C++ Game Development' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2010 Programmers Heaven</copyright>
    <pubDate>Thu, 18 Mar 2010 00:42:48 -0700</pubDate>
    <lastBuildDate>Thu, 18 Mar 2010 00:42:48 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>minimax algorithm not working properly</title>
      <link>http://www.programmersheaven.com/mb/cplusgameprogramming/368950/368950/minimax-algorithm-not-working-properly/</link>
      <description>hello i got frustrated after making several tries to make the minimax algorithm work in my console tic tac toe. the problem is the algorithm&lt;br /&gt;
doesn't play the game as it should.The thing is it just tries to play the move that it 'sees' to be the best for itself without caring that its opponet&lt;br /&gt;
is building an easy three to win the game. &lt;br /&gt;
For ex. it playing as second player as&lt;br /&gt;
&lt;br /&gt;
ist player row=0;col=0;&lt;br /&gt;
minimax player row=2;col=1;&lt;br /&gt;
ist player row=0;col=1;&lt;br /&gt;
minimax row=2;col=0;(ignoring the fact the player will win in next move)&lt;br /&gt;
ist player row=0;col=2 and wins &lt;br /&gt;
&lt;br /&gt;
so please help me finding the bug in this code.thank you.&lt;br /&gt;
&lt;pre class="sourcecode"&gt;

//board.h file

#ifndef BOARD_H
#define BOARD_H

#include&amp;lt;iostream&amp;gt;
#include&amp;lt;cstdlib&amp;gt;
using namespace std;



enum PosValue {O_WIN=-1,TIE,X_WIN,UNDEFINED};
enum Piece {NAUGHT=-1,EMPTY,CROSS};
enum Player {HUMAN=1,COMP};



class board
{
public:
	
	board();
	~board(){}
	void initBoard(Player pl);
	bool isAWin(Piece p);
	bool isBoardFull();
	bool isOver();
	int evaluate();
	bool makeMove(int r,int c,Piece p=EMPTY);
	void undoMove(int r,int c);
	void drawBoard();
	Piece turnToPiece();
	void alterTurn();
	Player firstPlayer;
	void playComp();
	void playHuman();

	int chooseMove(Piece p,int &amp;amp;row,int &amp;amp;col);

	 
private:

	Piece hold[3][3];

	void placeMove(int r,int c,Piece p=EMPTY);
	char pieceToChar(Piece p);
	int turn;
	

};




#endif


#include "board.h"

board::board()
{

}

void board::initBoard(Player pl) //initialize the board, gets who is first playing
{
	firstPlayer=pl;
	for(int i=0;i&amp;lt;3;i++)
		for(int j=0;j&amp;lt;3;j++)
			hold[i][j]=EMPTY;

	turn =1;
}



bool board::isAWin(Piece p) //checks whether a Piece wins
{
	int row, column;

    //  check all rows
    for( row = 0; row &amp;lt; 3; row++ )
    {
        for( column = 0; column &amp;lt; 3; column++ )
            if( hold[ row ][ column ] != p )
                break;
        if( column &amp;gt;= 3 )
            return true;
    }

      // check all column
    for( column = 0; column &amp;lt; 3; column++ )
    {
        for( row = 0; row &amp;lt; 3; row++ )
            if( hold[ row ][ column ] != p)
                break;
        if( row &amp;gt;= 3 )
            return true;
    }

      // check diagonals
    if( hold[ 1 ][ 1 ] == p &amp;amp;&amp;amp; hold[ 2 ][ 2 ] == p
                        &amp;amp;&amp;amp; hold[ 0 ][ 0 ] == p )
        return true;

    if( hold[ 0 ][ 2 ] == p &amp;amp;&amp;amp; hold[ 1 ][ 1 ] == p
			&amp;amp;&amp;amp; hold[ 2 ][ 0 ] == p )
        return true;

    return false;

	
}

bool board::isBoardFull()
{
	for(int row=0;row&amp;lt;3;row++)
	{
		for(int col=0;col&amp;lt;3;col++)
		{
			if (hold[row][col]==EMPTY)
				return false;
		}
	}
	return true;
}

int board::evaluate()  //heuristic function used for minimax algo
{
	if (isAWin(CROSS))
		return X_WIN;
	else if (isAWin(NAUGHT))
		return O_WIN;
	else if (isBoardFull())
		return TIE;
	else
		return UNDEFINED;
}

bool board::makeMove(int r, int c, Piece p)  //makes a move on board, checking available
{
	if((r&amp;lt;0) || (r&amp;gt;2) || (c&amp;lt;0) || (c&amp;gt;2) || hold[r][c]!=EMPTY)
		return false;
	
	placeMove(r, c, p);

	return true;
}

void board::placeMove(int r, int c, Piece p )//makes a move on board,no checking available
{												//(used by minimax)
	hold[r][c]=p;
}

void board::undoMove(int r, int c)           //undo a move(used by minimax)
{
	hold[r][c]=EMPTY;
}

bool board::isOver()                       //if game is over
{
	if(evaluate()==UNDEFINED)
		return false;
	else
		return true;
}

char board::pieceToChar(Piece p)			//converts a piece to char( used by drawBoard)
{
	if(p==EMPTY)
		return ' ';
	else if(p==CROSS)
		return 'X';
	else
		return 'O';
}

void board::drawBoard()
{
	char print_board[3][3];
   
    for (int row=0; row&amp;lt;3; row++)
		for(int col=0;col&amp;lt;3;col++)
			print_board[row][col] = pieceToChar(hold[row][col]);
		
    
    cout &amp;lt;&amp;lt; print_board[0][0] &amp;lt;&amp;lt; '|' &amp;lt;&amp;lt; print_board[0][1] &amp;lt;&amp;lt; '|' &amp;lt;&amp;lt; print_board[0][2] &amp;lt;&amp;lt; endl;
    cout &amp;lt;&amp;lt; print_board[1][0] &amp;lt;&amp;lt; '|' &amp;lt;&amp;lt; print_board[1][1] &amp;lt;&amp;lt; '|' &amp;lt;&amp;lt; print_board[1][2] &amp;lt;&amp;lt; endl;
    cout &amp;lt;&amp;lt; print_board[2][0] &amp;lt;&amp;lt; '|' &amp;lt;&amp;lt; print_board[2][1] &amp;lt;&amp;lt; '|' &amp;lt;&amp;lt; print_board[2][2] &amp;lt;&amp;lt; endl;
	cout&amp;lt;&amp;lt;"\n\n";
}


Piece board::turnToPiece()  //converts current turn(1 for X, 0 for O) to piece
{
	if(turn&amp;gt;0)
		return CROSS;
	else
		return NAUGHT;
}

void board::alterTurn()  //alternates the turn
{
	turn*=-1;
}

void board::playHuman()
{
	int r,c;
	Piece p=turnToPiece();
	
	do
	{
		cout&amp;lt;&amp;lt;"Enter your move\n";
		cin&amp;gt;&amp;gt;r&amp;gt;&amp;gt;c;
	}while(makeMove(r,c,p)==false);
	drawBoard();
	alterTurn();
}

void board::playComp()
{
	int r,c;
	Piece p=turnToPiece();

	/*do
	{
		r=rand()%3;
		c=rand()%3;
	}while(makeMove(r,c,p)==false);*/
	chooseMove(p,r,c);
	cout&amp;lt;&amp;lt;makeMove(r,c,p)&amp;lt;&amp;lt;endl;
	cout&amp;lt;&amp;lt;"r"&amp;lt;&amp;lt;r&amp;lt;&amp;lt;endl;
	cout&amp;lt;&amp;lt;"c"&amp;lt;&amp;lt;c&amp;lt;&amp;lt;endl;
	drawBoard();
	alterTurn();
}

int board::chooseMove(Piece p, int &amp;amp;row, int &amp;amp;col) //minimax algorithm
{
	int imVal;
	int reply;
	int bestVal;
	int dr,dc;
	Piece opp;

	if((imVal=evaluate())!=UNDEFINED)
		return imVal;
	
	if(p==NAUGHT)
	{
		opp=CROSS;
		bestVal=-1;
	}
	else
	{
		opp=NAUGHT;
		bestVal=1;
	}

	for(int r=0;r&amp;lt;3;r++)
	{
		for(int c=0;c&amp;lt;3;c++)
		{
			if(hold[r][c]==EMPTY)
			{
				placeMove(r,c,p);
				reply=chooseMove(opp,dr,dc);
				
				placeMove(r,c,EMPTY);
			
				if(((p==NAUGHT) &amp;amp;&amp;amp; (reply&amp;gt;=bestVal)) || ((p==CROSS) &amp;amp;&amp;amp; (reply&amp;lt;=bestVal)))
				{
					bestVal=reply;
					row=r;
					col=c;
				}
			}
		}
	}
	return bestVal;
}
&lt;/pre&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/cplusgameprogramming/368950/368950/minimax-algorithm-not-working-properly/</guid>
      <pubDate>Sat, 26 Jan 2008 21:34:20 -0700</pubDate>
      <category>C++ Game Development</category>
    </item>
    <item>
      <title>Re: minimax algorithm not working properly</title>
      <link>http://www.programmersheaven.com/mb/cplusgameprogramming/368950/369783/re-minimax-algorithm-not-working-properly/#369783</link>
      <description>: hello i got frustrated after making several tries to make the &lt;br /&gt;
: minimax algorithm work in my console tic tac toe. the problem is the &lt;br /&gt;
: algorithm&lt;br /&gt;
: doesn't play the game as it should.The thing is it just tries to &lt;br /&gt;
: play the move that it 'sees' to be the best for itself without &lt;br /&gt;
: caring that its opponet&lt;br /&gt;
: is building an easy three to win the game. &lt;br /&gt;
I haven't read over your code entirely but I've seen this before with other tic-tac-toe games. One in particular I held on to for a while thinking I would modifiy it until I found this problem.&lt;br /&gt;
&lt;br /&gt;
The thing is Tic-Tac-Toe doesn't lend itself well to mini-max. Minimax is good for "where are going to be in X moves" but even for games that minimax well like reversi you need to handle the end game slightly different. Tic-Tac-Toe is all end game.&lt;br /&gt;
&lt;br /&gt;
The best thing to do is to find the "closest" 3 in a row if all paths are played out and make the move that has the most of them, randomly choosing ties. You'll notice this doesn't discriminate between X or O. If you have the chance to make 3 in a row after your opponent it doesn't matter. So you'll always want to block if your opponent will win before you. The problem with this becomes the first move. I think it will always pick the center, when we all know that this pretty much assures a tie game.&lt;br /&gt;
&lt;br /&gt;
Personally, I prefer a method &lt;a href="http://www.retroremakes.com/forum2/showpost.php?p=152971&amp;postcount=33"&gt;that learns&lt;/a&gt; over a perfect game.</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/cplusgameprogramming/368950/369783/re-minimax-algorithm-not-working-properly/#369783</guid>
      <pubDate>Sat, 23 Feb 2008 15:10:45 -0700</pubDate>
      <category>C++ Game Development</category>
    </item>
    <item>
      <title>Re: minimax algorithm not working properly</title>
      <link>http://www.programmersheaven.com/mb/cplusgameprogramming/368950/393007/re-minimax-algorithm-not-working-properly/#393007</link>
      <description>Hi,&lt;br /&gt;
&lt;br /&gt;
you can check out this nice tutorial article about &lt;a href="http://www.imapps.net/devblog/files/alphabeta-objc.html"&gt;minimax with alpha-beta pruning for Objective C&lt;/a&gt;, although it's for Objective C, you may find it useful for your problem...&lt;br /&gt;
&lt;br /&gt;
Cheers&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/cplusgameprogramming/368950/393007/re-minimax-algorithm-not-working-properly/#393007</guid>
      <pubDate>Mon, 29 Jun 2009 07:56:26 -0700</pubDate>
      <category>C++ Game Development</category>
    </item>
  </channel>
</rss>