我必须在php中制作一个tic tac toe游戏,这是我到目前为止所做的:
<html>
<body>
<h1>Tic Tac Toe</h1>
<?php
//vars
if (!ISSET ($_POST['submit'])){
$_POST ['value'] = '';
}
//players
if (!ISSET($_POST ['turn'])){
$turn=1;
$ul='';
$um='';
$ur='';
$ml='';
$mm='';
$mr='';
$ll='';
$lm='';
$lr='';
}
else{
$turn=$_POST['turn'];
if ($turn==1){
$turn=2;
}
else{
$turn=1;
}
}
//boxes
$ul='ul';
$um='um';
$ur='ur';
$ml='ml';
$mm='mm';
$mr='mr';
$ll='ll';
$lm='lm';
$lr='lr';
//forms
$box= $_POST ['box'];
//Calculations
//player 1
if ($turn==2){
if ($box == $ul){
$ul = "X";
}
if ($box == $um){
$um = "X";
}
if ($box == $ur){
$ur = "X";
}
if ($box == $ml){
$ml = "X";
}
if ($box == $mm){
$mm = "X";
}
if ($box == $mr){
$mr = "X";
}
if ($box == $ll){
$ll = "X";
}
if ($box == $lm){
$lm = "X";
}
if ($box == $lr){
$lr = "X";
}
}
//player 2
else{
if ($box == $ul){
$ul = "O";
}
if ($box == $um){
$um = "O";
}
if ($box == $ur){
$ur = "O";
}
if ($box == $ml){
$ml = "O";
}
if ($box == $mm){
$mm = "O";
}
if ($box == $mr){
$mr = "O";
}
if ($box == $ll){
$ll = "O";
}
if ($box == $lm){
$lm = "O";
}
if ($box == $lr){
$lr = "O";
}
}
?>
<table width='100pX' height='100pX' border='1'>
<tr>
<td><?php echo $ul;?></td>
<td><?php echo $um;?></td>
<td><?php echo $ur;?></td>
</tr>
<tr>
<td><?php echo $ml;?></td>
<td><?php echo $mm;?></td>
<td><?php echo $mr;?></td>
<tr>
<td><?php echo $ll;?></td>
<td><?php echo $lm;?></td>
<td><?php echo $lr;?></td>
</tr>
</table>
<?php echo 'Player ' . $turn . ", it's your turn"?>
<form method='POST' action='<?php echo $_SERVER['PHP_SELF'];?>'>
<input type='text' name='box'>
<input type='hidden' value= "<?php echo $turn;?>" name='turn'>
<input type='hidden' value= "<?php echo $ul;?>" name='ul'>
<input type='hidden' value= "<?php echo $um;?>" name='um'>
<input type='hidden' value= "<?php echo $ur;?>" name='ur'>
<input type='hidden' value= "<?php echo $ml;?>" name='ml'>
<input type='hidden' value= "<?php echo $mm;?>" name='mm'>
<input type='hidden' value= "<?php echo $mr;?>" name='mr'>
<input type='hidden' value= "<?php echo $ll;?>" name='ll'>
<input type='hidden' value= "<?php echo $lm;?>" name='lm'>
<input type='hidden' value= "<?php echo $lr;?>" name='lr'>
<input type= 'submit' name= 'submit' value= 'Go'>
</form>
</html>
</body>
&#13;
答案 0 :(得分:1)
您需要将值传递给下一个表单。您正在为表单中的每个部分执行隐藏输入,但是您没有获得这些值。
在$ ul ='ul'的正下方,您需要输入
$ul = $_POST['ul'];
等所有值。这样您就可以将之前设置的值拉到当前表单中。