Ergebnis 1 bis 6 von 6

Thema: Conways Spiel des Lebens in PHP und OOP

  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)

  2. #2
    Du solltest eventuell die ORs gruppieren, zudem könnte es ein Problem sein, dass du
    PHP-Code:
    $alive_neighbours 0;
    $dead_neighbours 0
    nicht in jedem for-Schleifen Durchgang machst. (Hab das Game of Life auch gerade letztens in C++ programmiert.)

  3. #3
    Kann sein, dass ich mich irre, aber wäre es nicht am einfachsten, die umliegenden Felder zusammenzurechnen? Die Summe müsste dann doch die Anzahl der lebenden Nachbarn sein, oder?

    Im übrigen machst du einen Regelverstoß: Du berechnest die nächste Generation nicht parallel. cells[i][gen+1] beeinflusst die Berechnung von cells[i+1][gen], was nicht der Fall sein darf. (ich hoffe, das war verständlich)

    Außerdem... sehe ich es richtig, dass du mit einem begrenzten Feld von 3*3 arbeitest? O_O

    Und ich würde mit PHP ein Bild generieren (mit den image* Funktionen), aber das ist natürlich Ansichtssache. ^^

  4. #4
    Dank der Hilfe von dead_orc hab ich es nun hin bekommen.


    Hier mal mein Code:

    PHP-Code:
    <?php

    session_start
    ();

    class 
    field
    {
        var 
    $row 9# Hier können die Zeilen bestimmt werden;
        
    var $col 9# Hier können die Spalten bestimmt werden;

        
    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();
        
        var 
    $_SESSION = array();
        
        var 
    $gameover 0;
        
        function 
    init_gen() # Hier wird das Spielfeld Initialisiert;
        
    {
            
    $_SESSION['count_gen'] = 0;
            
            for(
    $y 0$y <= $this->col-1$y++)
            {
                for(
    $x 0$x <= $this->row-1$x++)
                {
                    
    $this->cells[$x][$y] = 0;
                }
            }
            
            
    $randx rand(0,$this->row-1);
            
    $randy rand(0,$this->col-1);
            
            
    $this->cells[$randx][$randy] = 1;
            
    $this->cells[$randx][$randy+1] = 1;
            
    $this->cells[$randx][$randy-1] = 1;
            
    $this->cells[$randx+1][$randy] = 1;
            
    $this->cells[$randx-1][$randy] = 1;
            
            for(
    $y 0$y <= $this->col-1$y++)
            {
                for(
    $x 0$x <= $this->row-1$x++)
                {
                    
    $_SESSION['cells'][$x][$y] = $this->cells[$x][$y];
                }
            }
        }
        
        function 
    next_gen() # Berechnung der nächsten Generation;
        
    {
            
    $_SESSION['count_gen']++;
            
            for(
    $y 0$y <= $this->col-1$y++)
            {
                for(
    $x 0$x <= $this->row-1$x++)
                {
                    
    $this->cells[$x][$y] = $_SESSION['cells'][$x][$y];
                    
    $cells_new[$x][$y] = $this->cells[$x][$y];
                }
            }
            
            
    $cells_new $this->cells;
                
            for(
    $y 0$y <= $this->col-1$y++)
            {
                for(
    $x 0$x <= $this->row-1$x++)
                {
                    if(
    == $this->cells[$x][$y])
                    {
                        
    $dead_neighbours 0;
                        
                        
    # if weiß Nachbarn = 3 = 1*grün Geboren
                        
    if(== $this->cells[$x-1][$y])
                        {
                            
    $dead_neighbours++;
                        }
                        
                        if(
    == $this->cells[$x+1][$y])
                        {
                            
    $dead_neighbours++;
                        }
                        
                        if(
    == $this->cells[$x][$y-1])
                        {
                            
    $dead_neighbours++;
                        }
                        
                        if(
    == $this->cells[$x][$y+1])
                        {
                            
    $dead_neighbours++;
                        }
                        
                        if(
    == $this->cells[$x-1][$y-1])
                        {
                            
    $dead_neighbours++;
                        }
                        
                        if(
    == $this->cells[$x+1][$y+1])
                        {
                            
    $dead_neighbours++;
                        }
                        
                        if(
    == $this->cells[$x+1][$y-1])
                        {
                            
    $dead_neighbours++;
                        }
                        
                        if(
    == $this->cells[$x-1][$y+1])
                        {
                            
    $dead_neighbours++;
                        }
                        
                        if(
    == $dead_neighbours)
                        {
                            
    $cells_new[$x][$y] = 1;
                        }
                    }
                    elseif(
    == $this->cells[$x][$y])
                    {
                        
    $alive_neighbours 0;
                        
                        if(
    == $this->cells[$x-1][$y])
                        {
                            
    $alive_neighbours++;
                        }
                        
                        if(
    == $this->cells[$x+1][$y])
                        {
                            
    $alive_neighbours++;
                        }
                        
                        if(
    == $this->cells[$x][$y-1])
                        {
                            
    $alive_neighbours++;
                        }
                        
                        if(
    == $this->cells[$x][$y+1])
                        {
                            
    $alive_neighbours++;
                        }
                        
                        if(
    == $this->cells[$x-1][$y-1])
                        {
                            
    $alive_neighbours++;
                        }
                        
                        if(
    == $this->cells[$x+1][$y+1])
                        {
                            
    $alive_neighbours++;
                        }
                        
                        if(
    == $this->cells[$x+1][$y-1])
                        {
                            
    $alive_neighbours++;
                        }
                        
                        if(
    == $this->cells[$x-1][$y+1])
                        {
                            
    $alive_neighbours++;
                        }
                        
                        
                        
    # if grün Nachbarn < 2 Stirbt OR grün Nachbarn >= 5 Stirb
                        
    if(<= $alive_neighbours OR <= $alive_neighbours)
                        {
                            
    $cells_new[$x][$y] = 0;
                        }
                        
                        
    # if grün Nachbarn = 2 OR 3 Überlebt
                        
    if(== $alive_neighbours OR == $alive_neighbours)
                        {
                            
    $cells_new[$x][$y] = 1;
                        }
                    }
                }
            }
            
            for(
    $y 0$y <= $this->col-1$y++)
            {
                for(
    $x 0$x <= $this->row-1$x++)
                {
                    
    $_SESSION['cells'][$x][$y] = $cells_new[$x][$y];
                }
            }
        }
        
        function 
    show() # Anzeige des Spielfeldes + Lebenden und Toten Zellen;
        
    {
            for(
    $y 0$y <= $this->col-1$y++)
            {
                for(
    $x 0$x <= $this->row-1$x++)
                {
                    
    $this->cells[$x][$y] = $_SESSION['cells'][$x][$y];
                }
            }
            
            
    $block_row $this->row*30 $this->row*2;
            
    $block_col $this->col*30 $this->col*2;
            
            echo 
    "\n".'<div>'."\n".'<p>'."\n".'<a href="gol.php">Re Start</a> - <a href="?next-gen=go">Next-Gen</a> - Generation: '.$_SESSION['count_gen']."\n".'</p>'."\n";
            
            echo 
    '<p style="border: 1px solid black; width: '.$block_row.'px; height: '.$block_col.'px; margin: 0; padding: 0;">'."\n";
            
            for(
    $y 0$y <= $this->col-1$y++)
            {
                for(
    $x 0$x <= $this->row-1$x++)
                {
                    
                    if(
    == $this->cells[$x][$y])
                    {
                        echo 
    $this->alive;
                    }
                    elseif(
    == $this->cells[$x][$y])
                    {
                        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();

    ?>

  5. #5
    IMO machst du das mit den Nachbarn immer noch viiel zu kompliziert. Wenn 0 eine tote Zelle und 1 eine lebendige Zelle ist, könntest du es auch einfach so machen:
    PHP-Code:
    $alive_neighbours $cells[$x-1][$y-1] + $cells[$x][$y-1] + $cells[$x+1][$y-1] + $cells[$x-1][$y] + $cells[$x+1][$y] + $cells[$x-1][$y+1] + $cells[$x][$y+1] + $cells[$x+1][$y+1];
    $dead_neighbours $alive_neighbours

  6. #6
    Ach du plöde Q. Hast ja recht. Tue mich halt nochn bissel Schwer mit der Optimierung.

    Danke!

Berechtigungen

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