Created
October 15, 2023 07:31
-
-
Save katsube/760d159238b032289767f1d52af18e09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//--------------------------------------------------- | |
// 定数 | |
//--------------------------------------------------- | |
// 配列のインデックスを定義 | |
define('MONSTER_ID', 0); // モンスターID | |
define('MONSTER_NAME', 1); // モンスター名 | |
define('MONSTER_LEVEL',2); // モンスターレベル | |
define('MONSTER_AREA', 3); // 出現エリア | |
//--------------------------------------------------- | |
// グローバル変数 | |
//--------------------------------------------------- | |
// モンスターのリスト | |
$MONSTER_LIST = [ | |
// id, name, level, area | |
[1001, 'スライム', 1, '草原'], | |
[1002, 'ドラキー', 3, '草原'], | |
[1003, 'おおきづち', 5, '砂漠'] | |
]; | |
//--------------------------------------------------- | |
// メイン処理 | |
//--------------------------------------------------- | |
// 草原に出現するモンスターを取得する | |
$monsters = getAreaMonster('草原'); | |
// ★ここを回答する★ | |
// このコメントは削除してください | |
/** | |
* 指定されたエリアに出現するモンスターを返却する | |
* | |
* @param string $area エリア名('草原', '砂漠'...) | |
* @return array 該当するモンスターの配列 | |
*/ | |
function getAreaMonster($area){ | |
global $MONSTER_LIST; // グローバル変数を関数内で使うための記述 | |
$result = [ ]; // 結果を格納する配列 | |
// $monster配列から指定されたエリアのモンスターを抽出する | |
for($i=0; $i<count($MONSTER_LIST); $i++){ | |
$monster_area = $MONSTER_LIST[$i][MONSTER_AREA]; | |
// エリアが一致したら結果用配列に追加する | |
if($monster_area === $area){ | |
$result[ ] = $MONSTER_LIST[$i]; | |
} | |
} | |
return($result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment