doufan8805 2015-10-04 09:14 采纳率: 0%
浏览 30

如何检查X是否已播放并将转弯切换为O.

I just started learning PHP @ school and for the third assignment we have to create a TicTacToe game. I have followed a turorial video on youtube and have made a game that is playable at the moment. But it doesnt know whos turn it is. IE: you can keep pressing the submit button and the computer will keep filling in O's without the player to have to fill in a X.

Please can someone explain how i can make the script know who's turn it is? I want to know the logic behind it. So only a code wont help me at all, please explain how you check if the player has filled in a X before you switch turn for instance.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Boter, Kaas & Eieren</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
<?php

$winner = 'niemand';
$box = array('','','','','','','','','');

if (isset($_POST["submit"])){   //When the player hits submit, we retrieve the data
    $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'];
    //print_r($box); //kijken in welke array, wat is ingevuld

    //check if the player has won
    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[4]=='x' && $box[8]=='x') ||
        ($box[2]=='x' && $box[4]=='x' && $box[6]=='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')){
            $winner = 'x';
            echo "Speler wint";
        }

    //check if X has played and switch turn to O


    $blank = 0; //assume there is no empty box
    //check for an empty box
    for ($i=0; $i<=8; $i++){
        if ($box[$i]==''){
            $blank=1;
        }
    }

    //if there is an empty box and no winner yet its O's turn
    if ($blank == 1 && $winner == 'niemand'){
        $i = rand(0,8);
        while ($box[$i]!=''){ //keep looking for an empty box if $i isnt empty
            $i = rand(0,8);
        }
        $box[$i] = "o"; 

            //check if O has won
        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[4]=='o' && $box[8]=='o') ||
            ($box[2]=='o' && $box[4]=='o' && $box[6]=='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')){
                $winner = "o";
                echo "KI wint";
            }

    }

    //check if it is a draw
    if ($blank == 0 && $winner == 'niemand'){
        echo "Gelijkspel!";
    }
}

?>
<div id="beurt">
    <p>
    <form action="destroy.php" method="get">
    <input type="submit" id="destroy" onClick="windows.location.href'index.php'" value="Begin opnieuw!">
    </form>
    </p>
</div>
<form id="game" name="tictactoe" method="post">
<?php

//create the grid to play
for ($i=0; $i<=8; $i++){
    echo "<input class=\"box\" type=\"text\" name=\"box$i\" value=\"$box[$i]\">";

    if ($i==2||$i==5||$i==8){   //put in a break if $i is 2,5 or 8
        echo "<br>";
    }
}
    if ($winner == 'niemand'){
        echo "<br><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Spelen!\"><br></form>";
    }

?>
</body>
</html>

Please help me out.

  • 写回答

2条回答 默认 最新

  • dse323222 2015-10-04 09:34
    关注

    You can add a new variable $_POST['player'] which you can change from 0 to 1 or from 1 to 0 and that way know by the value which player should play now. I'm not going to write you the code since I gave you a hint how it should be done and you have to learn by yourself :)

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度