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

# 入力部分

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

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

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

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

```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
```
