Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 需要帮助创建一个tic-tac-toe游戏吗_Php_Tic Tac Toe - Fatal编程技术网

Php 需要帮助创建一个tic-tac-toe游戏吗

Php 需要帮助创建一个tic-tac-toe游戏吗,php,tic-tac-toe,Php,Tic Tac Toe,我正在用PHP创建一个tic-tac-toe游戏 我将用户的值作为网格中的整数位置,如- 1 | 2 | 3 ---|---|--- 4 | 5 | 6 ---|---|--- 7 | 8 | 9 1 | 2 | 3 ---|---|--- 4 | 5 | 6 ---|---|--- 7 | 8 | 9 我将用户输入的值存储为会话中的CVS。就像用户在列表行IInd列和IInd行IInd列中创建X一样,会话中的值将是2,5 现在主要的问题是,当我想为计算机创建一个在1到9之间的随机

我正在用PHP创建一个tic-tac-toe游戏

我将用户的值作为网格中的整数位置,如-

1 | 2 | 3 ---|---|--- 4 | 5 | 6 ---|---|--- 7 | 8 | 9 1 | 2 | 3 ---|---|--- 4 | 5 | 6 ---|---|--- 7 | 8 | 9 我将用户输入的值存储为会话中的CVS。就像用户在列表行IInd列和IInd行IInd列中创建X一样,会话中的值将是2,5

现在主要的问题是,当我想为计算机创建一个在1到9之间的随机位置O时,它不应该包含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>

你好

创建一个包含所有合法位置的数组(在本例中为1,3,4,6,7,8,9),然后使用rand()在创建的数组中选择一个随机索引。

首先,我将值存储在数组中(不是CSV字符串)

如果必须将值存储在CSV字符串中,请转换为数组:

$ticTacToe = array(1,2,3,4,5,6,7,8,9);
$available = array_diff($selected, $ticTacToe);
然后我将创建一个可用选择的数组

$selected[] = $available[rand(0, count($available) - 1)];
然后从
$available

$_SESSION['selected'] = implode(",", $selected);
最后,在会话中更新
$selectedCSV


OOP在这里应该对你有很大的帮助。我几年前在ProLog中做过这个。然后,方法是创建可用空间的列表,在创建位置时将其移动到O或X的列表中。因此,可用空间将只包含有效的左移。
$selected[] = $available[rand(0, count($available) - 1)];
$_SESSION['selected'] = implode(",", $selected);