Ergebnis 1 bis 6 von 6

Thema: Conways Spiel des Lebens in PHP und OOP

Baum-Darstellung

Vorheriger Beitrag Vorheriger Beitrag   Nächster Beitrag Nächster Beitrag
  1. #1

    Conways Spiel des Lebens in PHP und OOP

    Hallo Leute, ich bastel grad an einer Version von Conways Spiel des Lebens in PHP mit OOP.

    Leider habe ich da irgendwo Probleme. ;(

    Hier mal mein Quellcode.

    PHP-Code:
    <?
    session_start();

    class field
    {
        var $row = 3;
        var $col = 3;

        var $alive = '<img src="alive.png" alt="alive" width="30" height="30" border="1" />';
        var $dead = '<img src="dead.png" alt="dead" width="30" height="30" border="1" />';
        
        var $cells = array();
        
        function init_gen()
        {        
            for($i = 0; $i <= $this->row*$this->col-1; $i++)
            {
                $this->cells[$i] = 0;
            }
            $this->cells[0] = 1;
            $this->cells[2] = 1;
            $this->cells[4] = 1;
            
            $_SESSION['get_cells'] = implode(',', $this->cells);
        }
        
        function next_gen()
        {
            $this->cells = explode(',', $_SESSION['get_cells']);
            
            $pitch = $this->row;
            $alive_neighbours = 0;
            $dead_neighbours = 0;
                
            for($i = 0; $i <= $this->row*$this->col-1; $i++)
            {
                if(0 == $this->cells[$i])
                {
                    # if weiß Nachbarn = 3 = 1*grün Geboren
                    if(1 == $this->cells[$i-1] AND 1 == $this->cells[$i+1] AND 1 == $this->cells[$i-$pitch] OR 1 == $this->cells[$i-1] AND 1 == $this->cells[$i+1] AND 1 == $this->cells[$i+$pitch] OR 1 == $this->cells[$i+$pitch] AND 1 == $this->cells[$i+1] AND 1 == $this->cells[$i-$pitch] OR 1 == $this->cells[$i+$pitch] AND 1 == $this->cells[$i-1] AND 1 == $this->cells[$i-$pitch])
                    {
                        $dead_neighbours++;
                    }
                    if(3 == $dead_neighbours)
                    {
                        $this->cells[$i] = 1;
                    }
                }
                elseif(1 == $this->cells[$i])
                {
                    # if grün Nachbarn = 2 OR 3 Überlebt
                    if(1 == $this->cells[$i-1] AND 1 == $this->cells[$i+1] AND 1 == $this->cells[$i-$pitch] OR 1 == $this->cells[$i-1] AND 1 == $this->cells[$i+1] AND 1 == $this->cells[$i+$pitch] OR 1 == $this->cells[$i+$pitch] AND 1 == $this->cells[$i+1] AND 1 == $this->cells[$i-$pitch] OR 1 == $this->cells[$i+$pitch] AND 1 == $this->cells[$i-1] AND 1 == $this->cells[$i-$pitch])
                    {
                        $this->cells[$i] = 1;
                    }
                    elseif(1 == $this->cells[$i-1] AND 1 == $this->cells[$i+1] OR 1 == $this->cells[$i-$pitch] AND 1 == $this->cells[$i+$pitch] OR 1 == $this->cells[$i-1] AND 1 == $this->cells[$i+$pitch] OR 1 == $this->cells[$i+1] AND 1 == $this->cells[$i+$pitch] OR 1 == $this->cells[$i-$pitch] AND 1 == $this->cells[$i-1] OR 1 == $this->cells[$i-$pitch] AND 1 == $this->cells[$i+1])
                    {
                        $this->cells[$i] = 1;
                    }
                    
                    # if grün Nachbarn < 2 Stirbt OR grün Nachbarn >= 5 Stirb
                    if(1 == $this->cells[$i-1] OR 1 == $this->cells[$i+1] OR 1 == $this->cells[$i-$pitch] OR 1 == $this->cells[$i+$pitch])
                    {
                        $alive_eighbours++;

                        if(1 == $alive_neighbours OR 4 < $alive_neighbours)
                        {
                            $this->cells[$i] = 0;
                        }
                    }
                }
            }
            
            $_SESSION['get_cells'] = implode(',', $this->cells);
        }
        
        function show()
        {
            $this->cells = explode(',', $_SESSION['get_cells']);
            
            $block_row = $this->row*30 + $this->row*2;
            $block_col = $this->col*30 + $this->col*2;
            
            echo "\n".'<div>'."\n".'<p>'."\n".'<a href="?next-gen=go">Next-Gen</a> - <a href="gol.php">Re Start</a>'."\n".'</p>'."\n";
            
            echo '<p style="border: 1px solid black; width: '.$block_row.'px; height: '.$block_col.'px; margin: 0; padding: 0;">'."\n";
            
            foreach($this->cells as $value)
            {
                if(1 == $value)
                {
                    echo $this->alive;
                }
                else
                {
                    echo $this->dead;
                }
            }

            echo "\n".'</p>'."\n".'</div>';
        }
    }

    $gol = new field();

    if(!'go' == $_GET['next-gen'])
    {
        $gol->init_gen();
    }
    else
    {
        $gol->next_gen();
    }

    $gol->show();

    ?>
    Ich hoffe er ist für euch Verständlich. Mein derzeitiges Problem liegt in der Funktion next_gen(), den es wird keine Neue Generation erstellt. ;( Und ich weiß nicht mehr weiter mir raucht schon den ganzen Tag der Kopf.

    Stell ich vielleicht die Falschen IF fragen in der next_gen() Function?

    Geändert von deadshox (07.06.2008 um 18:14 Uhr)

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •