Permalink
Browse files

Minor README tweak. Added require_once to classes/stats.php for dice …

…reference. Updated unit tests for stats. Added CLI support for testing.
  • Loading branch information...
1 parent b40288f commit c80c8ba7596d5dd50207f747310516d254f0cafe @b-turchyn committed Apr 25, 2011
Showing with 284 additions and 11 deletions.
  1. +1 −3 README.textile
  2. +1 −1 src/classes/dice.php
  3. +214 −3 src/classes/stats.php
  4. +58 −0 test/classes/stats.php
  5. +10 −4 test/index.php
View
@@ -37,9 +37,7 @@ h1. Usage
h1. Support
-_CoC-CharGen_ is supported by the "I, Gamer Podcast":http://igamer.ca
-and "BrianTurchyn.net":http://brianturchyn.net . Support them by listening
-to the podcast or tossing them a couple bucks for a beer.
+_CoC-CharGen_ is supported by the "I, Gamer Podcast":http://igamer.ca and "BrianTurchyn.net":http://brianturchyn.net . Support them by listening to the podcast or tossing them a couple bucks for a beer. Beer has been known to "fuel programming productivity":http://xkcd.com/323/ !
h1. The Developers
View
@@ -195,4 +195,4 @@ function getSides() {
return $result;
}
} // END class DicePool
-?>
+?>
View
@@ -1,6 +1,217 @@
<?php
-
+require_once(dirname(__FILE__) . '/dice.php');
+/**
+ * StatGenerator class
+ * Uses a collection of dice pools to generate the basic stats for a
+ * character. Standard dice rules have been hard-coded.
+ *
+ * @package src
+ * @author Brian Turchyn
+ */
class StatGenerator {
-
+ private $strPool; // Strength
+ private $conPool; // Constitution
+ private $sizPool; // Size
+ private $dexPool; // Dexterity
+ private $appPool; // Appearance
+ private $sanPool; // Sanity
+ private $intPool; // Intelligence
+ private $powPool; // Power
+ private $eduPool; // Education
+
+ private $loaded = false; // Check for all valid dice pools.
+ /**
+ * Constructor
+ * Generates all needed dice pools
+ */
+ public function __construct() {
+
+ // 3d6 dice pools
+ $this->strPool = new DicePool(array(6,6,6),0);
+ $this->conPool = new DicePool(array(6,6,6),0);
+ $this->powPool = new DicePool(array(6,6,6),0);
+ $this->dexPool = new DicePool(array(6,6,6),0);
+ $this->appPool = new DicePool(array(6,6,6),0);
+
+ // 2d6+6 dice pools
+ $this->sizPool = new DicePool(array(6,6),6);
+ $this->intPool = new DicePool(array(6,6),6);
+
+ // 3d6+3 dice pools
+ $this->eduPool = new DicePool(array(6,6,6),3);
+
+ // Check if we're all valid
+ $this->loaded =
+ ( $this->strPool->isLoaded() && $this->conPool->isLoaded() &&
+ $this->powPool->isLoaded() && $this->dexPool->isLoaded() &&
+ $this->appPool->isLoaded() && $this->sizPool->isLoaded() &&
+ $this->intPool->isLoaded() && $this->eduPool->isLoaded() );
+ }
+
+ /**
+ * Rolls a set of stats by triggering the underlying dice pools.
+ *
+ * @return true on success, false on failure
+ * @author Brian Turchyn
+ */
+ public function roll() {
+ $result = false;
+ // Are we valid?
+ if($this->loaded) {
+ // Do the rolls!
+ $this->strPool->roll();
+ $this->conPool->roll();
+ $this->powPool->roll();
+ $this->dexPool->roll();
+ $this->appPool->roll();
+ $this->sizPool->roll();
+ $this->intPool->roll();
+ $this->eduPool->roll();
+ $result = true;
+ }
+ return $result;
+ }
+
+ /**
+ * Determines if the current stat pool is valid
+ * by checking the loaded variable.
+ *
+ * @return value of $this->loaded
+ * @author Brian Turchyn
+ */
+ public function isLoaded() {
+ return $this->loaded;
+ }
+
+ /**
+ * Getter for the value of the STR dice pool
+ *
+ * @return int a value between 3 and 18, false on failure
+ * @author Brian Turchyn
+ */
+ public function getSTR() {
+ return ($this->loaded ? $this->strPool->getResult() : false);
+ }
+
+ /**
+ * Getter for the value of the CON dice pool
+ *
+ * @return int a value between 3 and 18, false on failure
+ * @author Brian Turchyn
+ */
+ public function getCON() {
+ return ($this->loaded ? $this->conPool->getResult() : false);
+ }
+
+ /**
+ * Getter for the value of the POW dice pool
+ *
+ * @return int a value between 3 and 18, false on failure
+ * @author Brian Turchyn
+ */
+ public function getPOW() {
+ return ($this->loaded ? $this->powPool->getResult() : false);
+ }
+
+ /**
+ * Getter for the value of the DEX dice pool
+ *
+ * @return int a value between 3 and 18, false on failure
+ * @author Brian Turchyn
+ */
+ public function getDEX() {
+ return ($this->loaded ? $this->dexPool->getResult() : false);
+ }
+
+ /**
+ * Getter for the value of the APP dice pool
+ *
+ * @return int a value between 3 and 18, false on failure
+ * @author Brian Turchyn
+ */
+ public function getAPP() {
+ return ($this->loaded ? $this->appPool->getResult() : false);
+ }
+
+ /**
+ * Getter for the value of the SIZ dice pool
+ *
+ * @return int a value between 8 and 18, false on failure
+ * @author Brian Turchyn
+ */
+ public function getSIZ() {
+ return ($this->loaded ? $this->sizPool->getResult() : false);
+ }
+
+ /**
+ * Getter for the value of the INT dice pool
+ *
+ * @return int a value between 8 and 18, false on failure
+ * @author Brian Turchyn
+ */
+ public function getINT() {
+ return ($this->loaded ? $this->intPool->getResult() : false);
+ }
+
+ /**
+ * Getter for the value of the EDU dice pool
+ *
+ * @return int a value between 6 and 21, false on failure
+ * @author Brian Turchyn
+ */
+ public function getEDU() {
+ return ($this->loaded ? $this->eduPool->getResult() : false);
+ }
+
+ /**
+ * Getter for the value of SAN (ie. 5xPOW)
+ *
+ * @return int a value between 15 and 90, false on failure
+ * @author Brian Turchyn
+ */
+ public function getSAN() {
+ return ($this->loaded ? $this->powPool->getResult() * 5 : false);
+ }
+
+ /**
+ * Getter for the value of LUK (ie. 5xPOW)
+ *
+ * @return int a value between 15 and 90, false on failure
+ * @author Brian Turchyn
+ */
+ public function getLUK() {
+ return ($this->loaded ? $this->powPool->getResult() * 5 : false);
+ }
+
+ /**
+ * Getter for the value of IDEA (ie. 5xINT)
+ *
+ * @return int a value between 40 and 90, false on failure
+ * @author Brian Turchyn
+ */
+ public function getIDEA() {
+ return ($this->loaded ? $this->intPool->getResult() * 5 : false);
+ }
+
+ /**
+ * Getter for the value of KNOW (ie. 5xEDU)
+ *
+ * @return int a value between 30 and 105, false on failure
+ * @author Brian Turchyn
+ */
+ public function getKNOW() {
+ return ($this->loaded ? $this->eduPool->getResult() * 5 : false);
+ }
+
+ /**
+ * Getter for the derived value of hit points (ie. (STR + SIZ) / 2)
+ *
+ * @return int a value between 3 and 18, false on failure
+ */
+ public function getHP() {
+ return ($this->loaded ?
+ ($this->strPool->getResult() + $this->sizPool->getResult() ) / 2
+ : false);
+ }
}
-?>
+?>
View
@@ -0,0 +1,58 @@
+<?php
+require_once(dirname(__FILE__) . '/../simpletest/autorun.php');
+require_once(dirname(__FILE__) . '/../../src/classes/stats.php');
+
+/**
+ * TestOfStatGenerator class
+ * Test suite for the stat generator. Most testing is done in the DicePool
+ * and SingleDie test suites.
+ *
+ * @package test
+ * @author Brian Turchyn
+ */
+
+class TestOfStatGenerator extends UnitTestCase {
+ function testForValidConstructor() {
+ $stats = new StatGenerator();
+ $this->assertTrue($stats->isLoaded());
+ }
+
+ function testMultiStatTest() {
+ for($i = 1;$i <= 50; $i++)
+ $this->statsInRange();
+ }
+
+ private function statsInRange() {
+ $stats = new StatGenerator();
+ $stats->roll();
+
+ // STR
+ $this->assertWithinMargin(3,$stats->getSTR(), 18);
+ // CON
+ $this->assertWithinMargin(3,$stats->getCON(), 18);
+ // POW
+ $this->assertWithinMargin(3,$stats->getPOW(), 18);
+ // DEX
+ $this->assertWithinMargin(3,$stats->getDEX(), 18);
+ // APP
+ $this->assertWithinMargin(3,$stats->getAPP(), 18);
+ // SIZ
+ $this->assertWithinMargin(3,$stats->getSIZ(), 18);
+ // INT
+ $this->assertWithinMargin(8,$stats->getINT(), 18);
+ // EDU
+ $this->assertWithinMargin(3,$stats->getEDU(), 18);
+ // SAN
+ $this->assertWithinMargin(15,$stats->getSAN(), 90);
+ // LUK
+ $this->assertWithinMargin(15,$stats->getLUK(), 90);
+ // IDEA
+ $this->assertWithinMargin(40,$stats->getIDEA(), 90);
+ // KNOW
+ $this->assertWithinMargin(30,$stats->getKNOW(), 105);
+ // HP
+ $this->assertWithinMargin(3,$stats->getHP(), 18);
+ }
+}
+
+?>
View
@@ -1,7 +1,13 @@
<?php
-if(isset($_POST['coc-chargen-test_all'])) {
- foreach (glob("classes/*.php") as $filename)
+function runTests() {
+ return (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR']))
+ || isset($_POST['coc-chargen-test-all']);
+}
+
+if(runTests()) {
+ echo "Running tests";
+ foreach (glob(dirname(__FILE__) . "/classes/*.php") as $filename)
{
require_once($filename);
}
@@ -10,8 +16,8 @@
else {
?>
<form method="POST">
-<input type="hidden" value="yes" name="coc-chargen-test_all" /><input type="submit" value="Run all tests" />
+<input type="hidden" value="yes" name="coc-chargen-test-all" /><input type="submit" value="Run all tests" />
</form>
<?php
}
-?>
+?>

0 comments on commit c80c8ba

Please sign in to comment.