3

I've implemented a tic-tac-toe with ai but now im facing one problem, how to rate a board of tic-tac-toe game ?

Maybe at start I will descript how it should work :

  1. We have n boards of tic-tac-toe game (with different variants)
  2. Our ai should rate which board is the best to move on/the worst for opponent
  3. Ai calculate move by minimax algorithm (done)

Problem is in 2. Is there any way to "rate" a board?

I want to say that i dont want anybody to write me a code, just to help me finding algorithm or something :)

Thanks for help guys!

Edit #1

Ok, i have a minimax to play a board, but how to rate many boards and choose which is the best. Maybe im not clearly say what i want so i will show it.

e = empty

 *   x | e | e      e | o | e
 *  ---+---+---    ---+---+---
 *   x | e | e      e | o | e
 *  ---+---+---    ---+---+---
 *   o | e | e      x | x | e

And now, my implementation of minimax algorith is just telling me where i should put my sign (lets say o) but I need to tell on which board, so how to use it to rate whole board to choose on which to play ?

Minimax code :

minimax : function(tempBoard,depth){
    if (CheckForWinner(tempBoard) !== 0)
        return score(tempBoard, depth);
    depth+=1;
    var scores = new Array();
    var moves = new Array();
    var availableMoves = Game.emptyCells(tempBoard);
    var move, possibleGame, maxScore, maxScoreIndex, minScore,minScoreIndex;

    for(var i=0; i < availableMoves.length; i++) {
        move = availableMoves[i];
        possibleGame = Game.getNewBoard(move,tempBoard);
        scores.push(Ai.minimax(possibleGame, depth));
        moves.push(move);
        tempBoard = Game.undoMove(tempBoard, move);
    }
    if (Game.turn === "ai") {
        maxScore = Math.max.apply(Math, scores);
        maxScoreIndex = scores.indexOf(maxScore);
        choice = moves[maxScoreIndex];
        return scores[maxScoreIndex];

    } else {
        minScore = Math.min.apply(Math, scores);
        minScoreIndex = scores.indexOf(minScore);
        choice = moves[minScoreIndex];
        return scores[minScoreIndex];
    }
}
Direkts
  • 93
  • 7
  • Try and take a look at the MINIMAX algorithm and alter it cater your need! – N00b Pr0grammer Feb 12 '16 at 08:00
  • Are you sure this method returns __where__ the player should make his move? And I don't mean *where* as in which board, I mean *where* as in which position. From what I can see, you're only returning the best __score__ for each player `return scores[minScoreIndex]` and `return scores[maxScoreIndex]`. You're not returning where the player should make his move to achieve that score. – Omar Sharaki Feb 17 '16 at 18:18

2 Answers2

2

The rating of moves of Tic-tac-toe can be done using the Minimax algorithm. This algorithm aims at executing the move to evaluate, switch to the perspective of the opponent (which in turn tries to do the respective best move as well) and recursively evaluate moves until one of the players has won (which means that a leaf of the game tree has been reached).

Although implementation of the basic version of this algorithm is not incredibly difficult, understanding the approach is crucial; note that the 'artificial intelligence' basically consists out of hypothetically playing the entire game with all possible moves - it is not a heuristic but an exact algorithm. It can be refined using Alpha-Beta-Pruning to spare subtrees of the game tree which are not necessary for evaluation.

Codor
  • 17,447
  • 9
  • 29
  • 56
  • 2
    There are less than 9!=362880 possible games. One may wonder if pruning is of any use. –  Feb 12 '16 at 11:54
  • @YvesDaoust Perhaps it is not of any use, I just included the remark for the sake of completeness. Imagine running the Minimax algorithm on _extremely_ limited hardware. – Codor Feb 12 '16 at 12:01
1

Here is one formula with which you can rate the board:

value = (10 * x3 + 3 * x2 + x1) - (10 * o3 + 3 * o2 + o1)

where:

  • xN => number of rows/columns/diagonals with N x's on it and no o's
  • oN => number of rows/columns/diagonals with N o's on it and no x's

This assumes max-player is X. You can change signs if its otherwise.

vish4071
  • 5,135
  • 4
  • 35
  • 65
  • Thanks, great answer @vish4071! Just one thing : **x1** = number of rows with N x's on it and no o's **x2** = number of columns with N x's on it and no o's **x3** = number of diagonals with N x's on it and no o's **o1** = number of rows with N o's on it and no x's **o2** = number of columns with N o's on it and no x's **o3** = number of diagonals with N o's on it and no x's Is this correct ? – Direkts Feb 12 '16 at 12:21
  • Yeah. Actually, more like, `x1 = number of rows with 1 x's on it and no o's` and so on... It is because if there are `o's` on that row, there is no possibility of ever winning by drawing on that row. – vish4071 Feb 12 '16 at 12:27
  • So **x3** = number of rows with 3 x's on it and no o's ? – Direkts Feb 12 '16 at 12:34
  • Yeah...basically, that ends the game. I've used this formula in one of my previous implementations of tic-tac-toe bot. Basically, if ever you have score >10, there is a forced win. Similarly, if score < -10, you are forcibly losing. Though that is sufficient condition, it is not necessary. – vish4071 Feb 12 '16 at 12:34