如何在PHP中使用OOP将数组的值放入表中

如何在PHP中使用OOP将数组的值放入表中,php,arrays,oop,object,Php,Arrays,Oop,Object,我正在建立一个国际象棋游戏,随机将7个皇后放置在棋盘上,而不相互碰撞。电路板由7行7列组成 我已经知道如何将女王放置在阵阵中。但是现在我尝试绘制棋盘(在类游戏中使用showBoard方法)和其中“box”数组的值。因此,当数组改变时,它应该反映在棋盘上。下面是代码和数组。有人知道怎么做吗 索引 我理解正确了吗:当有人用Box::setPiece()更改棋盘时,您想调用Game::showBoard()?没错。我还包括了索引页。为什么box是自己的类?将此方法作为类板的一部分或扩展它。然后在函数中

我正在建立一个国际象棋游戏,随机将7个皇后放置在棋盘上,而不相互碰撞。电路板由7行7列组成

我已经知道如何将女王放置在阵阵中。但是现在我尝试绘制棋盘(在类游戏中使用showBoard方法)和其中“box”数组的值。因此,当数组改变时,它应该反映在棋盘上。下面是代码和数组。有人知道怎么做吗

索引


我理解正确了吗:当有人用Box::setPiece()更改棋盘时,您想调用Game::showBoard()?没错。我还包括了索引页。为什么box是自己的类?将此方法作为类板的一部分或扩展它。然后在函数中调用::showBoard()。如何访问要在showBoard()方法中使用的数组?可以使用。但是,如果您更改棋盘的唯一位置是Game::setPiece(),那么您可以在那里简单地调用$this->showBoard()。我是否正确理解您的意思:当有人使用Box::setPiece()更改棋盘时,您想调用Game::showBoard()?这是正确的。我还包括了索引页。为什么box是自己的类?将此方法作为类板的一部分或扩展它。然后在函数中调用::showBoard()。如何访问要在showBoard()方法中使用的数组?可以使用。但是,如果您更改棋盘的唯一位置是Game::setPiece(),那么您只需在那里调用$this->showBoard()。
<?php

require_once '../core/init.php';

/* Init $game */
$game = new Game();

/* Init board */
$game->setBoard(7);

/* Set piece */
$game->setPiece();

/* Show chessboard (including real-time status of the boxes) */
echo $game->showBoard();

/* Debug tool */
echo "<pre>";
print_r($game);
echo "</pre>";
<?php

/**
 * This class will handle:
 *  1) Setting up the board.
 *  2) Set a piece.
 *  3) Connecting to the Board class
 *  4) Show the chessboard (including real-time status of the boxes).
 */
class Game {
    private $_board;
    private $_length;
    private $_queen;

    /**
     * This method 1) will:
     *  1) Initialize the board based on the length.
     *  2) Set the property $_length
     * @param $length
     */
    public function setBoard($length) {
        // Action 1)
        $this->_board = new Board($length);
        // Action 2)
        $this->_length = $length;
    }

    /**
     * This method 2) will:
     *  1) Pick a random box with the method setRandomColRow() from the class Board.
     *  2) Create a new property $box and store the random row and column in it.
     *  3) Check if the random box is empty.
     *  4) Create a new Queen object.
     *  5) Place the Queen in the random box.
     */
    public function setPiece() {
        // Action 1)
        $this->getBoard()->setRandomColRow();
        // Action 2)
        $box = $this->getBoard()->getBox($this->getBoard()->randomRow,$this- >getBoard()->randomCol);
        // Action 3)
        if($box == !null) {
            // Action 4)
            $this->_queen = new Queen();
            // Action 5)
            $this->getBoard()->getBox($this->getBoard()->randomRow,$this->getBoard()->randomCol)->setPiece($this->_queen);
        }
     }

    /**
     * This method 3) will:
     *  1) Return access to the board properties and methods.
     * @return mixed
     */
    public function getBoard() {
        // Action 1)
        return $this->_board;
    }

    /**
     * This method 4) will:
     *  1) Create a table with rows and place it in the string $table_str.
     *  2) Create rows and columns basses on the length property and place it in the string $table_str.
     *  3) TODO: Place the value of box->piece in the table box.
     *  4) Return the string
     * @return string
     */
    public function showBoard() {
        // Action 1)
        $table_str = '<table border="1px">';
        $table_str .= '<tr>';
            // Action 2)
            for($row=1; $row <= $this->_length; $row++) {
                for($col=1; $col <= $this->_length; $col++) {
                    $table_str .= '<td height=100px width=100px bgcolor=#FFFFFF></td>';
                }
                $table_str .= '</tr>';
            }
            // Action 4)
            return $table_str;
    }
}
?>
<?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 {

    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($length) {
        // Action 1)
        $this->_length = $length;
        for($row=0; $row < $length; $row++){
            for($col=0; $col< $length; $col++){
                $this->_row = $row;
                $this->_col = $col;
                // Action 2
                $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() {
        // Action 1)
        return $this->_row;
    }

    /**
     * This method 3) will:
     *  1) Return the $_col property.
     * @return int
     */
    public function getCol() {
        // Action 1)
        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(123456);
        // Action 1)
        $this->randomRow = mt_rand(1, $this->_length);
        // Action 2)
        $this->randomCol = mt_rand(1, $this->_length);
        // Action 3)
        $this->MarkRelatedHorizontalBoxes();
        // Action 4)
        $this->MarkRelatedVerticalBoxes();
    }

    /**
     * This method 6) will: TODO: check if this method is doing the write things and where it is used
     *  1) Set the random column to property $col
     *  2) Set the random column to property $row
     *  3) Return the random box
     * @return mixed
     */
    public function relatedBoxes() {
        $col = $this->randomCol;
        $row = $this->randomRow;
        return $this->getBox($row,$col);
    }

    /**
     * This method 7) 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
     *  4) TODO: Check if the box is empty
     */
    public function MarkRelatedHorizontalBoxes() {
        // Action 1)
        for($row = 0; $row < $this->_length; $row++) {
            $horizontalCells[$row+1] = $row+1;
        }
        // Action 2)
        foreach ($horizontalCells as $horizontalCel) {
            // Action 3)
            $this->getBox($horizontalCel,$this->randomCol)->setPiece("X");
        }
    }

    /**
     * This method 8) 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() {
        // Action 1)
        for($col = 0; $col < $this->_length; $col++) {
            $verticalBoxes[$col+1] = $col+1;
        }
        // Action 2)
        foreach ($verticalBoxes as $verticalBox) {
            // Action 3)
            $this->getBox($this->randomRow, $verticalBox)->setPiece("X");
        }
    }

    /**
     * TODO: 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
     *  4) Check if the box is empty
     * @return array
     */
    public function relatedDiagonalBoxes() {

    }
}
<?php

/**
 * This class will handle;
 *  1) Setting the $_piece property
 */
class Box {

    private $_piece;

    /**
     * This method 1) will:
     *  1) Set the $_piece property equal to the input value
     * @param $piece
     */
    public function setPiece($piece) {
        $this->_piece = $piece;
    }
}
<?php

/**
 * This class will handle;
 */
class Piece {

}
<?php

/**
 * This class will handle;
 *  1) Extending the piece class, and therefore inherit all methods and properties.
 */
class Queen extends Piece {

}
Game Object
(
    [_board:Game:private] => Board Object
        (
            [randomRow] => 5
            [randomCol] => 4
            [_box:Board:private] => Array
                (
                    [1] => Array
                        (
                            [1] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [2] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [3] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [4] => Box Object
                                (
                                    [_piece:Box:private] => X
                                )

                            [5] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [6] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [7] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                        )

                    [2] => Array
                        (
                            [1] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [2] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [3] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [4] => Box Object
                                (
                                    [_piece:Box:private] => X
                                )

                            [5] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [6] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [7] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                        )

                    [3] => Array
                        (
                            [1] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [2] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [3] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [4] => Box Object
                                (
                                    [_piece:Box:private] => X
                                )

                            [5] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [6] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [7] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                        )

                    [4] => Array
                        (
                            [1] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [2] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [3] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [4] => Box Object
                                (
                                    [_piece:Box:private] => X
                                )

                            [5] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [6] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [7] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                        )

                    [5] => Array
                        (
                            [1] => Box Object
                                (
                                    [_piece:Box:private] => X
                                )

                            [2] => Box Object
                                (
                                    [_piece:Box:private] => X
                                )

                            [3] => Box Object
                                (
                                    [_piece:Box:private] => X
                                )

                            [4] => Box Object
                                (
                                    [_piece:Box:private] => Queen Object
                                        (
                                        )

                                )

                            [5] => Box Object
                                (
                                    [_piece:Box:private] => X
                                )

                            [6] => Box Object
                                (
                                    [_piece:Box:private] => X
                                )

                            [7] => Box Object
                                (
                                    [_piece:Box:private] => X
                                )

                        )

                    [6] => Array
                        (
                            [1] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [2] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [3] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [4] => Box Object
                                (
                                    [_piece:Box:private] => X
                                )

                            [5] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [6] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [7] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                        )

                    [7] => Array
                        (
                            [1] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [2] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [3] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [4] => Box Object
                                (
                                    [_piece:Box:private] => X
                                )

                            [5] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [6] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                            [7] => Box Object
                                (
                                    [_piece:Box:private] => 
                                )

                        )

                )

            [_length:Board:private] => 7
            [_row:Board:private] => 6
            [_col:Board:private] => 6
        )