2012-08-06 87 views
-2

我正在用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> 
+0

OOP應該是有很大幫助的,以你在這裏的$selectedCSV。 – 2012-08-06 15:13:20

+0

幾年前,我在ProLog中做過這件事。然後,這個方法是創建一個可用空間的列表,當它們被創建時,將一個位置移動到O或X的列表。所以可用的空間將只包含有效的移動。 – Waygood 2012-08-06 15:17:32

回答

2

創建包含所有合法的位置(在這種情況下1,3,4,6,7,8,9),然後使用蘭特陣列()來在創建的數組中選擇一個隨機索引。

1

首先,我會將值存儲在數組中(而不是CSV字符串)。

$selected = array(2, 5); 

如果必須存儲在一個CSV字符串的值轉換爲數組:

$selected = explode(',', $_SESSION['selected']); 

然後我會創建可用的選項

$ticTacToe = array(1,2,3,4,5,6,7,8,9); 
$available = array_diff($selected, $ticTacToe); 

數組,接着從隨機挑選$available

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

最後,更新會話

$_SESSION['selected'] = implode(",", $selected);