# 比較

## 練習課題

[記入できるファイルをダウンロード](https://class.illogic.games/attachments/7)

### C#を読みましょう

以下の表は、C#で書いた命令とその日本語の意味が書いてある。空枠を完成してください。

<table id="bkmrk-c%23-%E6%97%A5%E6%9C%AC%E8%AA%9E-a-%3E-b-%E5%A4%89%E6%95%B0%E3%80%8Ca%E3%80%8D%E3%81%AF%E5%A4%89" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 34.5633%;"></col><col style="width: 65.4367%;"></col></colgroup><thead><tr><td class="align-center">**C#**

</td><td>**日本語**

</td></tr></thead><tbody><tr><td>```c#
a > b
```

</td><td>変数「a」は変数「b」よりも大きい？

</td></tr><tr><td>```c#
i <= 10
```

</td><td><details><summary>答え</summary>

変数「i」は「10」以下？

</details></td></tr><tr><td><details><summary>答え</summary>

```c#
playerHP > 0
```

</details></td><td>プレーヤーの体力は残っている？

</td></tr><tr><td>```c#
stageSelect == "Tutorial"
```

</td><td><details><summary>答え</summary>

選択されたステージはチュートリアルであるか？

</details></td></tr><tr><td>```c#
coinCount >= itemCost
```

</td><td><details><summary>答え</summary>

持っているコイン数はアイテム価格を超える？

</details></td></tr><tr><td><details><summary>答え</summary>

```c#
keyCount == 3
```

</details></td><td>鍵がちょうど3個ある？

</td></tr><tr><td><details><summary>答え</summary>

```c#
time < 60
```

</details></td><td>残りの時間は60秒未満？

</td></tr><tr><td>```c#
player != "CPU Player"
```

</td><td><details><summary>答え</summary>

プレーヤーはCPUプレーヤーではないね？

</details></td></tr></tbody></table>

### 比較を評価

以下の比較を確認し、「true」（真・はい）または「false」（偽・いいえ）をチェックしてください。ただし、事前に以下の変数をすでに作成されている：

```c#
int a = 100;
int b = 20;
float c = 5.0f;
```

<table border="1" id="bkmrk-%E6%AF%94%E8%BC%83-true-false-a-%3D%3D-1" style="border-collapse: collapse; width: 100%; height: 390.6px;"><colgroup><col style="width: 29.7974%;"></col><col style="width: 36.0094%;"></col><col style="width: 34.1932%;"></col></colgroup><thead><tr style="height: 29.8px;"><td class="align-center" style="height: 29.8px;">**比較**</td><td class="align-center" style="height: 29.8px;">**true**</td><td class="align-center" style="height: 29.8px;">**false**</td></tr></thead><tbody><tr style="height: 66.2px;"><td style="height: 66.2px;">```c#
a == 100;
```

</td><td class="align-center" style="height: 66.2px;">✔</td><td style="height: 66.2px;">  
</td></tr><tr style="height: 66.2px;"><td style="height: 66.2px;">```c#
b < c
```

</td><td style="height: 66.2px;"><details><summary>答え</summary>

falseでした！ &gt;&gt;

・b は 20

・c は 5

・b は c 未満 (&lt;) ではない

</details>  
</td><td style="height: 66.2px;"><details><summary>答え</summary>

✔

</details></td></tr><tr style="height: 66.2px;"><td style="height: 66.2px;">```c#
c >= a / b;
```

</td><td style="height: 66.2px;"><details><summary>答え</summary>

✔

</details></td><td style="height: 66.2px;"><details><summary>答え</summary>

 &lt;&lt; trueでした！

・c は 5

・a / b ⇒ 100 /20 ⇒ 5

・5は5以上( &gt;= )である

</details></td></tr><tr style="height: 66.2px;"><td style="height: 66.2px;">```c#
c * 6 < b * 2;
```

</td><td style="height: 66.2px;"><details><summary>答え</summary>

✔

</details>  
</td><td style="height: 66.2px;"><details><summary>答え</summary>

 &lt;&lt; trueでした！

・c×6 ⇒ 5×6 ⇒ 30

・b×2 ⇒ 20×2 ⇒ 40

・30は40未満（&lt;）である

</details></td></tr><tr style="height: 66.2px;"><td style="height: 66.2px;">```c#
a == b * c;
```

</td><td style="height: 66.2px;"><details><summary>答え</summary>

✔

</details></td><td style="height: 66.2px;"><details><summary>答え</summary>

 &lt;&lt; trueでした！

・aは100

・b×c⇒ 20×5 ⇒ 100

・同じ（==）である

</details></td></tr><tr style="height: 29.8px;"><td style="height: 29.8px;">```c#
b != a - 80;
```

</td><td style="height: 66.2px;"><details><summary>答え</summary>

 falseでした！ &gt;&gt;

・b は 20

・a - 80 ⇒ 100 - 80 ⇒ 20

・異なる（!=）のではない

</details></td><td style="height: 66.2px;"><details><summary>答え</summary>

✔

</details></td></tr></tbody></table>

### 論理演算子で条件を連結：「かつ」と「または」

ゲームを開発しているときに、if文で同時に２つのことを確認したい場合もある。

例えば、レースゲームの勝利条件は：

- ゴールに着いた
- 時間がまだ残っている

両方とも同時に「true」だったら、ゲームの勝ちになる。２つの条件同時に満たさないといけない場合は「AND演算子」を使いましょう

#### 「かつ」演算子：AND（アンド）：`&&`

この演算子で両方の条件を満たさないと「true」にならない。２つの条件「Ａ」と「Ｂ」があるとしたら：

<table border="1" id="bkmrk-a-b-a-%26%26-b-false-fal" style="border-collapse: collapse; width: 58.6905%; height: 145.334px;"><colgroup><col style="width: 33.3131%;"></col><col style="width: 33.3131%;"></col><col style="width: 33.3131%;"></col></colgroup><thead><tr style="height: 29.4667px;"><td class="align-center" style="height: 29.4667px;">A</td><td class="align-center" style="height: 29.4667px;">B</td><td class="align-center" style="height: 29.4667px;">A &amp;&amp; B</td></tr></thead><tbody><tr style="height: 27.4667px;"><td class="align-center" style="height: 27.4667px;"><span style="color: rgb(186, 55, 42);">false</span></td><td class="align-center" style="height: 27.4667px;"><span style="color: rgb(186, 55, 42);">false</span></td><td class="align-center" style="height: 27.4667px;"><span style="color: rgb(186, 55, 42);">false</span></td></tr><tr style="height: 29.4667px;"><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(22, 145, 121);">true</span></td><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(186, 55, 42);">false</span></td><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(186, 55, 42);">false</span></td></tr><tr style="height: 29.4667px;"><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(186, 55, 42);">false</span></td><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(22, 145, 121);">true</span></td><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(186, 55, 42);">false</span></td></tr><tr style="height: 29.4667px;"><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(22, 145, 121);">true</span></td><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(22, 145, 121);">true</span></td><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(22, 145, 121);">true</span></td></tr></tbody></table>

つまり、いずれかの条件が「false」でしたら、すべて「false」になる。上記の例で、C#にすると：

```c#
// 走行距離
float runDistance = 1235.0f;

// ゴールまでの距離
float goalDistance = 1200.0f

// 残りの時間
float timeLeft = 35.7f;

// 走行距離はゴールよりも大きく「かつ」残りの時間が０以上だったら
if (runDistance >= goalDistance && timeLeft > 0)
{
  Debug.Log("勝ち！");
}
```

これで、必ず２つの条件を満たしたら、ゲームクリアになることを保証できる。

#### 「または」演算子：OR（オア）：`||`

もう一つのパターンは「いずれかの条件が true だったら、すべて true になる」かある。例えば、あるゲームの中で２つのドアの開き方がある：

- ドアの鍵を持っている
- プレーヤーの力が十分だったら、自力で壊す

この場合は、１つだけの条件を満たしたら、ドアを開くことができるので、「または」の演算子を使えば良いでしょう。

<table border="1" id="bkmrk-a-b-a-%7C%7C-b-false-fal" style="border-collapse: collapse; width: 58.6905%; height: 145.334px;"><colgroup><col style="width: 33.3131%;"></col><col style="width: 33.3131%;"></col><col style="width: 33.3131%;"></col></colgroup><thead><tr style="height: 29.4667px;"><td class="align-center" style="height: 29.4667px;">A</td><td class="align-center" style="height: 29.4667px;">B</td><td class="align-center" style="height: 29.4667px;">A || B</td></tr></thead><tbody><tr style="height: 27.4667px;"><td class="align-center" style="height: 27.4667px;"><span style="color: rgb(186, 55, 42);">false</span></td><td class="align-center" style="height: 27.4667px;"><span style="color: rgb(186, 55, 42);">false</span></td><td class="align-center" style="height: 27.4667px;"><span style="color: rgb(186, 55, 42);">false</span></td></tr><tr style="height: 29.4667px;"><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(22, 145, 121);">true</span></td><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(186, 55, 42);">false</span></td><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(186, 55, 42);"><span style="color: rgb(22, 145, 121);">true</span></span></td></tr><tr style="height: 29.4667px;"><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(186, 55, 42);">false</span></td><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(22, 145, 121);">true</span></td><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(186, 55, 42);"><span style="color: rgb(22, 145, 121);">true</span></span></td></tr><tr style="height: 29.4667px;"><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(22, 145, 121);">true</span></td><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(22, 145, 121);">true</span></td><td class="align-center" style="height: 29.4667px;"><span style="color: rgb(22, 145, 121);">true</span></td></tr></tbody></table>

上記の例で、C#にすると：

```c#
// プレーヤーの力
float playerPower = 30;

// 今持っている鍵の数
int keyCount = 0;

// ドアの固さ
float doorHardness = 25;

// 鍵があれば「または」力は固さよりも大きければ…
if (keyCount > 0 || playerPower >= doorHardness)
{
  Debug.Log("ドアを開ける！");
}
```

この例だと、鍵がなくても力で壊せるので、ドアを開くことができる！

### 練習

以下、使った「かつ」と「または」の比較を確認し、「true」（真・はい）または「false」（偽・いいえ）を書いてください。ただし、事前に以下の変数をすでに作成されている：

```c#
int coin = 230;       // コインの数
int maxItem = 10;     // 最大のアイテムの数
int nowItem = 9;      // 現在持っているアイテムの数
int potionCost = 100; // ポーションのコスト
```

1\. 比較：

```c#
coin > potionCost && nowItem + 1 <= maxItem
```

<table border="1" id="bkmrk-true-false-%E7%AD%94%E3%81%88-%E2%9C%94-%E7%AD%94%E3%81%88-%3C" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 50.0596%;"></col><col style="width: 50.0596%;"></col></colgroup><thead><tr><td class="align-center" style="height: 29.8px;">**true**</td><td class="align-center" style="height: 29.8px;">**false**</td></tr></thead><tbody><tr><td><details><summary>答え</summary>

✔

</details></td><td><details><summary>答え</summary>

&lt;&lt; trueでした！

コインは230でポーションは100なので

```c#
coin > potionCost
```

は `true`（真）である

で、持っているアイテムは9個で、最大10個なので

```c#
nowItem + 1 <= maxItem 
```

も `true`（真）である

両方とも「true」であり、「true かつ true」は「true」

</details></td></tr></tbody></table>

2\. 比較：

```c#
potionCost > coin || nowItem >= maxItem
```

<table border="1" id="bkmrk-true-false-%E7%AD%94%E3%81%88-false%E3%81%A7" style="border-collapse: collapse; width: 100%; height: 90.1px;"><colgroup><col style="width: 50.0596%;"></col><col style="width: 50.0596%;"></col></colgroup><thead><tr style="height: 29.8px;"><td class="align-center" style="height: 29.8px;">**true**</td><td class="align-center" style="height: 29.8px;">**false**</td></tr></thead><tbody><tr style="height: 60.3px;"><td style="height: 60.3px;"><details><summary>答え</summary>

falseでした！&gt;&gt;

ポーション（100）はコイン（230）を超えないので

```c#
potionCost > coin
```

は「false」である。

で、持っているアイテムの数（9）最大アイテム以上ではないため

```c#
nowItem >= maxItem
```

も「false」である

「false」または「false」（両方も「偽」）であるため、結果は false である

</details></td><td style="height: 60.3px;"><details><summary>答え</summary>

✔

</details></td></tr></tbody></table>

3\. 比較：

```c#
potionCost * 2 <= coin && maxItem >= nowItem + 2
```

<table border="1" id="bkmrk-true-false-%E7%AD%94%E3%81%88-false-" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 50.0596%;"></col><col style="width: 50.0596%;"></col></colgroup><thead><tr><td class="align-center" style="height: 29.8px;">**true**</td><td class="align-center" style="height: 29.8px;">**false**</td></tr></thead><tbody><tr><td><details><summary>答え</summary>

false でした！&gt;&gt;

ポーション２個は200で、もっといるコインは230なので

```c#
potionCost * 2 <= coin
```

は `true`（真）である

だが、持っているアイテム（9個）にさらに２つを増やすと、最大数（10個）を超えてしまうので

```c#
maxItem >= nowItem + 2
```

も `false`（偽）である

「かつ」（&amp;&amp;）は両方とも「true」じゃないとだめなので、最終結果は「false」になってしまう。

</details></td><td><details><summary>答え</summary>

✔

</details></td></tr></tbody></table>

4\. 比較：

```c#
nowItem == 0 && coin != 0
```

<table border="1" id="bkmrk-true-false-%E7%AD%94%E3%81%88-false--1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 50.0596%;"></col><col style="width: 50.0596%;"></col></colgroup><thead><tr><td class="align-center" style="height: 29.8px;">**true**</td><td class="align-center" style="height: 29.8px;">**false**</td></tr></thead><tbody><tr><td><details><summary>答え</summary>

false でした！&gt;&gt;

持っているアイテムは０ではなく、９個なので

```c#
nowItem == 0
```

は `false`（偽）である

「かつ」（&amp;&amp;）は両方とも「true」じゃないとだめなので、もう一つの条件は関係なく、最終結果は「false」になってしまう。

</details></td><td><details><summary>答え</summary>

✔

</details></td></tr></tbody></table>

5\. 比較：

```c#
maxItem - nowItem > 9 || coin > 0
```

<table border="1" id="bkmrk-true-false-%E7%AD%94%E3%81%88-%E2%9C%94-%E7%AD%94%E3%81%88-%3C-1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 50.0596%;"></col><col style="width: 50.0596%;"></col></colgroup><thead><tr><td class="align-center" style="height: 29.8px;">**true**</td><td class="align-center" style="height: 29.8px;">**false**</td></tr></thead><tbody><tr><td><details><summary>答え</summary>

✔

</details></td><td><details><summary>答え</summary>

&lt;&lt; trueでした！

maxItemは10で、nowItemは9なので

maxItem - nowItem ⇒ 10 - 9 ⇒ 1

で、1 は 9 以上ではないので、

```c#
maxItem - nowItem > 9
```

は `false`（偽）である

だが、コインの数（230）は０以上なので

```c#
coin > 0
```

も `true`（真）である

「または」( || ) の場合は、<span style="text-decoration: underline;">いずれか</span>真だったら、すべて真になるため、この比較は「true」である。

</details></td></tr></tbody></table>