<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); require_once "Maze.php"; $n = new Maze(); if (isset($_GET['num'])) { switch ($_GET['num']) { case 2: include 'maze1.php'; break; case 3: include 'maze3.php'; break; case 4: include 'maze4.php'; break; } } else { include 'maze2.php'; } ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Maze</title> <meta name="description" content="Solve the maze with PHP">
if ($this->validate($block, $this->x, $this->y) == false) { $loop = false; } } } /** * * A function for display Maze * */ public function display() { foreach ($this->maze as $block => $maze) { $this->create($block); shuffle($this->direction); $this->setPosition(); } foreach ($this->maze as $block => $maze) { echo 'Maze ' . ($block + 1) . '<br/>'; foreach ($maze as $m) { foreach ($m as $n) { echo '<span class="cell">' . $n . '</span>'; } echo '<br>'; } echo '<br>'; } } } $maze = new Maze(15); $maze->display();
function testShootLaser() { $maze = new Maze(3, 3); $maze->setStart(1, 2, 'S'); # Test no mirror $results = $maze->shootLaser(); $this->assertEquals(2, $results[0]); $this->assertEquals(1, $results[1]); $this->assertEquals(0, $results[2]); # Test 1 mirror $maze->setMirror(1, 1, '\\'); $results = $maze->shootLaser(); $this->assertEquals(2, $results[0]); $this->assertEquals(2, $results[1]); $this->assertEquals(1, $results[2]); # Test cycle $maze->setStart(1, 2, 'E'); $maze->setMirror(2, 2, '\\'); $maze->setMirror(2, 0, '/'); $maze->setMirror(0, 0, '\\'); $maze->setMirror(0, 2, '/'); $results = $maze->shootLaser(); $this->assertEquals(7, $results[0]); $this->assertEquals(-1, $results[1]); $this->assertEquals(-1, $results[2]); }
<?php error_reporting(E_ALL ^ E_NOTICE); require 'Maze.php'; require 'AStar.php'; $maze = new Maze(); $aStar = new AStar(); $w = $_POST['w'] ? $_POST['w'] : 6; // 迷宫宽 $h = $_POST['h'] ? $_POST['h'] : 6; // 迷宫高 $maze->set($w, $h); $maze->create(); $grids = $maze->get(); // 寻路 $aStar->set($w, $h, $grids); $path = $aStar->search(0, $w * $h - 1); // 从零开始找到35 // 画迷宫方法 function div($x, $y, $v) { global $w, $h, $path; //if ( $y > 4 ) $v = 0; $k = $y * $w + $x; //if ( array_key_exists($k, $this->_enable) ) { //echo "<div class=\"grid cell_enable_{$this->_enable[$k]}\" style=\""; //} else { echo "<div class=\"grid cell_{$v}\" style=\""; //} echo 'top:' . 34 * $y . 'px;'; echo 'left:' . 34 * $x . 'px;';
if (count($argv) > 2) { $input_file = $argv[1]; $output_file = $argv[2]; } else { # No input file or output file provided print "Usage: php maze ./path/to/input/file ./path/to/output/file\n"; exit; } $input = fopen($input_file, 'r'); while ($line = fgets($input)) { $coordinates = explode(' ', trim($line)); $x = $coordinates[0]; $y = $coordinates[1]; # Grid dimensions if (count($coordinates) == 2) { $maze = new Maze($x, $y); # Player coordinates } elseif ($coordinates[2] == 'S') { $maze->setStart($x, $y, $coordinates[2]); # Mirror } elseif ($coordinates[2] == '/' || $coordinates[2] == '\\') { $maze->setMirror($x, $y, $coordinates[2]); # Invalid } else { print "Usage: php maze ./path/to/input/file ./path/to/output/file\n"; fclose($input); exit; } } # SHOOT! $result = $maze->shootLaser();
<?php /* 深さ優先探索で迷路を解く 問題はS地点からG地点にたどり着くかどうか検証せよ 但し、0は進め、Xは進めないとする。 たどりついた場合はtrueを失敗した場合はfalseを返せ */ require_once './part/Maze.php'; $maze = new Maze('../file/maze.csv'); //S地点を引数にセットする var_dump($maze->search(0, 2));