예제 #1
0
 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]);
 }
예제 #2
0
파일: maze.php 프로젝트: EthanWright/PHP
        $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();
    # Check if they got stuck in a loop and write to output file
    $output = fopen($output_file, 'w');
    if ($result[1] == -1) {
        fwrite($output, "{$result[0]}\r\nStuck in a loop");
    } else {
        fwrite($output, "{$result[0]}\r\n{$result[1]} {$result[2]}");
    }
    fclose($output);
    exit;
}