i working on small dice game fun , dont understand how solve followig problem:
the problem everytime roll dice should input number (only number 1)
and once done, program output svg image of dice displaying random number ranging between 1 , 6. arbitrary number of runs, until reach dice value 1. @ point game terminate, , total score evaluated.
the problem have program evaluates latest point acquired. can´t seem fix though.
the form
$form = <<< eod <div> <form action='{$_server['php_self']}' method='get'> <fieldset> <input type='text' name='antal' value='{$antal}' /> <button type='submit'>kasta</button> </fieldset </form> <a href='{$_server['php_self']}'>starta om</a> </div> eod; the functions random dice roll
class cdice { private $ilastthrows = array(); public function roll() { return rand(1,6); } public function rollrepeatedly($anumber) { $this->ilastthrows = array(); for($i=1; $i<=$anumber; $i++) { $this->ilastthrows[$i] = $this->roll(); } return $this->ilastthrows; } public function getlastthrows() { return $this->ilastthrows; } } this function displays correct svg dice, removed svg code. each getdice1, 2, 3 correspond function containing svg code. public function getsvg($adice) {
$html = $this->getsvgheader(); switch($adice) { case '1': $html .= $this->getdice1(); break; case '2': $html .= $this->getdice2(); break; case '3': $html .= $this->getdice3(); break; case '4': $html .= $this->getdice4(); break; case '5': $html .= $this->getdice5(); break; case '6': $html .= $this->getdice6(); break; } $html .= "</svg>"; return $html; } this how evaluating total, not working correctly.
public function total($anarray) { foreach($anarray $key => $value){ return array_sum($anarray); } } $antal = filter_input(input_get, 'antal', filter_validate_int); if($antal) { $dice->rollrepeatedly($antal); //$slag = $dice->ilastthrows; $slag = $dice->getlastthrows(); $total = $histogram->total($slag, 1); // show dices svg foreach($slag $key => $value) { $dicehtml .= $dice->getsvg($value); } // present result in text' lastly check value make sure once reaches 1, end program
if($value != 1){ $html {$form} {$dicehtml} } else{ $html = <<<eod <div> <p>game over.</p> <p>you threw one.</p> <p>total points: {$total}</p> <div> {$dicehtml} </div> eod;
Comments
Post a Comment