当前位置:网站首页>PHP maze game
PHP maze game
2022-07-03 13:43:00 【Gu Yue's blog】
<?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("======= Game over , IQ is really low =======");
default:
echo 22;
return;
}
// Is it over
if ($next_row == $GLOBALS['rows'] && $next_col == $GLOBALS['cols'] - 1) {
die("======= Game over , High IQ =======");
}
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(){
// Clear the screen
echo chr(27)."[H".chr(27)."[2J";
echo "====== Maze game ======\n";
foreach($GLOBALS['data'] as $val){
echo join(',',$val).PHP_EOL;
}
}
边栏推荐
猜你喜欢

(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)

又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了

Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases

mysql更新时条件为一查询

太阳底下无新事,元宇宙能否更上层楼?

常见的几种最优化方法Matlab原理和深度分析

Brief analysis of tensorboard visual processing cases

Flink code is written like this. It's strange that the window can be triggered (bad programming habits)

用户和组命令练习

TensorBoard可视化处理案例简析
随机推荐
TensorBoard可视化处理案例简析
又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
MySQL constraints
The solution of Chinese font garbled code in keil5
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
Stack application (balancer)
父亲和篮球
MySQL_ JDBC
Brief analysis of tensorboard visual processing cases
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
rxjs Observable filter Operator 的实现原理介绍
Libuv Library - Design Overview (Chinese version)
物联网毕设 --(STM32f407连接云平台检测数据)
Servlet
Unity Render Streaming通过Js与Unity自定义通讯
Students who do not understand the code can also send their own token, which is easy to learn BSC
Several common optimization methods matlab principle and depth analysis
This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
106. How to improve the readability of SAP ui5 application routing URL