# BoardClassの実装

ゲーム板の情報を管理するBoardClassを作っていきます

### ボードの操作処理を一まとめにする

board.rbを新たに作成していきましょう。

置く場所があるのか・ゲーム板の出力に関してはボードクラスに尋ねることにします。

```ruby
# frozen_string_literal: true

require_relative "const"
require_relative "tic_tac_toe_input_error"
require_relative "tic_tac_toe_validator"

# ゲームの板に関する情報を管理する
class Board
  def initialize(board)
    @board = board
  end

  def board_state
    @board
  end

  # 概要: 現在のプレイヤーのゲームの勝利判定
  # 引数:   piece: プレイヤーのコマ
  # 戻り値: 勝利している場合 => true
  #        それ以外 => false
  def win?(piece)
    # 横の判定
    return true if piece == @board[0][0] && piece == @board[0][1] && piece == @board[0][2]
    return true if piece == @board[1][0] && piece == @board[1][1] && piece == @board[1][2]
    return true if piece == @board[2][0] && piece == @board[2][1] && piece == @board[2][2]

    # 縦の判定
    return true if piece == @board[0][0] && piece == @board[1][0] && piece == @board[2][0]
    return true if piece == @board[0][1] && piece == @board[1][1] && piece == @board[2][1]
    return true if piece == @board[0][2] && piece == @board[1][2] && piece == @board[2][2]

    # 斜めの判定
    return true if piece == @board[0][0] && piece == @board[1][1] && piece == @board[2][2]
    return true if piece == @board[0][2] && piece == @board[1][1] && piece == @board[2][0]

    false
  end

  # 概要: コマを配置する場所があるかどうかを判定する
  # 引数: なし
  # 戻り値: コマを配置する場所がある => true
  #         コマを配置する場所がない => false
  def can_plase_piece?
    @board.any? { |row| row.any? { |a| OPEN_SLOT == a } }
  end

  # 概要: ゲーム板にコマを配置する
  # 引数: row: 入力された更新する行番号
  #      col: 入力された更新する列番号
  #      piece: プレイヤーのコマ
  # 戻り値: なし
  def update(row, col, piece)
    TicTacToeValidator.validate_place!(@board, row, col)
    @board[row][col] = piece
  end

  # 説明: ゲーム板の状態をコンソールに出力する
  # 引数: なし
  # 戻り値: なし
  def print_board
    puts ""
    @board.each do |row|
      row.each do |e|
        print " N " if OPEN_SLOT == e
        print " o " if PLAYER1_PIECE == e
        print " x " if PLAYER2_PIECE == e
      end
      puts ""
    end
    puts ""
  end
end
```

### テスト実装

元あったテストファイルからPlayerクラスのテストを抽出して新たにboard\_test.rbを作成します

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

&#x20;player\_test.rbを作成しテストを実装する
{% endhint %}

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

{% embed url="<https://github.com/Kashiwara0205/ruby-tic-tac-toe/blob/master/ch4/code/test/board_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/rifakutaringu/boardclassno.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.
