SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot gregs's Avatar
    Join Date
    Aug 2002
    Posts
    161
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    A little ARRAY help please

    You can see this working at:
    http://wynnesports.com/tictactoe

    When I click an underscore _, it deletes all the previous clicks?
    Can someone help me with why it won't remember the $boards array?
    Do I somehow need to pass it when I click this?
    PHP Code:
    ECHO "<TT><a href=\"gamescreen.php?prow=$row&pcol=$col&choice=1&turn=$turn\">-</a>" "&nbsp;</TT>"
    Here is the entirety of the code

    PHP Code:
    <html>
    <head>
    <title>3D Boards</title>
    </head>
    <body>
    <?PHP
     
    $player
    [1]=0;
    $player[2]=0;
    if ( empty(
    $_REQUEST['turn']) ? $turn=$turn=($_REQUEST['turn']) );
    if ( empty(
    $_REQUEST['choice']) ? $choice=$choice=($_REQUEST['choice']) );
    if (
    $choice==1)
    {
    $board[$prow][$pcol]=$turn;
    $choice=0;
    if (( 
    $turn == ) ? $turn=$turn=1);
    }
    echo 
    "Your Turn, Player " $turn "<br /><br />\n";
     
    FOR (
    $row=1$row<17$row++)
    {
    IF (
    $row==5){ECHO "<BR>\n";}
    IF (
    $row==9){ECHO "<BR>\n";}
    IF (
    $row==13){ECHO "<BR>\n";}
        FOR (
    $col=1$col<5$col++)
        {
         IF (
    $board[$row][$col]==1)
         {
            ECHO 
    "<TT>X" "&nbsp;</TT>";
         }
         ELSE IF (
    $board[$row][$col]==2)
         {
            ECHO 
    "<TT>O" "&nbsp;</TT>";
         }
         ELSE
         {
            ECHO 
    "<TT><a href=\"gamescreen.php?prow=$row&pcol=$col&choice=1&turn=$turn\">-</a>" "&nbsp;</TT>";
         }
        }
        ECHO 
    "<BR />\n";
    }
    echo 
    "<br />\n";
    echo 
    "<TT>Player X: " $player[1] . "</TT><br />\n";
    echo 
    "<TT>Player O: " $player[2] . "</TT>\n";
    ?>
    </body>
    </html>

  2. #2
    SitePoint Guru
    Join Date
    Sep 2004
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because all your variables are stored in the URL and everytime someone clicks on something it changes.

  3. #3
    SitePoint Zealot gregs's Avatar
    Join Date
    Aug 2002
    Posts
    161
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Webnet
    Because all your variables are stored in the URL and everytime someone clicks on something it changes.
    How do I pass $boards within the URL then?

  4. #4
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is only one solution. Serialize the array, convert it to text and pass it through the URL.
    Then unserialize it back and reconstruct the board.
    Hope this helps

  5. #5
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But with a simple JavaScript you may post it through the POST method.

  6. #6
    SitePoint Guru
    Join Date
    Sep 2004
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are multiple options, you could store all the information on past clicks for x/o in an array and store it in each user's session. OR you could serialize it and use $_GET or $_POST. $_POST requires an information resubmit every time the page refreshes. $_GET allows the information to be modifiable in the URL.

    $_SESSION is your best route.

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    Building on webnut's theory about using sessions to store the data....
    Heres something I put together for you.

    (commented as much as possible!)
    PHP Code:
    <?php # tic-tac-toe
    // start a session to store the variables and array
    session_start();
    // restart the game by destroying the session
    if(isset($_GET['restart'])) {
    session_destroy();
    }
    // define players
    $player_1 'X';
    $player_2 'O';
    if(
    $_GET['player'] == 'X') { $player $player_2; } else { $player $player_1; }
    echo 
    "Your Turn, Player " $player "<br /><br />\n";
    // define the board. This could be done in a loop.....
    // check that the session count is not 1 ie: the game hasn't been started
    if($_SESSION['count'] != 1) {
    // setup array
    $_SESSION['board'] = array(
    "0"=>"[ * ]""1"=>"[ * ]","2"=>"[ * ]","3"=>"[ * ]","4"=>"[ * ]","5"=>"[ * ]","6"=>"[ * ]","7"=>"[ * ]",
    "8"=>"[ * ]","9"=>"[ * ]","10"=>"[ * ]","11"=>"[ * ]","12"=>"[ * ]","13"=>"[ * ]","14"=>"[ * ]","15"=>"[ * ]");
    }
    // if a turn has been made
    if(isset($_GET['id'])) {
    // set the session key corresponding to the passed id is set as the players character
    $_SESSION['board'][$_GET['id']] = '[ ' $player ' ]';
    // set the session counter to 1 to signify the game has begun
    $_SESSION['count']=1;

    ?>
    <style type="text/css">
    a:link {
    color: #000000;
    text-decoration: none;
    }
    a:hover {
    color: #FF0000;
    text-decoration: none;
    }
    a:active {
    color: #000000;
    text-decoration: none;
    }
    </style>
    <h2>Tic-Tac-toe!</h2>
    <?php
    // display board
    foreach($_SESSION['board'] as $key=>$data) {
    // if the key is divisible by 4, echo a <br>
    if(($key%4) == 0) { echo '<br>';}
    // if the data is equal to either X or O, dont offer a link
    if($data == '[ X ]') { echo '[ X ]'
    } elseif(
    $data == '[ O ]') { echo '[ O ]'; } else  
     {
     
    // else offer a link to click
    echo '<a href="?player='.$player '&id='.$key.'">'$data '</a>';
        }

    ?>
    <p><a href="?restart">restart</a></p>
    You can extend it further by setting up an function to check if the rows/ columns add up to 4 of the same characters.

    Hope this helps, let me know!

    Cheers

    Mike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •