# TicTacToeClassの実装

tic\_tac\_toe.rbの内容をクラスにし、リファクタリングした内容でゲームを動かしたいと思います。

## TicTacToeClassの実装

TicTacToeクラスを実装します。

GameFactoryを使ってゲームの生成を隠蔽し、ゲーム全の進行のみを管理するようにします。

```ruby
# frozen_string_literal: true

require_relative "game_manager_factory"

# 三目並べの実行クラス
class TicTacToe
  class << self
    # 概要: ゲームのmain部分
    #       このメソッドをコールするとゲームが始まる
    # 引数: なし
    # 戻り値: なし
    def start
      game = GameManagerFactoryClass.create

      loop do
        game.player_turn
        game.print_board
        break if game.over?

        game.change_to_opponent_turn
      end

      game.print_result
    end
  end
end

```

main.rbも修正します。

```ruby
require "./tic_tac_toe/tic_tac_toe"

TicTacToe.start()
```

第四章はこれにて終了です。お疲れ様でした！

いらなくなったファイル(tic\_tac\_toe\_test.rb)はけしちゃってOKです！

最後に、この章のソースコードを以下に記載しておきます。

{% embed url="<https://github.com/Kashiwara0205/ruby-tic-tac-toe/tree/master/ch4>" %}


---

# 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/tictactoeclassno.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.
