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
- Made 5 classes: Game, Board, Box, Piece and Queen.
- Created and instance of the game.
- Created a new Board object with the method createBoard.
- 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] =>
)
)
)
)