Jump to content

[SOLVED] php array_pop() and array_push() passing an array but says im not


FaT3oYCG

Recommended Posts

Hi, im creating a little script for a card game that i know that will calculate and work out how long it will take someone to win at the game with a randomised deck of cards, i will eventually make a form and make it all submittable but untill then im using my database and an automatically randomised deck, here is my code below i dont know why im getting these errors but here they are

 

Warning:  array_pop() [function.array-pop]: The argument should be an array in H:xampphtdocsphpcmspagespairs.php on line 70

Warning:  array_push() [function.array-push]: First argument should be an array in H:xampphtdocsphpcmspagespairs.php on line 73

 

just those two errors repeatedley as it is from a function and is looped through many times

 

            <?php
                echo('<b><u>Pairs Game Calculator:</u></b><br><br>');
            ?>

            <div style="margin: 5px 20px 20px;">
                <div style="margin-bottom: 2px;"><b>Randomised Deck:</b> <input value="Hide" onclick="if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') {
                this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = 'Hide'; }else{ this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = 'Show'; }" type="button">
                </div>
                <div style="border: 1px inset ; margin: 0px; padding: 6px;">
                <div style="">
                    <?php
                        include_once('../includes/DBConnector.php');

                        $connector = new DBConnector();
                        $result = $connector->query("SELECT * FROM cards");

                        $cards = Array();
                        $card = 0;

                        while($row = $connector->GetArray($result))
                        {
                            $cards[$card][1] = $row['Card_ID'];
                            $cards[$card][2] = $row['Name'];
                            $cards[$card][3] = $row['Suit'];
                            $card = $card + 1;
                        }

                        echo('<table border="1" cellpadding="2"><tr><td>Card No:</td><td>ID No:</td><td>Card Name:</td><td>Card Suit:</td></tr>');
                        shuffle($cards);
                        for($i=0; $i<=51; $i++)
                        {
                            $num = $i + 1;
                            echo("<tr><td>" . $num . "</td><td>" . $cards[$i]['1'] . "</td><td>" . $cards[$i]['2'] . "</td><td>" . $cards[$i]['3'] . "</td></tr>");
                        }
                        echo('</table>');
                    ?>
                </div>
                </div>
            </div>

            <?php
                $table = Array();

                for($i=1;$i<=8;$i++)
                {
                    $table[$i] = Array();
                }

                function Place_Card($position = '')
                {
                    $card_placed = false;
                    $card = array_pop($cards);
                    if($position != '')
                    {
                        array_push($table[$position], $card);
                        return true;
                    }else{
                        for($i=1;$i<=8;$i++)
                        {
                            if(count($table[$i]) == 0)
                            {
                                array_push($table[$i], $card);
                                return true;
                            }elseif($i =={
                                return false;
                            }
                        }
                    }
                }

                function Check_Cards()
                {
                    $card_placed = false;
                    for($i=1;$i<=8;$i++)
                    {
                        for($j=1;$j<=8;$j++)
                        {
                            if($table[$i]['Name'] == $table[$j]['Name'])
                            {
                                Place_Card($i);
                                Place_Card($j);
                                $card_placed = true;
                            }
                            if($i == 8 && $j == 8 && $card_placed == false)
                            {
                                $place_card = Place_Card();
                                if($place_card == true)
                                {
                                    $card_placed = true;
                                }else{
                                    $lost = true;
                                }
                            }
                        }
                    }
                }

                function Pickup_Cards()
                {
                    $cards = array_merge_recursive($cards, $table[1], $table[2], $table[3], $table[4], $table[5], $table[6], $table[7], $table[8]);
                    $lost = false;
                }

                $lost = false;
                $won = false;
                $games_played = 0;
                $hands_played = 0;
                $losses = 0;

                while($won == false)
                {
                    if(count($cards) > 0)
                    {
                        $games_played = $games_played + 1;
                        if($lost == false)
                        {
                            Check_Cards();
                            $hands_played = $hands_played + 1;
                        }else{
                            Pickup_Cards();
                            $losses = $losses + 1;
                        }
                    }else{
                        $won = true;
                        echo('Game Won<br><br>' . 'Games Played: ' . $games_played . '<br>Hands Played: ' . $hands_played . '<br>Losses: ' . $losses);
                    }
                }
            ?>
        </div>

 

here is the page if you would like to visit it for reference

 

http://gaming-network.ath.cx/phpcms/index.php?page=6

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.