English 中文(简体)
Tic Tac 逻辑错误
原标题:Tic Tac Toe logic errors
  • 时间:2011-11-21 14:46:57
  •  标签:
  • php
  • logic

我的节目是一种简单的语气游戏,我的赢家没有被适当展示,只有4个正确的东西是两条方言和双赢。 The html/javascript is not the problem it only the php.

<?php
//Players
$X = "Dalton";
$O = "Joe";
$winner = false;
//game board positoins
$r1c1 = !empty($_POST[ r1c1 ] ) ? $_POST[ r1c1 ] :   ;
$r1c2 = !empty($_POST[ r1c2 ] ) ? $_POST[ r1c2 ] :   ;
$r1c3 = !empty($_POST[ r1c3 ] ) ? $_POST[ r1c3 ] :   ;
$r2c1 = !empty($_POST[ r2c1 ] ) ? $_POST[ r2c1 ] :   ;
$r2c2 = !empty($_POST[ r2c2 ] ) ? $_POST[ r2c2 ] :   ;
$r2c3 = !empty($_POST[ r2c3 ] ) ? $_POST[ r2c3 ] :   ;
$r3c1 = !empty($_POST[ r3c1 ] ) ? $_POST[ r3c1 ] :   ;
$r3c2 = !empty($_POST[ r3c2 ] ) ? $_POST[ r3c2 ] :   ;
$r3c3 = !empty($_POST[ r3c3 ] ) ? $_POST[ r3c3 ] :   ;

if($r1c1 == $r1c2 && $r1c2 == $r1c3 ) {
    $winner = $r1c1;
} elseif($r2c1 == $r2c2 && $r2c2 == $r2c3 ) {
    $winner = $r2c1;
} elseif($r3c1 == $r3c2 && $r3c2 == $r3c3 ) {
    $winner = $r3c1;
} elseif($r1c1 == $r2c1 && $r2c1 == $r3c1 ) {
    $winner = $r1c1;
} elseif($r1c2 == $r2c2 && $r2c2 == $r3c2 ) {
    $winner = $r1c2;
} elseif($r1c3 == $r2c3 && $r2c3 == $r3c3 ) {
    $winner = $r1c3;
} elseif($r1c1 == $r2c2 && $r2c2 == $r3c3 ) {
    $winner = $r1c1;
} elseif($r1c3 == $r2c2 && $r2c2 == $r3c1 ) {
    $winner = $r1c3;
} else {
    $winner =  Draw ;
}

?>

<html>
    <head>
        <title>Tic-Tac-Toe</title>
        <!--

            The Javascript code below requires access to the internet to run. If you are
            running this page on a computer with no Internet access, it will cause errors.
            You may delete these lines or modify them if you wish.

            The 1st block in the code below is helping you by not allowing any character 
            except an uppercase  X  or uppercase  O . This is unnessisary, but makes it easier
            for you to process on the PHP side.

            The 2nd block of code is helping you by making any field that already has has 
             X  or  O  readonly when the page loads. Extra points will be giving to any student 
            how can replicate this part of the Javascript using only PHP. Simply delete the lines 
            below if you wish to attempt.

        -->
        <script src= https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js ></script>
        <script type= text/javascript >
            $(document).ready(function(){

                //  This block of code prevents you from entering anything but X or O
                $( input ).bind( keyup ,function() {
                    if( $(this).hasClass( button ) == false ){
                        this.value = this.value.toUpperCase();
                        if(this.value !=  X  && this.value !=  O ) {
                            this.value =   ;
                        }
                    }
                });

                //  This block of code makes the used spots on the grid readonly once used
                $( input ).each(function(i){
                    this.value = this.value.toUpperCase();
                    if(this.value ==  X  || this.value ==  O ) {
                        $(this).addClass( disabled ).attr( readonly , true);
                        //this.readOnly = true;
                    }
                });

            });
        </script>
        <!--   You may do as you please with this CSS code   -->
        <style>
            body * { padding:0; margin:0; }
            .disabled { background: #eeeeee; }
            .button { display: block; width:180px; margin: 10px 0; }
            #gameboard { border-collapse:collapse; padding:0; margin:0; }
            #gameboard tr:nth-child(even) { 
                border-top:2px solid black;
                border-bottom:2px solid black;
            }
            #gameboard tr td:first-child { border-right:2px solid black; }
            #gameboard tr td:last-child { border-left:2px solid black; }
            #gameboard input { 
                font-size:50px; 
                width:60px; 
                padding:3px;
                text-transform:uppercase;
                text-align:center;
            }
        </style>
    </head>
    <body>
        <!--   
            Notice that the default values for each box below is set to   , and empty string. 
            You will have to set them using data from the $_POST, if you don t the game will 
            reset after every move. Something like this for every box maybe ...

            if( !empty($_POST[ r1c0 ]) ) {
                $r1c0 = $_POST[ r1c0 ];
            } else {
                $r1c0 =   ;
            }

        -->
        <form action="" name= game  method= POST >
            <table cellpadding= 0  cellspacing= 0  id= gameboard >
                <tr>
                    <td><input type= text  maxlength= 1  name= r1c1  value= <?php echo $r1c1; ?>  /></td>
                    <td><input type= text  maxlength= 1  name= r1c2  value= <?php echo $r1c2; ?>  /></td>
                    <td><input type= text  maxlength= 1  name= r1c3  value= <?php echo $r1c3; ?>  /></td>
                </tr>
                <tr>
                    <td><input type= text  maxlength= 1  name= r2c1  value= <?php echo $r2c1; ?>  /></td>
                    <td><input type= text  maxlength= 1  name= r2c2  value= <?php echo $r2c2; ?>  /></td>
                    <td><input type= text  maxlength= 1  name= r2c3  value= <?php echo $r2c3; ?>  /></td>
                </tr>
                <tr>
                    <td><input type= text  maxlength= 1  name= r3c1  value= <?php echo $r3c1; ?>  /></td>
                    <td><input type= text  maxlength= 1  name= r3c2  value= <?php echo $r3c2; ?>  /></td>
                    <td><input type= text  maxlength= 1  name= r3c3  value= <?php echo $r3c3; ?>  /></td>
                </tr>
            </table>
        <?php if($winner !=   ) { ?>
            <h3><?php echo "The winner is ".$winner; ?></h3>
        <?php } else { ?>
            <input class= button  type= submit  value= Finish Move  />
            <input class= button  type="reset" value="Reset Game" />
        <?php } ?>
        </form>
    </body>
</html>
问题回答

在你的法典顶端添加这样的内容,应当使之发挥作用。 你们的一些囚室通过的案例价值较低。 你可能还会发现其他一些错误,因为可以操纵 Java字。

foreach ($_POST as $key => $val) {
    $_POST[$key] = strtoupper($val);
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签