我正在用PHP创建井字游戏。在创建井字游戏时需要帮助
予取值从用户作为整数位置在网格喜欢 -
1 | 2 | 3 ---|---|--- 4 | 5 | 6 ---|---|--- 7 | 8 | 9
我存储由用户输入的作为在会话的CVS的值。如果用户在第I行第II列和第II行第II列中创建X,则会话中的值将是2.5。
现在主要的问题是,当我想创建一个计算机O作为介于1到9的随机位置,它不应该包含2和5
我怎么会创造1和9之间的随机数不包含用户输入的值(它们作为CVS字符串存储在会话变量中)?
这是迄今为止脚本:
<?php
session_start();
?>
<html>
<head>
<title> Hello! </title>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
<meta name='txtweb-appkey' content ='1205be63-8293-4c02-82ce-17c500075e80' />
</head>
<body>
<?php
if(!empty($_GET['user'])){
$user=$_GET['user'];
if($user=='x'||$user=='X'){$com='O';}
if($user=='o'||$user=='O'||$user=='0'){$user='O';$com='X';}
$user=strtoupper($user);
echo 'U choose to play with ',$user;
if(empty($_GET['move'])){
echo '<br />Take ur keypad as grid & start playing by replying with grid number<br />E.g.: 4 for 2 row 1 column<br />';
}
$grid=array();
$grid[0]=array(' ',' ',' ');
$grid[1]=array(' ',' ',' ');
$grid[2]=array(' ',' ',' ');
if(!empty($_GET['move'])){
$usermove=$_GET['move'];
$s=$_SESSION['usermove'];
$pos=strpos($usermove,$s);
if(!($pos===false)){echo 'This position is not empty!!';exit();}
$s=$_SESSION['commove'];
$pos=strpos($usermove,$s);
if(!($pos===false)){echo 'This position is not empty!!';exit();}
if(!empty($_SESSION['usermove'])){$_SESSION['usermove']=$_SESSION['usermove'].','.$usermove;}
else{$_SESSION['usermove']=$usermove;}
$moves=explode(',',$_SESSION['usermove']);
foreach($moves as $value){
switch($value){
case '1':$grid[0][0]=' '.$user.' ';
break;
case '2':$grid[0][1]=' '.$user.' ';
break;
case '3':$grid[0][2]=' '.$user.' ';
break;
case '4':$grid[1][0]=' '.$user.' ';
break;
case '5':$grid[1][1]=' '.$user.' ';
break;
case '6':$grid[1][2]=' '.$user.' ';
break;
case '7':$grid[2][0]=' '.$user.' ';
break;
case '8':$grid[2][1]=' '.$user.' ';
break;
case '9':$grid[2][2]=' '.$user.' ';
break;
}
}
if(strlen($moves)==1){
$ran=rand(2,9);
if($ran==$moves[0]){
$ran=$ran-1;
}
}
else{
$ran=rand(1,9);
$i=0;
while(in_array($ran,$_SESSION['usermove'])){
$ran=rand(1,9);
$i++;
if($i>=9){$ran=12;break;}
}
}
if(!empty($_SESSION['commove'])){$_SESSION['commove']=$_SESSION['commove'].','.$ran;}
else{$_SESSION['commove']=$ran;}
$cmoves=explode(',',$_SESSION['commove']);
foreach($cmoves as $value){
switch($value){
case '1':$grid[0][0]=' '.$com.' ';
break;
case '2':$grid[0][1]=' '.$com.' ';
break;
case '3':$grid[0][2]=' '.$com.' ';
break;
case '4':$grid[1][0]=' '.$com.' ';
break;
case '5':$grid[1][1]=' '.$com.' ';
break;
case '6':$grid[1][2]=' '.$com.' ';
break;
case '7':$grid[2][0]=' '.$com.' ';
break;
case '8':$grid[2][1]=' '.$com.' ';
break;
case '9':$grid[2][2]=' '.$com.' ';
break;
}
}
}
echo '<br /><pre>';
echo $grid[0][0],'|',$grid[0][1],'|',$grid[0][2],'<br />';
echo '---|---|---<br />';
echo $grid[1][0],'|',$grid[1][1],'|',$grid[1][2],'<br/>';
echo '---|---|---<br />';
echo $grid[2][0],'|',$grid[2][1],'|',$grid[2][2],'</pre><br />';
echo '<form action="',$_SERVER['PHP_SELF'],'" method="get" class="txtweb-form">';
echo 'Your option<input type="text" name="move" />';
echo '<input type="hidden" name="user" value="',$user,'" />';
echo '<input type="submit" value="send" /></form>';
echo '</body></html>';
exit();
}
$_SESSION['usermove']='';
$_SESSION['commove']='';
echo 'Wellcome, choose X or O<br/>';
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="get" class="txtweb-form">';
echo 'X or O<input type="text" name="user" />';
echo '<input type="submit" value="choose" /></form>';
?>
</body>
</html>
OOP应该是有很大帮助的,以你在这里的
$selectedCSV
。 – 2012-08-06 15:13:20几年前,我在ProLog中做过这件事。然后,这个方法是创建一个可用空间的列表,当它们被创建时,将一个位置移动到O或X的列表。所以可用的空间将只包含有效的移动。 – Waygood 2012-08-06 15:17:32