Created
November 19, 2023 11:25
-
-
Save katsube/eb281ea74f902c062d65aa66eb705038 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 | |
/** | |
* パズルの盤面を生成するクラス | |
* | |
*/ | |
class PazzleBoard{ | |
//----------------------------------- | |
// プロパティ | |
//----------------------------------- | |
private $width; // 幅 | |
private $height; // 高さ | |
private $pattern = 3; // パターン数 | |
/** | |
* コンストラクタ | |
*/ | |
function __construct($width, $height, $pattern=null){ | |
// ★ここを回答する★ | |
// この行と、上の行は削除してください。 | |
} | |
/** | |
* パズル盤面を生成する | |
*/ | |
function generate(){ | |
$width = $this->width; // 幅 | |
$height = $this->height; // 高さ | |
$pattern = $this->pattern; // パターン数 | |
$result = [ ]; // 結果を入れる配列 | |
// ランダムな2次元配列を生成する | |
for( $i=0; $i<$height; $i++ ){ | |
$result[$i] = [ ]; | |
for( $j=0; $j<$width; $j++ ){ | |
$result[$i][$j] = rand(0, $pattern); // 0〜パターン数のランダムな値を入れる | |
} | |
} | |
return($result); | |
} | |
} | |
//------------------------------------------------- | |
// 10x10のパズル盤面を生成する | |
//------------------------------------------------- | |
$pazzle = new PazzleBoard(10, 10); // コンストラクタに幅と高さを渡す | |
$result = $pazzle->generate(); // 生成する | |
// 結果を表示する | |
for( $i=0; $i<count($result); $i++ ){ | |
for( $j=0; $j<count($result[$i]); $j++ ){ | |
switch($result[$i][$j]){ | |
case 0: | |
echo '🚣♀'; | |
break; | |
case 1: | |
echo '🐟'; | |
break; | |
case 2: | |
echo '🐬'; | |
break; | |
case 3: | |
echo '🐳'; | |
break; | |
} | |
} | |
echo "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment