用PHP中的OOP动态标记对角框

问题描述:

我试图在对角框中绘制一个X.女王被随机放置。见下图。用PHP中的OOP动态标记对角框

enter image description here

正如你可以看到只有在(5,5)是正确绘制的最后X。在这个例子中,X应该绘制在方框(1,1),(2,2),(3,3),(4,4)和(5,5)中。由于女王被随机放置,方法markRelatedDiagonalBoxes应该动态工作。在Board_model下方。任何人都知道如何正确地书写markRelatedDiagonalBoxes,所以它会绘制如上所述的X.

Board_model.php

<?php 

/** 
* This class will handle: 
* 1) Constructing the boards boxes. 
* 2) Returning the property $_row. 
* 3) Returning the property $_col. 
* 4) Returning the property $_box. 
* 5) Setting random rows and columns. 
* 6) Returning all the related boxes. 
* 7) Returning the horizontal related boxes. 
* 8) Returning the vertical related boxes. 
* 9) Returning the diagonal related boxes. 
*/ 
class Board_model extends CI_Model { 

    public $randomRow; 
    public $randomCol; 

    private $_box; 
    private $_length; 
    private $_row; 
    private $_col; 

    /** 
    * This method 1) will: 
    * 1) Set the $_length property. 
    * 2) Create an array of new objects, Boxes based on the length of the rows and columns. 
    * @param int $length 
    */ 
    public function __construct() { 
     parent::__construct(); 
     $this->load->model('box'); 
     $this->_length = 7; 
     for($row=0; $row < $this->_length; $row++){ 
      for($col=0; $col< $this->_length; $col++){ 
       $this->_row = $row; 
       $this->_col = $col; 
       $this->_box[$row+1][$col+1] = new Box($row+1, $col+1); 
      } 
     } 
    } 


    /** 
    * This method 2) will: 
    * 1) Return the $_row property. 
    * @return int 
    */ 
    public function getRow() { 
     return $this->_row; 
    } 

    /** 
    * This method 3) will: 
    * 1) Return the $_col property. 
    * @return int 
    */ 
    public function getCol() { 
     return $this->_col; 
    } 

    /** 
    * This method 4) will: 
    * 1) Return the $_box property based on the specified row and column. 
    * @param $row 
    * @param $col 
    * @return mixed 
    */ 
    public function getBox($row, $col) { 
     return $this->_box[$row][$col]; 
    } 

    /** 
    * This method 5) will: 
    * 1) Randomly choose a number within the rang from 1 to the $_length property and place it in the property $randomRow. 
    * 2) Randomly choose a number within the rang from 1 to the $_length property and place it in the property $randomCol. 
    * 3) Execute the MarkRelatedHorizontalBoxes method. 
    * 4) Execute the MarkRelatedVerticalBoxes method. 
    * 5) TODO: Execute the MarkRelatedDiagonalBoxes method. 
    */ 
    public function setRandomColRow() { 
     // STATIC RANDOM TEST NUMBER 
     //mt_srand(1234534); 
     $this->randomRow = mt_rand(1, $this->_length);  
     $this->randomCol = mt_rand(1, $this->_length); 

     // Set piece 
     $this->getBox($this->randomRow,$this->randomCol)->setPiece("&#9813"); 

     $this->relatedDiagonalBoxes(); 
     return $this->_box;   
    } 

    function object_to_array($object) { 
     return (array) $object; 
    } 

    /** 
    * This method 7) will: 
    * 1) Create the related vertical array 
    * 2) Loop thew the array 
    * 3) Get the box position and place and X in the box as piece 
    * 4) TODO: Check if the box is empty 
    */ 
    public function markRelatedVerticalBoxes() { 
     for($row = 0; $row < $this->_length; $row++) {     
      $verticalBoxes[$row+1] = $row+1; 
      // Delete the random cel out of the array 
      unset($verticalBoxes[$this->randomRow]);     
     } 
     foreach ($verticalBoxes as $verticalBox) {    
      $this->getBox($verticalBox,$this->randomCol)->setPiece("X");     
     }    
    } 

    /** 
    * This method 8) will: 
    * 1) Create the related horizontal array 
    * 2) Loop thew the array 
    * 3) Get the box position and place and X in the box as piece 
    */ 
    public function markRelatedHorizontalBoxes() { 
     for($col = 0; $col < $this->_length; $col++) { 
      $horizontalBoxes[$col+1] = $col+1; 
      // Delete the random cel out of the array 
      unset($horizontalBoxes[$this->randomCol]); 
     } 
     foreach ($horizontalBoxes as $horizontalBox) { 
      $this->getBox($this->randomRow, $horizontalBox)->setPiece("X"); 
     } 
    } 

    public function markRelatedDiagonalBoxes() { 

     $row = $this->randomRow - 1; 
     $col = $this->randomCol - 1; 

     while($row > 0) { 
      while($col > 0) { 
       $this->getBox($row, $col)->setPiece("X"); 
       $col--; 
      } 
      $row--; 
     } 
    } 

    /** 
    * This method 9) will: 
    * 1) Create the diagonal array 
    * 2) Loop thew the array 
    * 3) Get the box position and place and X in the box as piece 
    * @return array 
    */ 
    public function relatedDiagonalBoxes() { 

     //$this->MarkRelatedHorizontalBoxes(); 

     //$this->MarkRelatedVerticalBoxes(); 

     $this->markRelatedDiagonalBoxes(); 
    } 
} 
+0

如果我移动$ row--在$ col--之后,现在系统进入无限循环。所以这不是解决方案。但是谢谢你花时间帮助我 – MikeTSF

你的逻辑遍历行,然后遍历列内。在移动到下一行之前,您将所有的部分都设置为完全缩小列。

如果您只想为每一行设置一个块,您需要在斜面中放置一块后再打开。

while($row > 0) { 
    while($col > 0) { 
     $this->getBox($row, $col)->setPiece('X'); 
     $col--; 
     break; // placing this break is the logic you are trying to achieve 
       // however defeats the purpose of the secondary while loop 
    } 
    $row--; 
} 

类似以下内容就足够了:

while($row > 0 && $col > 0) { 
    $this->getBox($row--, $col--)->setPiece('X'); 
} 

这就像你会做一起去行或列直线一样的,你只需要减小两个值,实现了坡。