Skip to content

Commit

Permalink
Fix re-generation with same mapHash
Browse files Browse the repository at this point in the history
  • Loading branch information
A1essandro committed Dec 21, 2015
1 parent 8b50633 commit 7bce7c6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/DiamondAndSquare.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ public function generate()

$this->divide($this->size);

$this->setMapHash($this->mapHash);

return $this->terra;
}

Expand Down Expand Up @@ -224,7 +226,7 @@ private function getOffset($stepSize)
//update hash for new "random" value
$this->stepHash = md5($this->stepHash);
//calculate value from hash (from 0 to $maxOffset)
$rand = intval(substr(md5($this->stepHash), -7), 16) % $maxOffset;
$rand = intval(substr($this->stepHash, -8), 16) % $maxOffset;

return (float)$stepSize / $this->size * $rand;
}
Expand Down
25 changes: 21 additions & 4 deletions test/DiamondAndSquareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function testSetInvalidMapHash($mapHash)

public function testHashEquals()
{
$this->diamondSquare->setSize(4);
$this->diamondSquare->setSize(1);
$this->diamondSquare->setPersistence(100);

//same hashes
Expand All @@ -106,18 +106,23 @@ public function testHashEquals()
$map1 = $this->diamondSquare->generate();
$map2 = $this->diamondSquare->generate();

$this->assertEquals($map1, $map2);
$this->assertEquals(self::expandMap($map1), self::expandMap($map2));
}

//different hashes
public function testDifferentHashes()
{
$mapHash1 = uniqid() . '1';
$mapHash2 = uniqid() . '2';

$this->diamondSquare->setSize(3);
$this->diamondSquare->setPersistence(100);

$this->diamondSquare->setMapHash($mapHash1);
$map1 = $this->diamondSquare->generate();
$this->diamondSquare->setMapHash($mapHash2);
$map2 = $this->diamondSquare->generate();

$this->assertNotEquals($map1, $map2);
$this->assertNotEquals(self::expandMap($map1), self::expandMap($map2));
}

/**
Expand Down Expand Up @@ -147,4 +152,16 @@ public function testContains()

#endregion

private static function expandMap($map)
{
$expandPoints = array();
foreach ($map as $line) {
foreach ($line as $point) {
$expandPoints[] = $point;
}
}

return $expandPoints;
}

}

0 comments on commit 7bce7c6

Please sign in to comment.