need help on java tic tac toe fastest minimax search with AlphaBeta pruning

Started by
1 comment, last by capn_midnight 19 years, 6 months ago
guys how do you implement a 3x3 tic tac toe using fastest minimax search with AlphaBeta pruning? I am using java to compute this. I am sick of using a bunch of if statement when it cant possible make it unbeatable. I have initialize the game state to gameState[row][col]. This is the actual mapping 0,0|1,0|2,0 ----------- 0,1|1,1|2,1 ----------- 0,2|1,2|2,2 What I am doing now... if(playerState==gameState[0][2]&&playerState==gameState[2][2]&&gameState[1][2] == emptyBoard) { board[1][2]=AIstate; return; } This is to block the player when it is at (0,2)and(2,2) I really need help }
Advertisement
After implementation of NegaMax my AI becomes very stupid why is it so?

private int evaluateSituation(int player)
{
int best=-100000;

for(int row=0; row<3; ++row)
{
for(int column=0; column<3;++column)
{
if(board[row][column] == emptyBoard)
{
board[row][column]=playerPosition;
int evaluate=-evaluateSituation(-player);
board[row][column]=emptyBoard;
if(evaluate > best)
best=evaluate;
}
}
}
return best;
}

public void setSkillSmart()
{
int worst=10000000;
int bestx=-1, besty=-1;

for(int row=0; row<3; ++row)
for(int column=0; column<3; ++column)
if(board[row][column] == emptyBoard)
{
board[row][column]=computerPosition;

int evaluate=evaluateSituation(-computerPosition);
board[row][column]=emptyBoard;
if(evaluate < worst)
{
worst=evaluate;
bestx=row;
besty=column;
}
}
board[bestx][besty]=computerPosition;
}
you can use if statements to make the game unbeatable.

edit: hey, that was fun, implementing an exhaustive search for tictactoe. I'm now trying to generalize it for any NxN grid.

edit 2: basically, it's all about maximizing the win paths. Since TTT is such a small game, this is easy. You pick a point to play, and then iterate through for all combinations of the opponent's choice, multiplied by all combos of your NEXT choice (which is the point of recursion). Luckily, this results in stack calls only upto 8 methods deep.

In the future, post code in a [ source ] [ / source ] set of tags (remove the spaces, I did that to avoid the formatting).

you're hardcoding your board states way too much. In fact, the game should have no concept of the pattern of the board state, it should only be concerned with maximizing the branching paths to victory. It just so happens that this comes out into a pattern.

[Edited by - capn_midnight on October 3, 2004 9:51:29 PM]

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

This topic is closed to new replies.

Advertisement