<?php
$data = [
['#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'],
['#','O','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'],
['#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'],
['#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'],
['#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#'],
];
$row = 1;
$col = 1;
$rows = count($data) - 1;
$cols = count($data[0]) - 1;
show();
while(1){
$dir = fgets(STDIN);
move($dir);
// var_dump($data);
show();
}
function move($dir){
$dir = trim($dir);
switch($dir){
case 's':
$next_row = $GLOBALS['row'] + 1 > $GLOBALS['rows'] ?$GLOBALS['rows']:$GLOBALS['row'] + 1;
$next_col = $GLOBALS['col'];
break;
case 'd':
$next_row = $GLOBALS['row'];
$next_col = $GLOBALS['col'] + 1 > $GLOBALS['cols']?$GLOBALS['cols']:$GLOBALS['col']+1;
break;
case 'w':
$next_row = $GLOBALS['row'] - 1<0?0:$GLOBALS['row'] - 1;
$next_col = $GLOBALS['col'];
break;
case 'a':
$next_row = $GLOBALS['row'];
$next_col = $GLOBALS['col'] - 1<0?0:$GLOBALS['col'] - 1;
break;
case 'q':
die("=======游戏结束,智商真低=======");
default:
echo 22;
return;
}
// 是否结束了
if ($next_row == $GLOBALS['rows'] && $next_col == $GLOBALS['cols'] - 1) {
die("=======游戏结束,智商真高=======");
}
if($GLOBALS['data'][$next_row][$next_col] == ' '){
$GLOBALS['data'][$next_row][$next_col] = 'O';
$GLOBALS['data'][$GLOBALS['row']][$GLOBALS['col']] = ' ';
$GLOBALS['col'] = $next_col;
$GLOBALS['row'] = $next_row;
}
}
function show(){
// 清屏
echo chr(27)."[H".chr(27)."[2J";
echo "======迷宫游戏======\n";
foreach($GLOBALS['data'] as $val){
echo join(',',$val).PHP_EOL;
}
}
12-20
09-15
1299