我刚刚开始学习PHP @ school和第三项任务,我们必须创建一个TicTacToe游戏。我在youtube上关注了一个turorial视频,并制作了一款可玩的游戏。但它并不知道转向它的人。 IE:您可以继续按下提交按钮,计算机将继续填写O,而玩家无需填写X.
请有人解释我怎么能让剧本知道谁变成了? 我想知道它背后的逻辑。因此,只有一个代码根本不会帮助我,请解释如何在切换转弯之前检查玩家是否填写了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 :(得分:0)
你可以添加一个新变量$ _POST [&#39; player&#39;]你可以从0改为1或从1改为0,这样就可以通过玩家现在应该玩的值来知道。 我不打算给你写代码,因为我给你一个提示应该怎么做,你必须自己学习:)
答案 1 :(得分:0)
我会添加一个小的txt文件来保存最后一个文件的播放方式。
例如,您可以将许多不同的值保存到单个.txt文件中。
Applicative
您可以使用以下代码:
//保存下一位玩家的名字
ljoin . ljoin == ljoin . fmap ljoin
//加载播放器设置
<?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"];
}
?>