Objekt verschwindet

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • BrainBug
    antwortet
    cool.

    Das sind immer die besten Threads, in denen der Fragesteller selber die Lösung findet

    Einen Kommentar schreiben:


  • TBT
    antwortet
    Lösung

    gefunden

    es muß oben
    PHP-Code:
        function free_field($_x,$_y){
            echo 
    defined('DEBUG')?"Chessboard->free_field($_x,$_y);<br>":"";
            unset(
    $this->board[$_x][$_y]);
            
    $this->board[$_x][$_y]=0;
        } 
    heißen, dann brauch man auch kein Würgaround.

    Ist auch logisch, da $this->board[$_x][$_y] ja eine Referenz
    auf King ist, ich diesen also kille.
    Zuerst muß mit unset() die Referenz aufgelöst werden, und dann
    kann man das Feld 0 setzen.

    Einen Kommentar schreiben:


  • TBT
    antwortet
    Würgaround

    so geht's, obwohl es eigentlich Blödsinn ist

    PHP-Code:
    function move_to($_x,$_y){
        echo 
    defined('DEBUG')?"Figure->move_to($_x,$_y);<br>":"";
        if(
    $this->can_move($_x,$_y)){
            
    // Würgaround! make a copy of $this!
            
    $tmp $this;
            
    $this->chessboard->free_field($this->lx=$this->x,$this->ly=$this->y); 
            
    // Würgaround! set $this to the copy of $this !
            
    $this $tmp;
            
    $this->chessboard->set_field($this,$this->x=$_x,$this->y=$_y);
            return 
    true;
        }
        return 
    false;

    PS: das obige Script brauch zum berechnen von 4 Zügen (inklusive Prüfung auf "Schach") auf einem XP1800 4 ms

    Einen Kommentar schreiben:


  • TBT
    antwortet
    ich übergebe der Funktion eine Referenz, damit er beim Funktionsaufruf keine Kopie macht

    bei der Zuweisung ist das wieder extra, hier eine Referenz damit er bei der Zuweisung keine Kopie macht

    ohne die Referenzen, würde er 2 Kopien machen
    PHP-Code:
    function set_field($_figure,$_x,$_y){ // 1. Kopie
        
    $this->board[$_x][$_y]=$_figure// 2. Kopie

    Einen Kommentar schreiben:


  • BrainBug
    antwortet
    aber du übergibst doch der Funktion schon eine Referenz, d.h. nach meinem Verständnis ist somit $_figure innerhalb der Funktion nur eine Referenz. Und davon nochmal eine Referenz?
    Und funktioniert das System anders? Soviel habe ich mich damit noch nicht beschäftigt.

    Einen Kommentar schreiben:


  • TBT
    antwortet
    Original geschrieben von BrainBug
    warum steht in der Funktion set_field()

    $this->board[$_x][$_y]=&$_figure;

    und nicht

    $this->board[$_x][$_y]=$_figure;

    ?
    damit er eine Referenz von dem Objekt übernimmt,
    und keine Kopie macht. Ich will ja mit der original
    Figur weiterarbeiten.

    Einen Kommentar schreiben:


  • BrainBug
    antwortet
    ups, sorry. Bin in der Funktion verrutscht. Da lag ja gar nicht das Problem

    Einen Kommentar schreiben:


  • BrainBug
    antwortet
    warum steht in der Funktion set_field()

    $this->board[$_x][$_y]=&$_figure;

    und nicht

    $this->board[$_x][$_y]=$_figure;

    ?

    Einen Kommentar schreiben:


  • TBT
    antwortet
    ich bin inzwischen soweit,
    das er nicht das Chessboard killt,
    sondern den $this auf 0 setzt ???

    PHP-Code:
    function move_to($_x,$_y){
        echo 
    defined('DEBUG')?"Figure->move_to($_x,$_y);<br>":"";
        if(
    $this->can_move($_x,$_y)){
            
    var_dump($this);    // $this ist ein Object vom Type Figure
            // $this->chessboard ist ein Object vom Type Chessboard
            
    $this->chessboard->free_field($this->lx=$this->x,$this->ly=$this->y); 
            
    // jetzt ist $this->chessboard NULL ???   WIESO ???
            
    var_dump($this);    // $this ist ein int(0) ???
            
    $this->chessboard->set_field($this,$this->x=$_x,$this->y=$_y);
            return 
    true;
        }
        return 
    false;

    Einen Kommentar schreiben:


  • TBT
    hat ein Thema erstellt Objekt verschwindet.

    Objekt verschwindet

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

    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;
            
    $this->$_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-== $this->|| $_x == $this->|| $_x+== $this->)
                &&     ( 
    $_y-== $this->|| $_y == $this->|| $_y+== $this->
                &&    !
    $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>"
    Zuletzt geändert von TBT; 13.08.2003, 22:38.
Lädt...
X