$r = array(
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
array('.','.','.','*','.','*','.','.','.','*','.','*','.','.','.','.','*','.','*','.'),
);
foreach ($r as $value)
{
$value = rand(0,19);
if ($value<=19)
{
$r[$value]++;
}
}
print_r($r);
我也尝试过mt_rand,但这并没有帮助,但它根本没有随机。还有一种方法可以简化此代码,而不是每次都编写数组。我也可以使用感谢
以盒子形式制作它答案 0 :(得分:0)
我不确定这是否是您正在寻找的东西,但这里的东西输出类似于您的阵列。
<?php
// Config
$rows = 20;
$cols = 20;
$mines = 100;
// Error checking
if ( ($rows * $cols) <= $mines ) {
die("Error: There are too many mines to fit on this minefield!");
}
// How many spaces are not occupied by mines?
$not_mines = ($rows * $cols) - $mines;
// Build a string like "....***", with length corresponding to rows/cols/mines.
$mine_string = str_repeat('.', $not_mines);
$mine_string.= str_repeat('*', $mines);
// Randomize the string.
$mine_string = str_shuffle($mine_string);
// Display the string in rows/column format
for ( $y = 0; $y < $rows; $y++ ) {
for ( $x = 0; $x < $cols; $x++ ) {
// the position of the string to take a character from
$pos = ($y * $cols) + $x;
// get one character from the string
echo substr($mine_string, $pos, 1);
}
// End of a row, break to new column
echo '<br>';
}
输出(每次不同):
....*..**......*.*.*
.*.........*.***...*
.*..**......**.*....
.......*.*....***...
**....**...**.......
.......*..**.**.....
*.*....*.....*.*...*
....**.....**...*.*.
.*...*....*.*..*..*.
..*......**.*....**.
*..*...*...........*
.*..*...*........*..
.....*..........*..*
.***..*..***........
...*.....***........
***...........*..*..
.*...*..*.....*.*...
....*.......*.*...*.
.............**.....
...*........**...*..
答案 1 :(得分:0)
$size = 20;
$count_mines = 10;
$one_dimensial_map = array_fill(0, $count_mines, '*') + array_fill(0, $size * $size, '.');
shuffle($one_dimensial_map);
$two_dimensial_map = array_chunk($one_dimensial_map, $size);
print_r(array_map('json_encode', $two_dimensial_map)); // just for output
答案 2 :(得分:0)
function generateRandomString($length = 20) {
$characters = '.....*...***..***.*.*';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
for ($i = 0; $i <20; $i++)
{
echo generateRandomString();
echo "<br>";
}
这是否得到了必要的答案。感谢