Zend - The PHP Company




Games

Add Code


Robot Script  

Type: application
Added by: codewalkers
Entered: 24/08/2002
Last modified: 08/12/2001
Rating: - (fewer than 3 votes)
Views: 8650
This is a winner of the Codewalkers.com PHP Coding Contest. This script won the Capture the Flag contest.


<?
//*******************************************************************
// Written by: Tim Jensen
// Date: 08/10/02
//
//*******************************************************************


$key=0;
$path=0;
$input getInput();

echo 
"$inputn";

if ( 
ereg ("^L",$input) && $input != "LBAD" )
{
    
$rest substr$input1);
    list ( 
$holdeX$holdeY ) = explode(","$rest);
    
$input "LOCATE";
}

//Check to see if teleport response
if ( ereg ("^T",$input) && ( $input != "TRAPOK" && $input != "TRAPBAD" ))
{
    
$rest substr$input1);
    list ( 
$holdX$holdY ) = explode(","$rest);
    
$input "TRANSPORT";
}

switch ( 
$input ){

    case 
'STARTWHITE':    
        
$path 1;
        
$fX 26;
        
$fY 26;
        
$x 1;
        
$y 1;
        
$number 1;
        
$eX 29;
        
$eY 27;
        
$move "MR";
        
$death 1;
        
$locCount 0;
        
$trapCount 0;
        @
createDataFile($x$y$number$move$eX$eY$fX$fY$path$death$locCount$trapCount);
        
createMoveFile($move);
        break;

    case 
'STARTRED':
        
$path 2;
        
$fX 3;
        
$fY 3;
        
$x 28;
        
$y 27;
        
$number 1;
        
$eX 0;
        
$eY 1;
        
$move "ML";
        
$death 1;
        
$locCount 0;
        
$trapCount 0;
        @
createDataFile($x$y$number$move$eX$eY$fX$fY$path$death$locCount$trapCount);
        
createMoveFile($move);
        break;

     case 
'MOVEOK':
        
$data getData();
        list(
$x$y$number$lastMove$eX$eY$fX$fY$path$death$locCount$trapCount) = explode(",",$data);
        break;
        
    case 
'DEATHMISSED':
        
$data getData();
        list(
$x$y$number$lastMove$eX$eY$fX$fY$path$death$locCount$trapCount) = explode(",",$data);
        break;
        
    case 
'TRAPOK':
        
$data getData();
        list(
$x$y$number$lastMove$eX$eY$fX$fY$path$death$locCount$trapCount) = explode(",",$data);
        break;
        
    case 
'STUCK':
        
$data getData();
        list(
$x$y$number$lastMove$eX$eY$fX$fY$path$death$locCount$trapCount) = explode(",",$data);
        break;
        
    case 
'LBAD':
        
$data getData();
        list(
$x$y$number$lastMove$eX$eY$fX$fY$path$death$locCount$trapCount) = explode(",",$data);
        
$locCount 10;
        break;
        
    case 
'LOCATE':
        
$data getData();
        list(
$x$y$number$lastMove$eX$eY$fX$fY$path$death$locCount$trapCount) = explode(",",$data);
        
$eX $holdeX;
        
$eY $holdeY;
        break;
        
    case 
'TRANSPORT':
        
$data getData();
        list(
$x$y$number$lastMove$eX$eY$fX$fY$path$death$locCount$trapCount) = explode(",",$data);
        
$x $holdX;
        
$y $holdY;
        break;
        
    default :
        
$move "Did not work .. $input";
        
createMoveFile($move);

}

//Whites initial start
/*
$whiteInit  = array(1 => 'MR',
                        2 => 'MD',
                        3 => 'TR',
                        4 => 'MD',
                    5 => 'MD',
                        6 => 'TU',
                        7 => 'MR',
                        8 => 'MR',
                     9 => 'TL',
                    10 => 'MU',
                        11 => 'TT',
                    12 => 'MR',
                        13 => 'ML');
*/
/**
 * $redInit  = array(    1 => 'ML',
                        2 => 'TL',
                        3 => 'MU',
                        4 => 'MU',
                    5 => 'TD',
                        6 => 'ML',
                        7 => 'ML',
                        8 => 'TR',
                     9 => 'MD',
                    10 => 'TT',
                        11 => 'ML',
                    12 => 'MR');
 */
    
$whiteInit  = array(=> 'TT',
                        
=> 'MR',
                        
=> 'ML');
                    
$redInit  = array(    => 'TT',
                        
=> 'ML',
                        
=> 'MR');
    
//Will run Whites initial script    
if ( $path == )
{
    if ( 
$number )
    {
        
        
$move $whiteInit[$number];
        
$catch figureMovement$x$y$move );
        list(
$x$y) = explode ","$catch);
        
$number++;
        if ( 
ereg ("^T",$move) )
        {
            
$trapCount++;
        }
        
createDataFile($x$y$number$move$eX$eY$fX$fY$path$death$locCount$trapCount);
        
createMoveFile($move);
    }    
}

//Will run Reds initial script
if ( $path == )
{
    if ( 
$number )
    {
        
$move $redInit[$number];
        
$catch figureMovement$x$y$move );
        list(
$x$y) = explode ","$catch);
        
$number++;
        if ( 
ereg ("^T",$move) )
        {
            
$trapCount++;
        }
        
createDataFile($x$y$number$move$eX$eY$fX$fY$path$death$locCount$trapCount);
        
createMoveFile($move);
    }
}


if ( (
$number == 16 || $number == 24 || $number == 29 || $number == 38 || $number == 45) && ( $locCount ) && ( $death == ))
{
    
$move "LL";
    
$number++;
    
createDataFile($x$y$number$move$eX$eY$fX$fY$path$death$locCount$trapCount);
    
createMoveFile($move);
}

//If locate was requested last then now check to see if we fire
//        I check death here but really shouldn't have to since locate would not have been issued if death was false'
if ( $input == "LOCATE" && $death == )
{
    
$shoot 0;
    if ( 
$eX == $x )
    {
        if ( 
$eY $y )
        {
            
$move "DD";
            
$shoot 1;
        }
        else
        {
            
$move "DU";
            
$shoot 1;
        }
    }
    else if ( 
$eY == $y )
    {
        if ( 
$eX $x )
        {
            
$move "DR";
            
$shoot 1;
        }
        else
        {
            
$move "DL";
            
$shoot 1;
        }
    }    
    if ( 
$shoot == )
    {
        
$death 0;
        
createDataFile($x$y$number$move$eX$eY$fX$fY$path$death$locCount$trapCount);
        
createMoveFile($move);
    }
}
    
//default move if nothing else is valid
$number++;
if ( 
$x $fX )
{
    
$move "ML";
    
$x--;
}
else if ( 
$x$fX )
{
    
$move "MR";
    
$x++;
}
else
{
    
$key 1;
}
    
if ( 
$key == )
{
    if ( 
$y $fY )
    {
        
$move "MU";
        
$y--;
    }
    else if ( 
$y$fY )
    {
        
$move "MD";
        
$y++;
    }
}    
createDataFile($x$y$number$move$eX$eY$fX$fY$path$death$locCount$trapCount);
createMoveFile($move);


function 
figureMovement$a$b$mv )
{
    if ( 
$mv == "ML" )
    {
        
$a--;        
    }
    else if ( 
$mv == "MR" )
    {
        
$a++;
    }
    else if ( 
$mv == "MU" )
    {
        
$b--;
    }
    else if ( 
$mv == "MD" )
    {
        
$b++;
    }
    
$c $a "," $b;
    return (
$c);
}

function 
getData()
{
    
$inDatFile fopen ("data.txt""r");
    
$info trim(fgets ($inDatFile50));
    
fclose($inDatFile);
    return (
$info);
}

function 
createDataFile($x1$y1$number1$move1$eX1$eY1$fX1$fY1$path1$death1$locCount1$trapCount1)
{
    
$data $x1 "," $y1 "," $number1 "," $move1 "," $eX1 "," $eY1 "," .$fX1 "," $fY1 "," $path1 "," $death1 "," $locCount1 "," $trapCount1;
    
$outDatFile fopen('data.txt'"w+");
    
fputs($outDatFile, ($data) . "n");
    
fclose($outDatFile);    
    return;
}

function 
getInput()
{
    
    
$inFile fopen ("input.txt""r");
    
$info trim(fgets ($inFile50));
    
fclose($inFile);
    return (
$info);
}

function 
createMoveFile($mv)
{
    
$outFile fopen('move.txt'"w+");
    
fputs($outFile, ($mv) . "n");
    
fclose($outFile);    
    exit;
}



?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories