Last active
September 22, 2019 06:08
-
-
Save stmkza/41812e30b93bb1eb4202bd48ebce5098 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
const BOMB = -1; | |
const BLANK = 0; | |
function main() | |
{ | |
const width = 9; | |
const height = 9; | |
const bombCount = 10; | |
if(width * height < bombCount) { | |
console.log('爆弾の数がマス目の数よりも多いです'); | |
process.exit(1); | |
} | |
map = generateMap(width, height, bombCount); | |
map = setAdjacentBombCount(map, width, height); | |
console.log(mapToDiscordString(map)); | |
} | |
function generateMap(width, height, bombCount) | |
{ | |
const map = []; | |
for(let y = 0; y < height; y++) { | |
const row = []; | |
for(let x = 0; x < width; x++) { | |
row[x] = BLANK; | |
} | |
map[y] = row; | |
} | |
for(let i = 0; i < bombCount; i++) { | |
while(true) { | |
const posX = Math.floor(Math.random() * width); | |
const posY = Math.floor(Math.random() * height); | |
if(map[posY][posX] !== BOMB) { | |
map[posY][posX] = BOMB; | |
break; | |
} | |
} | |
} | |
return map; | |
} | |
function setAdjacentBombCount(map, width, height) | |
{ | |
for(let y = 0; y < height; y++) { | |
for(let x = 0; x < width; x++) { | |
if(map[y][x] === BOMB) { | |
continue; | |
} | |
const isLeftLimit = x === 0; | |
const isRightLimit = x === width - 1; | |
const isTopLimit = y === 0; | |
const isBottomLimit = y === height - 1; | |
if(!isLeftLimit && map[y][x - 1] === BOMB) { | |
map[y][x] += 1; | |
} | |
if(!isRightLimit && map[y][x + 1] === BOMB) { | |
map[y][x] += 1; | |
} | |
if(!isTopLimit && map[y - 1][x] === BOMB) { | |
map[y][x] += 1; | |
} | |
if(!isBottomLimit && map[y + 1][x] === BOMB) { | |
map[y][x] += 1; | |
} | |
if(!isTopLimit && !isLeftLimit && map[y - 1][x - 1] === BOMB) { | |
map[y][x] += 1; | |
} | |
if(!isTopLimit && !isRightLimit && map[y - 1][x + 1] === BOMB) { | |
map[y][x] += 1; | |
} | |
if(!isBottomLimit && !isLeftLimit && map[y + 1][x - 1] === BOMB) { | |
map[y][x] += 1; | |
} | |
if(!isBottomLimit && !isRightLimit && map[y + 1][x + 1] === BOMB) { | |
map[y][x] += 1; | |
} | |
} | |
} | |
return map; | |
} | |
function mapToDiscordString(map) | |
{ | |
const numbers = [':zero:', ':one:', ':two:', ':three:', ':four:', ':five:', ':six:', ':seven:', ':eight:']; | |
const bomb = ':bomb:'; | |
const hideSymbol = '||'; | |
let result = ''; | |
map.forEach(row => { | |
row.forEach(cell => { | |
if(cell === BOMB) { | |
result += `${hideSymbol}${bomb}${hideSymbol}`; | |
} else { | |
result += `${hideSymbol}${numbers[cell]}${hideSymbol}`; | |
} | |
}); | |
result += "\n"; | |
}); | |
return result; | |
} | |
main(); |
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 | |
define('BOMB', -1); | |
define('BLANK', 0); | |
function main() | |
{ | |
$width = 9; | |
$height = 9; | |
$bombCount = 10; | |
if($width * $height < $bombCount) | |
die('爆弾の数がマス目の数よりも多いです'); | |
$map = generateMap($width, $height, $bombCount); | |
$map = setAdjacentBombCount($map, $width, $height); | |
echo mapToDiscordString($map); | |
} | |
function generateMap($width, $height, $bombCount) | |
{ | |
$map = []; | |
for($y = 0; $y < $height; $y++) { | |
$row = []; | |
for($x = 0; $x < $width; $x++) { | |
$row[$x] = BLANK; | |
} | |
$map[$y] = $row; | |
} | |
for($i = 0; $i < $bombCount; $i++) { | |
while(true) { | |
$posX = mt_rand(0, $width - 1); | |
$posY = mt_rand(0, $height - 1); | |
if($map[$posY][$posX] !== BOMB) { | |
$map[$posY][$posX] = BOMB; | |
break; | |
} | |
} | |
} | |
return $map; | |
} | |
function setAdjacentBombCount($map, $width, $height) | |
{ | |
for($y = 0; $y < $height; $y++) { | |
for($x = 0; $x < $width; $x++) { | |
if($map[$y][$x] === BOMB) { | |
continue; | |
} | |
$isLeftLimit = $x === 0; | |
$isRightLimit = $x === $width - 1; | |
$isTopLimit = $y === 0; | |
$isBottomLimit = $y === $height - 1; | |
if(!$isLeftLimit && $map[$y][$x - 1] === BOMB) { | |
$map[$y][$x] += 1; | |
} | |
if(!$isRightLimit && $map[$y][$x + 1] === BOMB) { | |
$map[$y][$x] += 1; | |
} | |
if(!$isTopLimit && $map[$y - 1][$x] === BOMB) { | |
$map[$y][$x] += 1; | |
} | |
if(!$isBottomLimit && $map[$y + 1][$x] === BOMB) { | |
$map[$y][$x] += 1; | |
} | |
if(!$isTopLimit && !$isLeftLimit && $map[$y - 1][$x - 1] === BOMB) { | |
$map[$y][$x] += 1; | |
} | |
if(!$isTopLimit && !$isRightLimit && $map[$y - 1][$x + 1] === BOMB) { | |
$map[$y][$x] += 1; | |
} | |
if(!$isBottomLimit && !$isLeftLimit && $map[$y + 1][$x - 1] === BOMB) { | |
$map[$y][$x] += 1; | |
} | |
if(!$isBottomLimit && !$isRightLimit && $map[$y + 1][$x + 1] === BOMB) { | |
$map[$y][$x] += 1; | |
} | |
} | |
} | |
return $map; | |
} | |
function mapToDiscordString($map) | |
{ | |
$numbers = [':zero:', ':one:', ':two:', ':three:', ':four:', ':five:', ':six:', ':seven:', ':eight:']; | |
$bomb = ':bomb:'; | |
$hideSymbol = '||'; | |
$result = ''; | |
foreach($map as $row) { | |
foreach($row as $cell) { | |
if($cell === BOMB) { | |
$result .= sprintf('%s%s%s', $hideSymbol, $bomb, $hideSymbol); | |
} else { | |
$result .= sprintf('%s%s%s', $hideSymbol, $numbers[$cell], $hideSymbol); | |
} | |
} | |
$result .= "\n"; | |
} | |
return $result; | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment