2011/05/23

SimpleHeuristicsPlayer

単純なheuristicsに基づくプレイヤーを実装する。特に、reversiの場合はこれだけでも十分に強いプレイヤーになる。
class SimpleHeuristicsPlayer[N <: Node[N]] extends Player[N] {

  override def play(node: N, last: Move): Move = {
    val moves = node.possibleMoves(marker)
    var nextMove = List[Move]()
    var maxS = Int.MinValue
    for (m <- moves) {
      (m: @unchecked) match {
        case PutMarker(x, y, _) =>
          val s = score(x, y)
          if (s > maxS) {
            nextMove = List(m)
            maxS = s
          } else if (s == maxS) {
            nextMove = m :: nextMove
          }
        case Pass =>
          nextMove = List(Pass)
      }
    }
    nextMove(Random.nextInt(nextMove.length))
  }

  val scores = List( 8,  2,  7,  4,
                     2,  1,  3,  4,
                     7,  3,  6,  5,
                     4,  4,  5,  0)

  def score(x: Int, y: Int): Int =
    if (x < 4) {
      if (y < 4) {
        scores(x + y * 4)
      } else {
        scores(x + (7 - y) * 4)
      }
    } else {
      if (y < 4) {
        scores((7 - x) + y * 4)
      } else {
        scores((7 - x) + (7 - y) * 4)
      }
    }

}

実際に、RandomPlayerとの対戦結果は次のとおり。
D: 802, L: 170, -: 28
D: 185, L: 790, -: 25
先手、後手でも8割近く勝てる。まだ、2割も負けるともいうが。


# 次の手をどこに打つかということをスコアにしているため、このPlayerはreversi専用となっている。

0 件のコメント:

コメントを投稿