【问题标题】:Tic Tac Toe logic errors井字游戏逻辑错误
【发布时间】:2011-11-21 14:46:57
【问题描述】:

我的程序是一个简单的井字游戏,我的获胜者没有正确显示,唯一正确的 4 个是两个对角线和行获胜和列获胜。html/javascript 不是问题,它只是 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>

【问题讨论】:

  • 什么是正确的,什么是错误的?
  • 我没有收到任何错误,但是当我测试选项时,当第二列和第三列以及第二和第三行中连续有 3 个但如果连续三个在第一行或第一列,然后正确显示获胜者
  • 在第一次测试中,看起来您的一些单元格在提交时传递了小写值。 4 列以小写形式传递 o (r2c2, r2c3, r3c1, r3c2),3 列传递 x (r2c2, r3c1, r3c2)。

标签: php logic


【解决方案1】:

在代码顶部添加这样的内容应该可以使其正常工作。您的某些单元格正在传递小写值。您可能应该进行一些其他错误检查,因为可以操纵 Javascript。

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

【讨论】:

  • 另外,如果你将你的事件绑定到change而不是keyup,它将解决值被发布为小写的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多