# 勝利判定

勝利判定を実装します。

引き続きmain.rbに記載していくようにお願いします。

## 三目並べの勝利判定を考えよう

三目並べの勝利判定とは、縦、横、もしくは斜めに自分のコマが3つ揃うことです。

それをプログラムで表現します。

```ruby
# 説明: ゲームの勝利判定
# 引数: player: プレイヤーを表す。1 or 2の数値
#       board: ゲーム板, 3 x 3の二次元配列
# 戻り値: 勝利している場合 => true
#       それ以外 => false
def win? player, board
end
```

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

win?メソッドを実装してみましょう
{% endhint %}

ゲーム板の出力関数の時と同じように呼び出してデバッグしてください。

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

# trueが帰ってきたらOK
puts win?(1, board)
```

## 三目並べの勝利判定を実装する

player1がコマ1を置きplayer2がコマ2を配置すると考えます

以下のように実装しました

```ruby
# 説明: ゲームの勝利判定
# 引数: player: プレイヤーを表す。1 or 2の数値
#      board: ゲーム板, 3 x 3の二次元配列
# 戻り値: 勝利している場合 => true
#       それ以外 => false
def win? player, board
  
  # 横の判定
  return true if player == board[0][0] && player == board[0][1] && player == board[0][2]
  return true if player == board[1][0] && player == board[1][1] && player == board[1][2]
  return true if player == board[2][0] && player == board[2][1] && player == board[2][2]
  
  # 縦の判定
  return true if player == board[0][0] && player == board[1][0] && player == board[2][0]
  return true if player == board[0][1] && player == board[1][1] && player == board[2][1]
  return true if player == board[0][2] && player == board[1][2] && player == board[2][2]
  
  # 斜めの判定
  return true if player == board[0][0] && player == board[1][1] && player == board[2][2]
  return true if player == board[0][2] && player == board[1][1] && player == board[2][0]
  
  return false
end
```

可読性のことを考えて、あえてループは使いませんでした。

しかし3目でなく5目とかになってくると、この実装手法で横に長くなっていきます。

数が増える場合は素直にループを使いましょう。


---

# 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/sheng-li-pan-ding.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.
