php 迷宫游戏

<?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;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值