//============================================================================= // UpperHandPlayer.java // Copyright(c) 1998 Satoshi Kobayashi, All rights reserved. //============================================================================= import java.util.*; //============================================================================= // class UpperHandPlayer // コンピュータのプレーヤーを実装するクラス。 //============================================================================= public class UpperHandPlayer extends Object { //----------------------------------------------------------------------------- // クラス変数定義 //----------------------------------------------------------------------------- // 位置ごとの評価値 private final static int valueOfPosition[] = { 4, 7, 8, 7, 4, 7, 13, 15, 13, 7, 8, 15, 18, 15, 8, 7, 13, 15, 13, 7, 4, 7, 8, 7, 4, 3, 5, 5, 3, 5, 9, 9, 5, 5, 9, 9, 5, 3, 5, 5, 3, 2, 3, 2, 3, 5, 3, 2, 3, 2, 1, 1, 1, 1, 0 }; //----------------------------------------------------------------------------- // インスタンス変数定義 //----------------------------------------------------------------------------- // 着手の優先順位 private int priorityOfMove[] = new int[valueOfPosition.length]; //----------------------------------------------------------------------------- // コンストラクタ定義 //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // public UpperHandPlayer() // UpperHandPlayerのインスタンスを生成し、初期化を行う。 //----------------------------------------------------------------------------- public UpperHandPlayer() { // 着手の優先順位をランダムに並び替える。 Random random = new Random(); boolean flag[] = new boolean[priorityOfMove.length]; int i = 0; while (i < priorityOfMove.length) { int n = (int)(random.nextFloat() * priorityOfMove.length); // 位置をランダムに選ぶ。 if (! flag[n]) { // 選んだ位置が使用済みでない場合 priorityOfMove[i] = n; // 選んだ位置をi番目の着手位置とする。 flag[n] = true; // 選んだ位置を使用済みとする。 i++; } } // 位置ごとの評価値の大きい順に着手の優先順位を並び替える。 for (i = 0; i < priorityOfMove.length; i++) { int p = i; for (int j = i + 1; j < priorityOfMove.length; j++) { if (valueOfPosition[priorityOfMove[p]] < valueOfPosition[priorityOfMove[j]]) { p = j; } } int q = priorityOfMove[i]; priorityOfMove[i] = priorityOfMove[p]; priorityOfMove[p] = q; } } //----------------------------------------------------------------------------- // メソッド定義 //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // public int selectMove(UpperHandGame game) // gameから着手する位置を選択し返す。 //----------------------------------------------------------------------------- public int selectMove(UpperHandGame game) { int moves[] = game.moves(); // 着手可能な手を取得する。 // 着手可能な手の中で優先順位の一番高い手を返す。 for (int i = 0; i < priorityOfMove.length; i++) { for (int j = 0; j < moves.length; j++) { if (priorityOfMove[i] == moves[j]) { return moves[j]; } } } return 0; } }