2015-10-04 75 views
0

我刚开始学习PHP @ school,第三项任务是创建TicTacToe游戏。我在YouTube上播放了一段视频,并制作了一个可以在当前播放的游戏。但它不知道它是谁。 IE:你可以继续按下提交按钮,电脑将继续填写O's,无需玩家填写X.如何检查X是否已经播放并切换到O

请有人解释我可以如何让脚本知道谁将它变成了? 我想知道它背后的逻辑。所以只有一个代码不会对我有所帮助,请解释一下如何在切换之前检查玩家是否填入了X。

<!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> 

请帮帮我。

回答

0

您可以添加一个新的变量$ _ POST [“球员”],你可以改变从0到1或从1到0和途径,使玩家现在应该发挥的价值认识。 我不会写你的代码,因为我给你一个提示它应该怎么做,你必须自己学习:)

0

我会添加一个小的txt文件来保存最后一个玩。

例如,您可以将许多不同的值保存到一个.txt文件中,像这样。

<?php 

function savesettings($nextturn){ 

if(is_file("file.txt")){ 
$mysettings = unserialize(file_get_contents("file.txt")); 
}else{ 
$mysettings = array();} 
$mysettings["nextturn"]=$nextturn; 
file_put_contents("file.txt",serialize($mysettings)); 
} 

function loadsettings(){ 
if(is_file("file.txt")){ 
    $mysettings = unserialize(file_get_contents("file.txt")); 
}else{ 
    $mysettings = array(); 
} 
return $mysettings["nextturn"]; 

} 

?> 

您可以使用下面的代码:

//保存下一个球员的名字

savesettings("David"); 

//将播放器设置

$player = loadsettings();