www.webdeveloper.com
Results 1 to 5 of 5

Thread: need help in php

  1. #1
    Join Date
    Jun 2014
    Posts
    1

    Question need help in php

    So I have managed to get this far with the code. I'm making a simple tic tac toe game for my school project and I would be very happy if someone could help me with my code.

    PHP Code:
    <!DOCTYPE html>
    <?php
    $WINNER 
    'N';
    $box = array('''''''''''''''''');
    if (isset(
    $_POST["sb"])) {
        
    $box[0] = $_POST["box0"];
        
    $box[1] = $_POST["box1"];
        
    $box[2] = $_POST["box2"];
        
    $box[3] = $_POST["box3"];
        
    $box[4] = $_POST["box4"];
        
    $box[5] = $_POST["box5"];
        
    $box[6] = $_POST["box6"];
        
    $box[7] = $_POST["box7"];
        
    $box[8] = $_POST["box8"];

        if ((
    $box[0] == 'x' && $box[1] == 'x' && $box[2] == 'x') ||
                (
    $box[3] == 'x' && $box[4] == 'x' && $box[5] == 'x') ||
                (
    $box[6] == 'x' && $box[7] == 'x' && $box[8] == 'x') ||
                (
    $box[0] == 'x' && $box[3] == 'x' && $box[6] == 'x') ||
                (
    $box[1] == 'x' && $box[4] == 'x' && $box[7] == 'x') ||
                (
    $box[2] == 'x' && $box[5] == 'x' && $box[8] == 'x') ||
                (
    $box[0] == 'x' && $box[4] == 'x' && $box[8] == 'x') ||
                (
    $box[2] == 'x' && $box[4] == 'x' && $box[6] == 'x')) {
            
    $WINNER 'x';
            echo 
    'WINNER IS: X';
        }

        
    $blanck 0;
        for (
    $i 0$i <= 8$i++) {
            if (
    $box[$i] == '') {
                
    $blanck 1;
            }
        }

        if (
    $blanck == && $WINNER == 'n') {
            
    $i rand() % 8;
            while (
    $box[$i] != '') {
                
    $i rand() % 8;
            }
            
    $box[$i] = 'o';
            if ((
    $box[0] == 'o' && $box[1] == 'o' && $box[2] == 'o') ||
                    (
    $box[3] == 'o' && $box[4] == 'o' && $box[5] == 'o') ||
                    (
    $box[6] == 'o' && $box[7] == 'o' && $box[8] == 'o') ||
                    (
    $box[0] == 'o' && $box[3] == 'o' && $box[6] == 'o') ||
                    (
    $box[1] == 'o' && $box[4] == 'o' && $box[7] == 'o') ||
                    (
    $box[2] == 'o' && $box[5] == 'o' && $box[8] == 'o') ||
                    (
    $box[0] == 'o' && $box[4] == 'o' && $box[8] == 'o') ||
                    (
    $box[2] == 'o' && $box[4] == 'o' && $box[6] == 'o')) {
                
    $WINNER 'o';
                echo 
    'WINNER IS: O';
            }
        }  else {
            
    $WINNER ='t';
            print 
    'Tied game';
        }
    }
    ?>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
            <form name="tictactoe" action="index.php" method="POST">
                <?php

                
    for ($i 0$i <= 8$i++) {

                    
    printf('<input type="text" name="box%s" value="%s" />'$i$box[$i]);
                    if (
    $i == || $i == || $i == 8) {
                        echo 
    '</br>';
                    }
                }
                if (
    $WINNER == 'n') {
                    
    printf('<input type = "submit" value = "GO" name = "sb" />');
                } else {
                    
    printf('<input type="reset" value="Play again" name="newGame" onclick="window.location.href=\'index.php\'" />');
                }
                
    ?> 
            </form>
        </body>
    </html>
    Last edited by NogDog; 06-09-2014 at 08:37 AM. Reason: added [php] tags around code

  2. #2
    Join Date
    May 2014
    Posts
    75
    Please use [php ] [/php ] tags around your code, it makes it much more simple to read.

    It may also help to mention what is wrong. What exactly do you need help with?

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    I would only use printf() if you are going to use place-holders in the string with additional arguments, otherwise just use echo.

    I would try to come up with an algorithm that detected 3-in-a-row conditions that could be applied to a function call, returning the winner if there is one, else null or false. That would help with the DRY principle (Don't Repeat Yourself).

    As in the previous reply, though, do you have any specific problem/question you need addressed?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    May 2014
    Posts
    909
    Some advice:

    1) as of PHP 5.4, you don't have to say "array" anymore.

    2) I'd make it a 2 dimensional array, it's easier to keep track of [2,2] than it is [9].

    3) I'd set the array as empty on an else, so you aren't wasting time setting it if $_POST is set for it.

    4) I'd functionalize the check since you're doing the same set of 'if' for both 'x' and 'o'.

    5) and yeah I'm with @NogDog on this, I'd lose the printF. It's slow and often hard to follow.

    6) if you made the names of your input elements name="box[0][0]", you could just do $box = $_POST['box'] instead of the massive list of manual copies.

    7) I'd consider making empty cells be radio buttons so the user can simply click one cell, then use some JS to trap 'onchange' to submit the form. Likewise I'd consider making filled out fields "readonly", or not even send that data client-side and instead track it in sessions. That last option could work our really well and would have a lot less overhead.

    If I have time later this week I'll try to see if I can spare a few to toss together a demo of that approach.
    Java is to JavaScript as Ham is to Hamburger.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    A General Algorithm for Tic-Tac-Toe Board Evaluation (PDF)

    Okay, back to work for me, even though that looks more interesting.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles