# 入力部分

ゲームに「どこのマスにコマを配置するのか」を命令する入力部分を実装します。

## 入力部分の実装を考えよう

入力部分の実装を考えましょう。

人間から行番号と列番号の情報を受け取って返却します。

```ruby
# 説明: コマを配置する行と列の値をコンソールから受け取り返却する
# 引数: なし
# 戻り値: 入力された行と列の情報を配列に格納して数値で返す => [1, 1]
def gets_piece_location
end
```

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

gets\_piece\_locationメソッドを実装してみましょう
{% endhint %}

## Rubyのgets

Rubyには入力を受け取る事ができるgetsという名前の関数が実装されています。

今回は、getsを使って入力部分を実装します。

{% embed url="<https://docs.ruby-lang.org/ja/latest/method/IO/i/gets.html>" %}

## キャスト(型変換)

"1"を1に変換したり、300.5を"300.5"に変換する事をキャストといいます。

getsメソッドから入ってくるデータは、文字列型なので数値として扱いたい場合は

キャストする必要があります。

文字列から整数に変換する場合はto\_iメソッドを使用します。

{% embed url="<https://docs.ruby-lang.org/ja/latest/class/String.html#I_TO_I>" %}

## 入力部分を実装する

下記に実装例を記載します

to\_iメソッドを使って文字列から整数に変換しているのがポイントです

```ruby
# 説明: コマを配置する行と列の値をコンソールから受け取り返却する
# 引数: なし
# 戻り値: 入力された行と列の情報を配列に格納して数値で返す => [1, 1]
def gets_piece_location
  print "行を入力してください:"
  row = gets

  print "列を入力してください:"
  col = gets
  return row.to_i, col.to_i
end
```

メソッドが正しく動作するかを見てみましょう。

```ruby
row, col = gets_piece_location
puts "行: #{row}"
puts "列: #{col}"
```

行に1列に1をそれぞれ入力して以下のような出力になれば成功です

```
行: 1
列: 1
```


---

# 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/ru-li-bu-fen.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.
