Plit00's Story

[Websec.fr] Hard - level23 본문

Wargame/websec.fr

[Websec.fr] Hard - level23

plit00 2019. 4. 12. 14:15

 체스나 알파고를 예제로 든다고 한다.

일단 소스를 보자. 

<?php

class Maze {
    private $directions = array(array(0, 1), array(0, -1), array(1, 0), array(-1, 0));
    private $max_x;
    private $max_y;
    private $maze = array();
    private $player_position;
    
    public function __construct($x, $y) {
        $this->max_x = $x;
        $this->max_y = $y;
        $this->max_x += ($this->max_x % 2) + 1;
        $this->max_y += ($this->max_y % 2) + 1;
        
        $this->maze = array_fill(0, $this->max_x, array_fill(0, $this->max_y, '█'));
        
        $this->player_position = array('x' => 1, 'y' => 1);
        
        self::generate();
    }
    
    private function generate() {
        // $this->maze[$this->max_x-2][$this->max_y-1] = 'E';                   # There is no exit! Muhahahah
        $cells = array(array(1, 1));
        while($cells) {
            list($x, $y) = array_pop($cells);  # I arbitrary choose the most recent cell, feel free to pick another one
            shuffle($this->directions);
            foreach($this->directions as $direction){  # pick a random direction
                list($nx, $ny) = array($x + 2*$direction[0], $y + 2*$direction[1]); # pick the neighbor in the chosen direction
                if(0 < $nx && $nx < ($this->max_x-1) && 0 < $ny && $ny < ($this->max_y-1)) { # is this neighbor in the maze ?
                    if($this->maze[$nx][$ny] !== ' ') {  # is this neighbor already visited ?
                        $cells[] = array($nx, $ny);  # add it to the waitlist
                        $this->maze[$x + $direction[0]][$y + $direction[1]] = $this->maze[$nx][$ny] = ' '; # clear the way to it
                    }
                }
            }
        }
        $this->maze[$this->player_position['y']][$this->player_position['x']] = '🚹'; 
        # player starting position
    }
    
    
    public function valid_direction($direction) { 
        return in_array($direction, $this->directions);
    }
    
    public function is_wall($direction) { 
        if(self::valid_direction($direction)) {
            return $this->maze[$this->player_position['y'] + $direction[1]][$this->player_position['x'] + $direction[0]] === '█';
        }
        return true;
    }
    
    public function move($direction) { 
        if(self::valid_direction($direction)) {
            if(!self::is_wall($direction)) {
                $this->maze[$this->player_position['y']][$this->player_position['x']] = ' '; 
                $this->player_position['x'] = $this->player_position['x'] + $direction[0];
                $this->player_position['y'] = $this->player_position['y'] + $direction[1];
                if($this->maze[$this->player_position['y']][$this->player_position['x']] === 'E') {
                    flag();
                } else {
                    $this->maze[$this->player_position['y']][$this->player_position['x']] = '🚹';
                }
            }
        }
    }
    
    public function print_maze() {
        print '<pre class="line_height_one">';
        foreach($this->maze as $line) {
            print join('', $line) . "\n";
        }
        print '</pre>';
    }
}

 

이 소스에 대해서 해석을 하자면 

'Wargame > websec.fr' 카테고리의 다른 글

[Websec.fr] Easy - Level24  (0) 2019.08.16
[Websec.fr] Hard - Level14  (0) 2019.08.16
[Websec.fr] medium - level3  (0) 2019.04.08
[Websec.fr] medium - Level5  (0) 2019.04.08
[Websec.fr] Easy - Level22  (0) 2019.04.08
Comments