井字游戏评分板算法 [英] Tic-tac-toe rate a board algorithm

查看:144
本文介绍了井字游戏评分板算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用ai实施了井字游戏,但现在我面临一个问题,如何对一个井字游戏板进行评级?

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. 我们有n个井字游戏板(有不同的变体)

  2. 我们的ai应该评估哪个板最适合/最不利于对手

  3. Ai通过minimax算法(完成)来计算移动
  4. >
  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)

问题出在2。有什么方法可以评估董事会?

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 :)

感谢帮助人员!

编辑#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 =空

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

现在,我对minimax算法的实现只是告诉我应该在哪里放置标志(说o),但是我需要告诉哪个板,那么如何使用它来评估整个板以选择在哪个板上玩?

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代码:

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];
    }
}


推荐答案

此处是可以用来评估董事会的一个公式:

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

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

其中:


  • xN =>带有 N x's 上没有 o

  • oN =>行数/列数/对角线数 N o是,没有 x是

  • 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

这假定最大玩家数 X 。否则,您可以更改符号。

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

这篇关于井字游戏评分板算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆