読者です 読者をやめる 読者になる 読者になる

いものやま。

雑多な知識の寄せ集め

「BirdHead」の思考ルーチンを作ってみた。(その1)

前回までで「BirdHead」を遊べるようにした。

今回からは「BirdHead」の思考ルーチンを作っていく。

ランダムAI

まずは一番簡単なところから。
合法手からランダムにアクションを選択するだけの、ランダムAI。

//==============================
// BirdHead
//------------------------------
// RandomCom.swift
//==============================

import Foundation

class RandomCom: Player {
  private(set) var name: String
  private(set) var isCom: Bool

  init(name: String) {
    self.name = name
    self.isCom = true
  }

  func select(view: GameInfo.PlayerView) throws -> Action {
    let legalActions = view.legalActions
    let action = legalActions[Random.getUniformedRandom(legalActions.count)]
    return action
  }
}

Playerプロトコルに準拠するようにして、Player#select()を実装した。
ただ、学習はしないので、Player#learn()はデフォルトの実装を使っている。

実行例

試しにランダムAI同士で対戦させてみる。

import Foundation

let deck = Deck()
let game = GameInfo(deck: deck, playerCount: 4)
let players: [Player] = [
  RandomCom(name: "Random Com 1"),
  RandomCom(name: "Random Com 2"),
  RandomCom(name: "Random Com 3"),
  RandomCom(name: "Random Com 4"),
]

let controller = GameController(gameInfo: game, players: players)
controller.output = true
try! controller.start()

ビルドして実行すると、次のような感じ。

--------------------
deal...
----------
[Random Com 1]
selected action: Play([8])
[Random Com 2]
selected action: Play([10])
[Random Com 3]
selected action: Discard([4])
[Random Com 4]
selected action: Play([11])
----------
trick 0 is done.
Random Com 4 takes trick.
----------
[Random Com 4]
selected action: Play([7])
[Random Com 1]
selected action: Discard([2])
[Random Com 2]
selected action: Discard([3])
[Random Com 3]
selected action: Play([11])
----------
trick 1 is done.
Random Com 3 takes trick.
----------
[Random Com 3]
selected action: Play([8])
[Random Com 4]
selected action: Play([8])
[Random Com 1]
selected action: Discard([2])
[Random Com 2]
selected action: Discard([3])
----------
trick 2 is done.
Random Com 4 takes trick.
----------
[Random Com 4]
selected action: Play([4])
[Random Com 1]
selected action: Play([10])
[Random Com 2]
selected action: Discard([3])
[Random Com 3]
selected action: Play([11])
----------
trick 3 is done.
Random Com 3 takes trick.
----------
[Random Com 3]
selected action: Play([4])
[Random Com 4]
selected action: Play([6])
[Random Com 1]
selected action: Play([6])
[Random Com 2]
selected action: Play([6])
----------
trick 4 is done.
Random Com 2 takes trick.
----------
[Random Com 2]
selected action: Play([5])
[Random Com 3]
selected action: Play([6])
[Random Com 4]
selected action: Play([7])
[Random Com 1]
selected action: Play([9])
----------
trick 5 is done.
Random Com 1 takes trick.
----------
[Random Com 1]
selected action: Play([9])
[Random Com 2]
selected action: Discard([3])
[Random Com 3]
selected action: Discard([5])
[Random Com 4]
selected action: Play([10])
----------
trick 6 is done.
Random Com 4 takes trick.
----------
[Random Com 4]
selected action: Play([9])
[Random Com 1]
selected action: Play([10])
[Random Com 2]
selected action: Discard([4])
[Random Com 3]
selected action: Discard([5])
----------
trick 7 is done.
Random Com 1 takes trick.
----------
[Random Com 1]
selected action: Play([6])
[Random Com 2]
selected action: Discard([4])
[Random Com 3]
selected action: Play([9])
[Random Com 4]
selected action: Discard([2])
----------
trick 8 is done.
Random Com 3 takes trick.
----------
deal is done.
last cards:
Random Com 1: 7
Random Com 2: 5
Random Com 3: 8
Random Com 4: 7
["Random Com 3"] lose in deal.
minus points:
Random Com 1: []
Random Com 2: []
Random Com 3: [8]
Random Com 4: []
--------------------
deal...

〜省略〜

--------------------
deal...
----------
[Random Com 2]
selected action: Play([5])
[Random Com 3]
selected action: Play([7])
[Random Com 4]
selected action: Play([11])
[Random Com 1]
selected action: Discard([2])
----------
trick 0 is done.
Random Com 4 takes trick.
----------
[Random Com 4]
selected action: Play([4, 4])
[Random Com 1]
selected action: Play([5, 6])
[Random Com 2]
selected action: Play([9, 9])
[Random Com 3]
selected action: Discard([2, 2])
----------
trick 1 is done.
Random Com 2 takes trick.
----------
[Random Com 2]
selected action: Play([3, 3])
[Random Com 3]
selected action: Play([5, 9])
[Random Com 4]
selected action: Discard([3, 5])
[Random Com 1]
selected action: Play([6, 10])
----------
trick 2 is done.
Random Com 1 takes trick.
----------
[Random Com 1]
selected action: Play([11])
[Random Com 2]
selected action: Play([11])
[Random Com 3]
selected action: Discard([2])
[Random Com 4]
selected action: Discard([6])
----------
trick 3 is done.
Random Com 2 takes trick.
----------
[Random Com 2]
selected action: Play([7, 7])
[Random Com 3]
selected action: Discard([3, 4])
[Random Com 4]
selected action: Discard([6, 8])
[Random Com 1]
selected action: Discard([3, 4])
----------
trick 4 is done.
Random Com 2 takes trick.
----------
[Random Com 2]
selected action: Play([4])
[Random Com 3]
selected action: Play([6])
[Random Com 4]
selected action: Play([10])
[Random Com 1]
selected action: Play([10])
----------
trick 5 is done.
Random Com 1 takes trick.
----------
deal is done.
last cards:
Random Com 1: 7
Random Com 2: 10
Random Com 3: 8
Random Com 4: 8
["Random Com 2"] lose in deal.
minus points:
Random Com 1: [9, 8]
Random Com 2: [9, 11, 10]
Random Com 3: [8]
Random Com 4: [10]
--------------------
game ended.
total minus points:
Random Com 1: 17
Random Com 2: 30
Random Com 3: 8
Random Com 4: 10
["Random Com 2"] lose.

まぁ、とりあえずプレイは出来てる、というレベル。
(といっても、手札が見えているわけではないので、評価は難しいけど)

一番最後のディールの5トリック目と最後のカードを見てみると、Random Com 2は手札が4, 10の状態で4をリードしていたり、Random Com 3も手札が6, 8の状態で6をプレイしていたり、ランダムに手を選択するんだと、こんなことも出てくるよなぁ、と。

ということで、次はもうちょいまともなAIを考えていく。

今日はここまで!