Skip to content

Instantly share code, notes, and snippets.

@vace
Created August 26, 2022 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vace/1706fc6b39a94338fb218badb2cd8f38 to your computer and use it in GitHub Desktop.
Save vace/1706fc6b39a94338fb218badb2cd8f38 to your computer and use it in GitHub Desktop.
Mid Bo cake 中秋博饼
<?php
/**
* 中秋博饼
* MidAutumn::random();
*/
class MidAutumn {
public static function random () {
return (new MidAutumn())->run();
}
public $dices = [0, 0, 0, 0, 0, 0];
public function run () {
foreach ($this->dices as $key => $value) {
$this->dices[$key] = mt_rand(1, 6);
}
return $this->parse($this->dices);
}
// 验证
public function parse ($dices) {
$level = MidAutumn::getLevel($dices);
return array_merge($level, ['dices' => $dices]);
}
public static function getLevel ($dices) {
asort($dices);
$counter = array_count_values($dices);
foreach (static::$levels as $level) {
if ($level['match'] === 'hit') {
if (implode('', $dices) === implode('', $level['dices'])) {
return $level;
}
} else if ($level['match'] === 'count') {
foreach ($level['dices'] as $dice => $count) {
if (isset($counter[$dice]) && $counter[$dice] === $count) {
return $level;
}
}
} else {
return $level;
}
}
return $level;
}
private static $levels = [
[
// 种类序号
'type' => 1,
// 主称号
'name' => '状元',
// 副称号
'alias' => '金花',
'point' => 50,
'match' => 'hit',
'dices' => [1, 1, 4, 4, 4, 4]
],
[
'type' => 2,
'name' => '状元',
'alias' => '六杯红',
'point' => 30,
'match' => 'hit',
'dices' => [4, 4, 4, 4, 4, 4]
],
[
'type' => 3,
'name' => '状元',
'alias' => '遍地锦',
'point' => 30,
'match' => 'hit',
'dices' => [1, 1, 1, 1, 1, 1]
],
[
'type' => 4,
'name' => '状元',
'alias' => '六杯黑',
'point' => 30,
'match' => 'count',
'dices' => [ 6 => 6, 5 => 6, 3 => 6, 2 => 6 ]
],
[
'type' => 5,
'name' => '状元',
'alias' => '五红',
'point' => 20,
'match' => 'count',
'dices' => [ 4 => 5 ]
],
[
'type' => 6,
'name' => '状元',
'alias' => '五子登科',
'point' => 20,
'match' => 'count',
'dices' => [ 1 => 5, 2 => 5, 3 => 5, 5 => 5, 6 => 5 ]
],
[
'type' => 7,
'name' => '状元',
'alias' => '四点红',
'point' => 20,
'match' => 'count',
'dices' => [ 4 => 4 ]
],
[
'type' => 8,
'name' => '榜眼',
'alias' => '对堂',
'point' => 10,
'match' => 'hit',
'dices' => [1, 2, 3, 4, 5, 6]
],
[
'type' => 9,
'name' => '探花',
'alias' => '三红',
'point' => 5,
'match' => 'count',
'dices' => [ 4 => 3 ]
],
[
'type' => 10,
'name' => '进士',
'alias' => '四进',
'point' => 3,
'match' => 'count',
'dices' => [ 1 => 4, 2 => 4, 3 => 4, 5 => 4, 6 => 4 ]
],
[
'type' => 11,
'name' => '举人',
'alias' => '二举',
'point' => 2,
'match' => 'count',
'dices' => [ 4 => 2 ]
],
[
'type' => 12,
'name' => '秀才',
'alias' => '一秀',
'point' => 1,
'match' => 'count',
'dices' => [ 4 => 1 ]
],
[
'type' => 99,
'name' => '罚黑',
'alias' => '罚黑',
'point' => 0,
'match' => 'none',
'dices' => []
]
];
}
print_r(MidAutumn::random());
print_r(MidAutumn::random());
print_r(MidAutumn::random());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment