View on GitHub

Phpwar

code battle with php

Download this project as a .zip file Download this project as a tar.gz file
               _____  _    _ _____   __          __        
              |  __ \| |  | |  __ \  \ \        / /        
              | |__) | |__| | |__) |  \ \  /\  / /_ _ _ __ 
              |  ___/|  __  |  ___/    \ \/  \/ / _` | '__|
              | |    | |  | | |         \  /\  / (_| | |   
              |_|    |_|  |_|_|          \/  \/ \__,_|_|
              
          

Programming Your Player.

PHP War is a game where you put your code to play.

You develop a Player and go to the arena to test your algorithm. The winner is the one with more warriors in the arena when the last round finishes.

Here is the simplest Player your can create

namespace Iannsp\PhpWar\Player;
use Iannsp\PhpWar\Move as move;
use \Iannsp\PhpWar\Arena as Arena;

class SimplestPlayer implements playerInterface
{
    private $arenaWidth  = 0;
    private $arenaHeight = 0;
    public function __construct($width, $height)
    {
        $this->arenaWidth  = $width;
        $this->arenaHeight = $height;
    }
    public function play()
    {
        $arenaHeight = $this->arenaHeight;
        $arenaWidth  = $this->arenaWidth;
        // random strategy. Simple, isn't it? 
       return new move(rand(0, $arenaHeight), rand(0,$arenaWidth)); 
    }
    
    public function feedback($status)
    {
     /* 
      use it to state your play strategy, like save the result status
      of your move (win|lose), the positions of your warrior and...
      well, put your win algorithm here. ;)
     */
    }
}

The Arena

The arena is a simple board you need to dominate through putting warriors up to defeat your opponent.

There are two situations:

  1. Hit a position

    When you play the move(1,1) and the position 1,1 is empty, you win the position. If position 1,1 has an opponent then you win the position as well as reducing the army of your opponent.

  2. Hit a position with neighbors

    When you play the move(1,1) and the position 1,1 has neighbors. In this case the game analyzes the neighborhood, looking at the players and the number of warriors each one has. If your opponent's army in the neighborhood is bigger than yours, you lose. :(

How to Play?

You need to put your Player into a game, of course. And it's simple. You can find a sample of how to do this in playgame.php

Let me introduce you to the playgame.php script


require 'bootstrap.php';

// you can choose what strategies you are going to use to score in your game. 
// There are these two, but there will be more, I guarantee.

$scoreStrategy = array(
    'Hit'=>'\\Iannsp\PhpWar\\Game\\Score\\Hit',
    'Neibor'=>'\\Iannsp\PhpWar\\Game\\Score\\Neibor',
);

// create the arena. 
// You can set the dimensions of the area as big or small you like.
// I realize your play strategy depends on this.

$arena = new Iannsp\PhpWar\Arena(10,10, $scoreStrategy);

// This is the case when you play against yourself. ;) yes, you can.
$players = array (
new Iannsp\PhpWar\Player\P1($arena->getWidth(), $arena->getHeight()),
new Iannsp\PhpWar\Player\P1($arena->getWidth(), $arena->getHeight())
);

// finally, the game
$game = new Iannsp\PhpWar\Game($arena, $players);

// please take a look in the playgame.php to see this function.
// it renders in the bash terminal the status of the arena.

function render($game, $arena, $players)
{
 //...
}

// running how many rounds your prefer. 
// The next improvement to the game is to run rounds until find a winner 
// (dominate all positions in the arena).

echo "running a game\n";
$moves = 0;
while($moves < 100){
    $game->round();
    render($game, $arena, $players);
    $moves++;
}

echo "
 
                                              
    \nResult\n";
// here you get the stats of the game and know the score.
$stats = $arena->stats();
foreach ($stats as $id=>$stat){
    if ($id!=='.')
    echo "Player {$id} has {$stat} position(s).\n";
}

Authors and Contributors

This game is in its infancy, and I like to invite you to become part of it.

For now there is just me Ivo Nascimento

If you would like to send me a mail, please contact the batman at phpbr.org.