2015-10-04 41 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();