-2

I am creating a tic-tac-toe game in PHP.

I take value from user as integer position in a grid like-

 1 | 2 | 3 
---|---|---
 4 | 5 | 6 
---|---|---
 7 | 8 | 9 

I store the value entered by user as a CVS in session. Like if user creates X in Ist row IInd column and IInd row IInd column, then the value in session will be 2,5.

Now the main problem is when I want to create O for computer as a random position between 1 to 9, it should not contain 2 and 5.

How would I create a random number between 1 and 9 that does not contain the values entered by user (which are stored in session variable as a CVS string)?

This is the script so far:

<?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>
Don
  • 863
  • 1
  • 8
  • 22
  • OOP should be of great assistance to you here. – Madara's Ghost Aug 06 '12 at 15:13
  • I did this in ProLog years ago. The approach then was create a list of available spaces, move a position to O's or X's list as they are made. So available spaces will only ever contain valid moves left. – Waygood Aug 06 '12 at 15:17

2 Answers2

2

Create an array containing all legal positions (In this case 1,3,4,6,7,8,9), then use rand() to pick a random index in the created array.

ymgve
  • 318
  • 2
  • 7
1

First, I would store the values in an array (not a CSV string).

$selected = array(2, 5);

If you MUST store the values in a CSV string, convert to an array:

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

Then I would create an array of available selections

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

Then pick randomly from $available

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

Finally, update the $selectedCSV in the session

$_SESSION['selected'] = implode(",", $selected);
Matt
  • 6,993
  • 4
  • 29
  • 50