Example #1
0
    public function process($input)
    {
        $id = 0;
        $minesAll  = array();
        while(count($input)>0)
        {           
            $line = array_shift($input);    
            
            if($this->isNewField($line))
            {
                $dim = $this->getFieldDimensions($line);
                $m = $dim[0]; 
                $n = $dim[1]; 
                if(!$this->IsTheEnd($m, $n))
                {
                    if(!$this->isValidDim($m, $n)){
                        throw new Exception ("Error: Invalid field dimensions! $m, $n");
                    }
                    ++$id;
                    $lineNum = 0;
                    $field = new Field($id, $m, $n);
                    $field->setDebug($this->debug);
                    $this->fields[$id] = $field;  
                }             
            }
            else
            {                                
                if( count($this->fields)<=0 ){
                    throw new Exception ("Error something wrong\n");
                }
                $field = $this->getField($id);
                $id = $field->getFieldId();

                /* line data process */
                foreach(str_split($line) as $x=>$cellvalue)
                {
                    $field->addCell($x, $lineNum);
                    /* detecting mines */
                    if($this->isMinedCell($cellvalue)){
                       $minesAll[$id][] = array($x, $lineNum);
                    }
                }
                $this->fields[$id] = $field;
                $lineNum++;
            }
        } 
        
        if(count($minesAll)>0)
        {
            foreach($minesAll as $id=>$mines)
            {
                $field = $this->getField($id); 
                $field = $this->MineField($field, $mines);
            }
            
        }
        
        return $this->getOutput();
    }