dtg25862 2015-12-08 18:49
浏览 44

如何在PHP中使用OOP更改多维数组中对象的值

I'm building a chess game that randomly places 7 Queens on the board without hitting each other. The board is constructed of 7 rows and 7 columns. So far I made the following steps

  1. Made 5 classes: Game, Board, Box, Piece and Queen.
  2. Created and instance of the game.
  3. Created a new Board object with the method createBoard.
  4. Created a showBoard method to display the board.

Now I want to place the first queen in a random box on the board, but I don't know how to access the object of a multidimensional array. The set_piece method should access the board object and place a Q in a random box and then mark the box as occupied = true. Anyone an idea how the set_piece method in the Game class should be constructed?

Game class:

class Game {
    var $board;
    var $length;

    public function createBoard($length) {
        $this->board = new Board($length);
        $this->length = $length;
    }

    public function showBoard() {
        // Create table
        $table_str = '<table border="1px">';
        for($row=1; $row <= $this->length; $row++) {
            $table_str .= "<tr>";
            for($col=1; $col <= $this->length; $col++) {
                $total = $row + $col;
                if($total%2==0) {
                    $table_str .= "<td height=100px width=100px bgcolor=#FFFFFF></td>";
                } else {
                    $table_str .= "<td height=60px width=60px bgcolor=#000000></td>";
                }
            }
            $table_str .= "</tr>";
        }
        return $table_str;
    }
    public function set_piece() {

    }
}

Board class:

class Board {
    public $box;
    public function __construct($length) {
        for($i=0; $i < $length; $i++){
            for($j=0; $j< $length; $j++){
               $this->box[$i+1][$j+1] = new Box($i+1, $j+1);
            }
        }
    }
}

Box class:

class Box {
    public $piece;
    public $occupied = false;
}

Piece class:

class Piece {

}

Queen class:

class Queen extends Piece {

}

Index:

$game = new Game();

$game->createBoard(7);
$game->set_piece();

echo $game->showBoard();

echo "<pre>";
print_r($game->board);
echo "</pre>";

The array I'm trying to access looks like this:

Board Object
(
    [box] => Array
        (
            [1] => Array
                (
                    [1] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [2] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [3] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [4] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [5] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [6] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [7] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                )

             [2] => Array
                (
                    [1] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [2] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [3] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [4] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [5] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [6] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [7] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                )

             [3] => Array
                 (
                    [1] => Box Object
                         (
                            [piece] => 
                            [occupied] => 
                        )

                    [2] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [3] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [4] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [5] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [6] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [7] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                )

            [4] => Array
                (
                    [1] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [2] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [3] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [4] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [5] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [6] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [7] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                )

            [5] => Array
                (
                    [1] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [2] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [3] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [4] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [5] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [6] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [7] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                 )

             [6] => Array
                (
                    [1] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [2] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [3] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [4] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [5] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [6] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [7] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                )

            [7] => Array
                (
                    [1] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [2] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [3] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [4] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [5] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [6] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                    [7] => Box Object
                        (
                            [piece] => 
                            [occupied] => 
                        )

                )

        )

)
  • 写回答

4条回答 默认 最新

  • dpl3350 2015-12-08 19:02
    关注

    As you already have an almost perfect OO-Layout, try to reduce the problem: Instead of accessing a multi-dimensional array, try to add getter's and setter's to explicitly address a certain level.

    One approach:

    $Game->getBoard()->getBox($row, $col)->getPiece()->set($queen);
    

    Where the more complex function would be the getBox($row, $col):

    class Board {
        public $box;
        public function __construct($length) {
            for($i=0; $i < $length; $i++){
                for($j=0; $j< $length; $j++){
                   $this->box[$i+1][$j+1] = new Box($i+1, $j+1);
                }
            }
        }
    
        public function getBox($row, $col) {
            return $this->box[$row][$col];  // should ave some eception handling here!
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)