Browse Source

1st commit

master
l4p1n 8 months ago
commit
0642fa2b0e
Signed by: l4p1n <l4p1n@l4p1n.ch> GPG Key ID: 8A2C2F68B58DC35C
7 changed files with 242 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +68
    -0
      Lib/Ant.php
  3. +30
    -0
      Lib/Cell.php
  4. +42
    -0
      Lib/Coordinates.php
  5. +75
    -0
      Lib/Map.php
  6. +19
    -0
      composer.json
  7. +6
    -0
      generate_map.php

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
vendor
map.txt

+ 68
- 0
Lib/Ant.php View File

@@ -0,0 +1,68 @@
<?php

namespace Lib;


class Ant {

const STEP_BOTTOM = 1;
const STEP_UP = 2;
const STEP_LEFT = 3;
const STEP_RIGHT = 4;

private $remaining_steps = 50;
private $coordinates;
private $map;

public function __construct(Coordinates $coordinates, Map $map, int $remaining_steps = 15) {
$this->coordinates = $coordinates;
$this->remaining_steps = $remaining_steps;
$this->map = $map;
}

public function step(){
$this->remaining_steps--;

if($this->remaining_steps == 0){
throw new \OutOfBoundsException('Ant is dead');
}

// Decide where we're going

do{
$direction = random_int(1, 4);
assert($direction >= 1 && $direction <= 4);

switch ($direction) {
case self::STEP_BOTTOM:
$new_coord = $this->coordinates->down();
break;

case self::STEP_UP:
$new_coord = $this->coordinates->up();
break;

case self::STEP_LEFT:
$new_coord = $this->coordinates->left();
break;

case self::STEP_RIGHT:
$new_coord = $this->coordinates->right();
break;

default:
throw new \OutOfRangeException('Unexpected ant move n°' . $direction);
}
echo 'Requesting ' . $new_coord . PHP_EOL;

$valid_direction = $this->map->valid_coordinates($new_coord);
}while(!$valid_direction);

// Set new location
$this->coordinates = $new_coord;

// Get cell and change internal state
$cell = $this->map->get_cell($new_coord);
$cell->setType(Cell::CELL_TYPE_WATER);
}
}

+ 30
- 0
Lib/Cell.php View File

@@ -0,0 +1,30 @@
<?php

namespace Lib;


class Cell {

const CELL_TYPE_GRASS = '.';
const CELL_TYPE_WATER = 'W';

private $coordinates;
private $type = self::CELL_TYPE_GRASS;

public function __construct(Coordinates $coordinates, $cell_type = self::CELL_TYPE_GRASS) {
$this->coordinates = $coordinates;
$this->type = $cell_type;
}

public function getCoordinates(): Coordinates {
return $this->coordinates;
}

public function getType(): string {
return $this->type;
}

public function setType(string $type) {
$this->type = $type;
}
}

+ 42
- 0
Lib/Coordinates.php View File

@@ -0,0 +1,42 @@
<?php

namespace Lib;


class Coordinates {
protected $x;
protected $y;

public function __construct(int $x, int $y) {
$this->x = $x;
$this->y = $y;
}

public function up(): self {
return new self($this->x, $this->y + 1);
}

public function down(): self {
return new self($this->x, $this->y - 1);
}

public function left(): self {
return new self($this->x - 1, $this->y);
}

public function right(): self {
return new self($this->x + 1, $this->y);
}

public function getX(): int {
return $this->x;
}

public function getY(): int {
return $this->y;
}

public function __toString() {
return '(' . $this->x . ';' . $this->y . ')';
}
}

+ 75
- 0
Lib/Map.php View File

@@ -0,0 +1,75 @@
<?php

namespace Lib;


class Map {
/**
* @var Cell[][]
*/
protected $cells = [];
protected $width = 0;
protected $height = 0;

public function __construct(int $width, int $height) {
$this->width = $width;
$this->height = $height;

for($i = 0; $i < $this->width; $i++){
$this->cells[$i] = [];

for($j = 0; $j < $this->height; $j++){
$this->cells[$i][$j] = new Cell(new Coordinates($i, $j));
}
}
}

public function generate(int $ants_count = 1){
$ants = [];
for($i = 0; $i < $ants_count; $i++){
$ants[] = new Ant(new Coordinates(random_int(0, $this->width), random_int(0, $this->height)), $this);
}

$alive_ants = $ants_count;

while($alive_ants > 0){
foreach($ants as $i => $ant){
try{
$ant->step();
}catch (\OutOfBoundsException $e){
unset($ants[$i]);
$alive_ants--;
}
}
}
}

public function valid_coordinates(Coordinates $coordinates): bool {
return
($coordinates->getX() > 0 && $coordinates->getX() < $this->width)
&& ($coordinates->getY() > 0 && $coordinates->getY() < $this->height)
;
}

public function get_cell(Coordinates $coordinates): Cell {
if(!$this->valid_coordinates($coordinates)){
throw new \OutOfBoundsException('Asked cell is outside the map');
}

return $this->cells[$coordinates->getX()][$coordinates->getY()];
}

public function save($filename){
$map_str = '';

foreach($this->cells as $row){
foreach ($row as $cell){
$map_str .= $cell->getType();
}

$map_str .= PHP_EOL;
}

file_put_contents($filename, $map_str);
}
}

+ 19
- 0
composer.json View File

@@ -0,0 +1,19 @@
{
"name": "l4p1n/mappingstuff",
"license": "no",
"authors": [
{
"name": "l4p1n",
"email": "l4p1n@l4p1n.ch"
}
],
"require": {

},

"autoload": {
"psr-4": {
"Lib\\": "Lib"
}
}
}

+ 6
- 0
generate_map.php View File

@@ -0,0 +1,6 @@
<?php
require_once 'vendor/autoload.php';

$map = new \Lib\Map(50, 50);
$map->generate(10);
$map->save('map.txt');

Loading…
Cancel
Save