Ich habe den mal hier abgeklemmt http://www.php-resource.de/forum/sho...threadid=24145
ich hatte heute etwas lange Weile *g*
aber ein Problem gibt es,
warum verschwindet in der Funktion
Figure->move_to($_x,$_y)
mein Objekt Chessboard ???
ich hatte heute etwas lange Weile *g*
aber ein Problem gibt es,
warum verschwindet in der Funktion
Figure->move_to($_x,$_y)
mein Objekt Chessboard ???
PHP-Code:
class Chessboard{
var $board;
function Chessboard(){
echo "Chessboard->Chessboard();<br>";
for($i=1;$i<9;++$i){
for($ii=1;$ii<9;++$ii){
$this->board[$i][$ii]=0;
}
}
}
function new_game(){
echo "Chessboard->new_game();<br>";
$this->board[1][1] = &new Tower($this,'white',1,1);
$this->board[1][2] = &new Knight($this,'white',1,2);
// ...
$this->board[1][4] = &new King($this,'white',1,4);
// ...
$this->board[1][8] = &new Tower($this,'white',1,8);
$this->board[1][7] = &new Knight($this,'white',1,7);
// ...
$this->board[8][1] = &new Tower($this,'black',8,1);
$this->board[8][2] = &new Knight($this,'black',8,2);
// ...
$this->board[8][4] = &new King($this,'black',8,4);
// ...
$this->board[8][8] = &new Tower($this,'black',8,8);
$this->board[8][7] = &new Knight($this,'black',8,7);
}
function is_free($_x,$_y){
echo "Chessboard->is_free($_x,$_y);<br>";
return !is_object($this->board[$_x][$_y]);
}
function is_color($_color,$_x,$_y){
echo "Chessboard->is_color($_color,$_x,$_y);<br>";
if(!is_object($this->board[$_x][$_y]))
return false;
return $this->board[$_x][$_y]->get_color()==$_color;
}
function free_field($_x,$_y){
echo "Chessboard->free_field($_x,$_y);<br>";
$this->board[$_x][$_y]=0; // dies ist die Problemzeile ???
}
function set_field(&$_figure,$_x,$_y){
echo "Chessboard->set_field(&$_figure,$_x,$_y);<br>";
$this->board[$_x][$_y]=&$_figure;
}
function get_king($_color,&$_x,&$y){
echo "Chessboard->get_king($_color,&$_x,&$y);<br>";
for($_x=1;$_x<9;++$_x){
for($_y=1;$_y<9;++$_y){
if(get_class($this->board[$_x][$_y])=='king')
return;
}
}
}
function is_chess($_color,$_x,$_y){
echo "Chessboard->is_chess($_color,$_x,$_y);<br>";
for($i=1;$i<9;++$i){
for($ii=1;$ii<9;++$ii){
if( is_object($this->board[$i][$ii])
&&
$this->board[$i][$ii]->get_color()!=$_color
&&
$this->board[$i][$ii]->can_move($_x,$_y)){
return true;
}
}
}
return false;
}
function move($_x1,$_y1,$_x2,$_y2){
echo "Chessboard->move($_x1,$_y1,$_x2,$_y2);<br>";
return $this->board[$_x1][$_y1]->move_to($_x2,$_y2);
}
}
// abstract figure
class Figure{
var $chessboard; // object of Chessboard
var $x,$y; // 1-8 , 1-8 => 1:1 = upper left corner of white
var $color; // 'white', 'black'
function Figur(&$_board,$_color,$_x,$_y){
echo defined('DEBUG')?"Figure->Figur(&$_board,$_color,$_x,$_y);<br>":"";
$this->init($_board,$_color,$_x,$_y);
}
function move_to($_x,$_y){
echo defined('DEBUG')?"Figure->move_to($_x,$_y);<br>":"";
if($this->can_move($_x,$_y)){
// $this->chessboard ist ein Object vom Type Chessboard
$this->chessboard->free_field($this->x,$this->y);
// jetzt ist $this->chessboard NULL ??? WIESO ???
$this->x=$_x;
$this->y=$_y;
$this->chessboard->set_field($this,$_x,$_y);
return true;
}
return false;
}
function can_move($_x,$_y){
echo defined('DEBUG')?"Figure->can_move($_x,$_y);<br>":"";
die('virtual function \'Figure->can_move()\' called!');
}
function &get_color(){
echo defined('DEBUG')?"Figure->get_color();<br>":"";
return $this->color;
}
function init(&$_board,$_color,$_x,$_y){
echo defined('DEBUG')?"Figure->init(&$_board,$_color,$_x,$_y);<br>":"";
$this->x = $_x;
$this->y = $_y;
$this->color = $_color;
$this->chessboard = &$_board;
}
}
// figures
class King extends Figure{
function King(&$_board,$_color,$_x,$_y){
echo defined('DEBUG')?"King->King(&$_board,$_color,$_x,$_y);<br>":"";
$this->init($_board,$_color,$_x,$_y);
}
function can_move($_x,$_y){
echo defined('DEBUG')?"King->can_move($_x,$_y);<br>":"";
// logical of moves
if( ( $_x-1 == $this->x || $_x == $this->x || $_x+1 == $this->x )
&& ( $_y-1 == $this->y || $_y == $this->y || $_y+1 == $this->y )
&& !$this->chessboard->is_color($this->color,$_x,$_y) )
{
// controlling chess
// ... looking for a figure, that have an other color and can move to this place
// $this->chessboard->get_king($this->color,$x=0,$y=0); // we have the king already!
if(!$this->chessboard->is_chess($this->color,$_x,$_y))
return true;
}
return false;
}
}
class Knight extends Figure{
function Knight(&$_board,$_color,$_x,$_y){
echo defined('DEBUG')?"Knight->Knight(&$_board,$_color,$_x,$_y);<br>":"";
$this->init($_board,$_color,$_x,$_y);
}
function can_move($_x,$_y){
echo defined('DEBUG')?"Knight->can_move($_x,$_y);<br>":"";
// logical of moves
return false;
}
}
class Tower extends Figure{
function Tower(&$_board,$_color,$_x,$_y){
echo defined('DEBUG')?"Tower->Tower(&$_board,$_color,$_x,$_y);<br>":"";
$this->init($_board,$_color,$_x,$_y);
}
function can_move($_x,$_y){
echo defined('DEBUG')?"Tower->can_move($_x,$_y);<br>":"";
// logical of moves
return false;
}
}
// and so on ...
define('DEBUG',1);
// create Chessboard
$board = &new Chessboard();
// initialize new game
$board->new_game();
// make move
echo "<br>Weißer König von A4 auf B4 : ".($board->move(1,4,2,4)?"ok":"geht nicht")."<br><br>";
echo "<br>Schwarzer König von H4 auf G4 : ".($board->move(8,4,7,4)?"ok":"geht nicht")."<br><br>";
echo "<br>Weißer König von B4 auf C4 : ".($board->move(2,4,3,4)?"ok":"geht nicht")."<br><br>";
echo "<br>Schwarzer König von G4 auf A6 : ".($board->move(7,4,1,6)?"ok":"geht nicht")."<br><br>";
Kommentar