> For the complete documentation index, see [llms.txt](https://kashiwara.gitbook.io/rubydesurufurusukuratchibe/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kashiwara.gitbook.io/rubydesurufurusukuratchibe/rojikkuno/sheng-li-pan-ding.md).

# 勝利判定

勝利判定を実装します。

引き続き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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://kashiwara.gitbook.io/rubydesurufurusukuratchibe/rojikkuno/sheng-li-pan-ding.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
