# メイン部分の実装

ゲームのメイン部分を実装していきます

完了したら三目並べで遊べるようになります

## ゲームの進行部分の実装を考えよう

今まで実装してきたメソッド群を使って、ゲームの進行部分を実装してみましょう

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

ゲームの進行部分を実装する
{% endhint %}

## 初期値の定義

plyaer変数, board変数の初期値を定義します

```ruby
player = 1
board = [
  [0, 0, 0],
  [0, 0, 0],
  [0, 0, 0]
]
```

## ゲームループの実装

loop構文を使いゲームのループを表現します

以前に実装したcontinue?メソッドを使います

```ruby
player = 1
board = [
  [0, 0, 0],
  [0, 0, 0],
  [0, 0, 0]
]


loop do
  break if !continue?(player, board)
end
```

## 入力部分の実装

プレイヤーがコマを配置できるようにします

以前に実装したgets\_positionメソッドを使います

```ruby
player = 1
board = [
  [0, 0, 0],
  [0, 0, 0],
  [0, 0, 0]
]

loop do
  row, col = gets_piece_location()

  break if !continue?(player, board)
end
```

## ゲーム板の更新

入力できるようになったのでゲーム板にコマを置けるようにします

```ruby
player = 1
board = [
  [0, 0, 0],
  [0, 0, 0],
  [0, 0, 0]
]

loop do
  row, col = gets_piece_location()

  place_piece(board, player, row, col)

  break if !continue?(player, board)
end
```

## プレイヤーの入れ替え

プレイヤーの入れ替え部分を実装します。

相手のプレイヤーを取得するメソッドがあれば便利そうです。

```ruby
# 説明: 対戦中の相手プレイヤーを返却する
# 引数: player: プレイヤーを表す。1 or 2の数値
# 戻り値: 1の時 => 2
#        2の時 => 1
def get_opponent_player player
  return player == 1 ? 2 : 1
end
```

このget\_opponent\_playerメソッドを適用します。

```ruby
player = 1
board = [
  [0, 0, 0],
  [0, 0, 0],
  [0, 0, 0]
]

loop do
  row, col = gets_piece_location()

  place_piece(board, player, row, col)

  break if !continue?(player, board)

  player = get_opponent_player(player)
end
```

## ゲーム画面の出力

今のままでは、何が起こっているのか分からないのでゲーム画面を出力しましょう。

```ruby
player = 1
board = [
  [0, 0, 0],
  [0, 0, 0],
  [0, 0, 0]
]

loop do
  row, col = gets_piece_location()

  place_piece(board, player, row, col)

  print_board(board)
   
  break if !continue?(player, board)

  player = get_opponent_player(player)
end
```

## 試合結果の出力

試合結果の出力メソッドを実装しましょう。

```ruby
# 説明: ゲームの結果をコンソールに出力する
# 引数: board: ゲーム板, 3 x 3の二次元配列
# 戻り値: なし
def print_result board
  if win?(1, board)
    puts "Player1が勝利しました!"
  elsif win?(2, board)
    puts "Player2が勝利しました!" 
  else
    puts "引き分けです!"
  end
end
```

上記で実装したメソッドをメイン部分に適用させて完了です。

```ruby
player = 1
board = [
  [0, 0, 0],
  [0, 0, 0],
  [0, 0, 0]
]

loop do
  row, col = gets_piece_location()

  place_piece(board, player, row, col)

  print_board(board)

  break if over?(player, board)

  player = get_opponent_player(player)
end

print_result(board)
```

基本ロジックの実装はこれで完了です！

以下にプログラムを動作させた例をのせておきます。

```
行を入力してください:0
列を入力してください:0

 o  N  N 
 N  N  N 
 N  N  N 

行を入力してください:1
列を入力してください:1

 o  N  N 
 N  x  N 
 N  N  N 

行を入力してください:0 
列を入力してください:1

 o  o  N 
 N  x  N 
 N  N  N 

行を入力してください:1
列を入力してください:0

 o  o  N 
 x  x  N 
 N  N  N 

行を入力してください:0
列を入力してください:2

 o  o  o 
 x  x  N 
 N  N  N 

Player1が勝利しました!
```

この章で実装した内容のフルセットは以下のリポジトリに乗せておきました。

なにかトラブルが発生した場合は、参照してみてください。

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

## このプログラムの問題点

お気づきの方もいると思いますが、このプログラムは入力部分に欠点があり不完全です。

相手が置いた場所にも上書きして置けるし、ゲーム板の外にも置くことができます(エラーになりますが)。

ここら辺の問題を四章のリファクタリングで修正していきたいと思います。


---

# 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/rojikkuno/meinno.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.
