1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| <?php
// sac de numéros à tirer
$bag = array_fill_keys(range(1, 90), 0);
// grille résultat
$grid = [
1 => [],
2 => [],
3 => []
];
// lignes complètes : 5 numéros
$full_rows = [];
// colonnes pleines : 2 numéros
$full_cols = [];
$placed = 0;
while ($placed < 15) {
// on tire un numéro et on le retire du sac
$num = array_rand($bag, 1);
unset($bag[$num]);
if ($num < 10) {
$col = 1;
} elseif ($num < 90) {
$col = substr($num, 0, 1) + 1;
} else {
$col = 9;
}
if (in_array($col, $full_cols, true)) {
continue;
}
$rows = array();
foreach(array_diff(array(1, 2, 3), $full_rows) as $v) {
if (empty($grid[$v][$col])) {
$rows[] = $v;
}
}
if (empty($rows)) {
continue;
}
$row = $rows[0];
$grid[$row][$col] = $num;
++$placed;
if (count($grid[$row]) === 5) {
$full_rows[] = $row;
}
$nb = (int)isset($grid[1][$col]) + (int)isset($grid[2][$col]) + (int)isset($grid[3][$col]);
if ($nb === 2) {
$full_cols[] = $col;
}
}
echo '<table border="1">';
foreach($grid as $row => $nums) {
echo '<tr>';
for($i = 1; $i <= 9; ++$i) {
echo '<td width="20" align="center">', ((isset($nums[$i])) ? $nums[$i] : ' ') , '</td>';
}
echo '</tr>';
}
echo '</table>'; |