PHP

Overview

The version of PHP supported is PHP 7.4.1

Each turn your source code will be compiled within a new sandbox, by default no information is shared or persisted between turns, if you wish to persist data then you should use the User Data field within the API.


Input - Standard Input (STDIN)

Within PHP you can use fopen() to capture the STDIN input

// Load in the map state
$stdin = fopen( 'php://stdin', 'r' );
$input = "";
while( $line = fgets( $stdin ) ) {
  $input .= $line;
}
fclose( $stdin );

Ouput - Standard Outout (STDOUT)

Within PHP you can simply use echo() to return your CSV based instructions to BotWars.

// Return our instructions to the game
echo($output);

Example Bot

The example bot will simply instruct it's robots to move in random directions around the map, it's just to show how the input and output works within PHP

<?php

// Load in the map state
$stdin = fopen( 'php://stdin', 'r' );
$input = "";
while( $line = fgets( $stdin ) ) {
  $input .= $line;
}
fclose( $stdin );
$gameData = explode("#", $input);
$bots = explode(",", $gameData[1]);
$output = "";

// Loop through all the bots
foreach($bots as $nextBot){
    $botInfo = explode("-", $nextBot);

    if($botInfo[0] == "F")
    {
        // We can control this bot, as it's a friend
        if(strlen($output) > 0) $output .= ",";
        $output .= $botInfo[1] . "-M-" . generateRandomDirection();
    }
}

// Return our instructions to the game
echo($output);

function generateRandomDirection() {
    $characters = array ("N", "E", "S", "W") ;
    $index = array_rand($characters, 1);
    return $characters[array_rand($characters, 1)];
}

?>
❤️ Feedback