# OrderlyComPlayerの追加

### エラークラスの追加

comの挙動で何か意図しないことが発生した時にキャッチできるようエラークラスを定義して

使うことにします。

新たにerrorsフォルダを作成し、com\_error.rbを実装します。

既存のtic\_tac\_toe\_input\_error.rbも移動させてください。

```ruby
# frozen_string_literal: true

# 3目並べのCOMに関するエラークラス
class ComError < StandardError; end
```

### OrdelyComPlayerを追加する

マスが空いてたら埋めにいくOrdelyComPlayerを追加します。

先ほど実装したBasePlayerClassを利用して実装しましょう

{% hint style="info" %}
**課題コーナー**

BasePlayerClassを継承したOrdelyComPlayerを作成する

コマを配置できなければComErrorを発生させる
{% endhint %}

私の場合、以下のように実装しました。

```ruby
# frozen_string_literal: true

require_relative "base_player"
require_relative "../const"
require_relative "../errors/com_error"

# 順番に見ていって空きマスがあれば配置するCOM
class OrdelyComPlayer < BasePlayer
  # 概要: ゲーム板を受け取りマスを順番に見ていって空いてたら、その行と列番号を返却する
  # 引数: board_state: ゲーム板の情報
  # 戻り値: 行と列の情報を配列に格納して返却する => [1, 1]
  def gets_piece_location(board_state)
    board_state.each_with_index do |row, i|
      row.each_with_index do |e, j|
        return [i, j] if OPEN_SLOT == e
      end
    end

    raise ComError, "OrderlyComクラスで配置場所が決まりませんでした"
  end
end
```

### テストの追加

最後にテストを追加して完了となります。

{% hint style="info" %}
**課題コーナー**

ordely\_com\_player\_test.rbを実装して見てください
{% endhint %}

参考コードは以下のURLから参照してください。

{% embed url="<https://github.com/Kashiwara0205/ruby-tic-tac-toe/blob/master/ch5/code/test/players/orderly_com_player_test.rb>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kashiwara.gitbook.io/rubydesurufurusukuratchibe/zui-zhong-zhang-ji-neng-zhui-jia/orderlycomplayerno.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
