所有可能的集合(3 位数字)的排列,包括零



我正在创建一个小游戏程序,其中有两种类型的游戏。SingleDouble. 对于每个游戏,都有一个固定的图表集。

单个图表

'128',   '129',  '120',  '130',  '140',   '123',  '124',  '125',  '126',  '127',
'137',   '138',  '139',  '149',  '159',   '150',  '160',  '134',  '135',  '136',
'146',   '147',  '148',  '158',  '168',   '169',  '179',  '170',  '180',  '145',
'236',   '156',  '157',  '167',  '230',   '178',  '250',  '189',  '234',  '190',
'245',   '237',  '238',  '239',  '249',   '240',  '269',  '260',  '270',  '235',
'290',   '246',  '247',  '248',  '258',   '259',  '278',  '279',  '289',  '280',
'380',   '345',  '256',  '257',  '267',   '268',  '340',  '350',  '360',  '370',
'470',   '390',  '346',  '347',  '348',   '358',  '359',  '369',  '379',  '389',
'489',   '480',  '490',  '356',  '357',   '349',  '368',  '378',  '450',  '460',
'579',   '570',  '580',  '590',  '456',   '367',  '458',  '459',  '469',  '479',
'560',   '589',  '670',  '680',  '690',   '457',  '467',  '468',  '478',  '569',
'678',   '679',  '689',  '789',  '780',   '790',  '890',  '567',  '568',  '578',

双图表

'100',   '110',  '166',  '112',  '113',   '114',  '115',  '116',  '117',  '118',
'119',   '200',  '229',  '220',  '122',   '277',  '133',  '224',  '144',  '226',
'155',   '228',  '300',  '266',  '177',   '330',  '188',  '233',  '199',  '244',
'227',   '255',  '337',  '338',  '339',   '448',  '223',  '288',  '225',  '299',
'335',   '336',  '355',  '400',  '366',   '466',  '377',  '440',  '388',  '334',
'344',   '499',  '445',  '446',  '447',   '556',  '449',  '477',  '559',  '488',
'399',   '660',  '599',  '455',  '500',   '600',  '557',  '558',  '577',  '550',
'588',   '688',  '779',  '699',  '799',   '880',  '566',  '800',  '667',  '668',
'669',   '778',  '788',  '770',  '889',   '899',  '700',  '990',  '900',  '677',

当我选择一个Single并输入数字0123456789(any sequence(sorted & unsorted), min 4 digits & max 10 digits from 0-9, repeated)时,它返回与Single Chart相关的整个集合。 对于这种情况,它返回120集合。

如果我选择double并输入相同的数字0123456789它将返回仅与Double Chart相关的所有集合。

目前,我正在为游戏使用一种解决方案Single该解决方案在此处给出排列-所有可能的数字集,但在某些集合中,它像001一样返回,但必须100

我也找不到Double游戏的解决方案。 我尝试什么:

public function insertSingleGameData($motorGameData)
{
$chartvalidation    = config('chartValidation');
$tempMotorSet       = str_split($motorGameData->Playgame->cane);
$motorSet           = $this->arrayCombination(3, $tempMotorSet); // Get All Sets
$motorRange         = array_unique($motorSet);
foreach($motorRange as $cane)
{
if(in_array($cane, $chartvalidation['single'])){
$tempGameData[]     =   ([
'playgame_id'   =>  $motorGameData->Playgame->id,
'user_id'       =>  $motorGameData->Playgame->user_id,
'cane'          =>  $cane,
'amount'        =>  $motorGameData->Playgame->amount,
'gamemaster_id' =>  $motorGameData->Playgame->gamemaster_id,
"created_at"    =>  CarbonCarbon::now(),  
"updated_at"    =>  CarbonCarbon::now(),  
]);
}
}
Tempgame::insert($tempGameData);
}
function arrayCombination($le, $set){
$lk = $this->combination_number($le, count($set));
$ret = array_fill(0, $lk, array_fill(0, $le, '') );
$temp = array();
for ($i = 0 ; $i < $le ; $i++)
$temp[$i] = $i;
$ret[0] = $temp;
for ($i = 1 ; $i < $lk ; $i++){
if ($temp[$le-1] != count($set)-1){
$temp[$le-1]++;
} else {
$od = -1;
for ($j = $le-2 ; $j >= 0 ; $j--)
if ($temp[$j]+1 != $temp[$j+1]){
$od = $j;
break;
}
if ($od == -1){
break;
}
$temp[$od]++;
for ($j = $od+1 ; $j < $le ; $j++)    {
$temp[$j] = $temp[$od]+$j-$od;
}
}
$ret[$i] = $temp;
}
for ($i = 0 ; $i < $lk ; $i++) {
for ($j = 0 ; $j < $le ; $j++){
$ret[$i][$j] = $set[$ret[$i][$j]];   
}
}
$tempSet = array();
foreach ($ret as $key => $value) {
$tempSet[] = implode('',  $value);
}
return $tempSet;
//print("<pre>".print_r($ret,true)."</pre>");
}
function combination_number($k,$n){
$n = intval($n);
$k = intval($k);
if ($k > $n){
return 0;
} elseif ($n == $k) {
return 1;
} else {
if ($k >= $n - $k){
$l = $k+1;
for ($i = $l+1 ; $i <= $n ; $i++)
$l *= $i;
$m = 1;
for ($i = 2 ; $i <= $n-$k ; $i++)
$m *= $i;
} else {
$l = ($n-$k) + 1;
for ($i = $l+1 ; $i <= $n ; $i++)
$l *= $i;
$m = 1;
for ($i = 2 ; $i <= $k ; $i++)
$m *= $i;            
}
}
return $l/$m;
}

如何在一个函数中同时实现SingleDouble游戏?

您可以通过创建正则表达式对数字进行preg_match,例如:

/^[235046]+$/

这意味着我们试图匹配一个字符串,该字符串从开始(^插入符号(到结束($美元符号(都有数字235046(意思,235等等(。如果我们找到匹配项,我们将它们收集在另一个数组中。

片段:

<?php
$digits = '235046';
$single_chart_filtered = [];
foreach($single_chart as $chart_value){
if(preg_match("/^[$digits]+$/",$chart_value) === 1){
$single_chart_filtered[] = $chart_value;
}
}
print_r($single_chart_filtered);
$double_chart_filtered = [];
foreach($double_chart as $chart_value){
if(preg_match("/^[$digits]+$/",$chart_value) === 1){
$double_chart_filtered[] = $chart_value;
}
}

演示:https://3v4l.org/jChvm

001 是0123456789的有效组合。您需要筛选数组以查找可能的集合:

// simple artificial setup
$single = [111,222,333,444,555];
function generatePossibleSet($input) { return [000,001,010,100,011,101,110,111]; }
$possibleSet = generatePossibleSet("01");
$set = $single;
// just interscet the picked set with the possible set
$set = array_intersect($set, $possibleSet);

此示例将给出 [111] - 集合列表中唯一有效的 0 和 1 组合。

最新更新