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

时间:2015-12-15 13:06:07

标签: php arrays oop object

我正在建造一个国际象棋游戏,随机将7个皇后牌放在棋盘上而不会相互击打。该板由7行7列构成。

我已经知道如何将女王放入阵列中。但现在我正在尝试绘制棋盘(使用类游戏中的方法showBoard)和其中“box”数组的值。因此,当数组发生变化时,它应该反映在棋盘上。下面是代码和数组。有人知道如何做到这一点?

索引

<?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() {

    }
}

Box class

<?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;
    }
}

Piece class

<?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
        )

0 个答案:

没有答案