-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) | ||
<!-- and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). --> | ||
Semantic Versioning cannot be used as the exercise instructions specify final rev as 2.0.x | ||
|
||
## [Unreleased] | ||
- Create and add a UML-diagram for Dice100 classes | ||
|
||
## [v2.0.3] - 2018-04-22 | ||
### Added | ||
- This CHANGELOG file | ||
- GameInit function to initialize default game settings for Dice100 | ||
- Game winner to show who won the game | ||
- stopGame session variable to inhibit further throws after win | ||
- checkbox for viewing $_SESSION | ||
- Stop function | ||
- Calcualtion of round and total score. | ||
- Player switch. | ||
- Selecting number of dices | ||
- Creating game, functions for throw, reset, display dices results. | ||
- Route and view for Dice100 | ||
|
||
### Changed | ||
- Moved version info to CHANGELOG. README now contains general project info | ||
- Made doDebug persistent | ||
- Corrected game calcualations | ||
- Navbar to incorporate link for game Dice100 | ||
|
||
## [v2.0.2] - 2018-04-13 | ||
### Added | ||
- GET, POST and SESSION versions of the Guess game | ||
- Built new documentation including Guess game | ||
|
||
### Changed | ||
- Navbar to incorporate link for game Gissa | ||
|
||
## [v2.0.1] - 2018-04-09 | ||
### Added | ||
- phpDocumentor | ||
- Built documentation | ||
|
||
### Changed | ||
- Moved bootstrap stylesheet to Page.php | ||
|
||
## [v1.0.x] - 2018-04-08 | ||
### Added | ||
- Report text | ||
- Styling | ||
- Bootstrap stylesheets. | ||
kmom01 finished. | ||
|
||
2018-04-06 Project init |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,9 @@ | ||
My redovisa in oophp | ||
==================== | ||
# My redovisa in oophp-v4 | ||
|
||
This is a project repo for course oophp at BTH (bth.se). | ||
Author: thba17 | ||
|
||
v2.0.2 | ||
2018-04-13 Added GET, POST and SESSION versions of the Guess game | ||
Built new documentation including Guess game | ||
## Built With | ||
* [Anax-flat](https://github.com/canax/anax-flat) - The web framework used | ||
|
||
v2.0.1 | ||
2018-04-09 Moved bootstrap stylesheet to Page.php | ||
Added phpDocumentor | ||
Built documentation | ||
|
||
v1.0.x | ||
2018-04-08 Added report text, styling and bootstrap stylesheets. | ||
kmom01 finished. | ||
2018-04-06 Project init | ||
## Author | ||
* **thba17 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Tärningsspelet 100 | ||
=========================== | ||
|
||
Spelet spelas med en eller flera tärningar och det gäller att samla ihop poäng för att komma först till 100. | ||
|
||
* [Tärningsspelet 100](dice100/dice100) |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
namespace Thba17\Dice100; | ||
|
||
/** | ||
* A dice throwing class | ||
*/ | ||
class Dice | ||
{ | ||
private $sides; | ||
private $throws; | ||
private $result = []; | ||
|
||
/** | ||
* Public function to throw the Dice | ||
* | ||
* @param integer $throws Number of times to throw dice. Default 6 for standard dice. | ||
* @return array An array containing each dice result for the series. | ||
*/ | ||
public function throwDice(int $throws = 6) | ||
{ | ||
$this->throws = $throws; | ||
|
||
if ($throws < 1) { | ||
throw new DiceException("The dice must be thrown at least one (1) time."); | ||
} | ||
|
||
for ($i = 1; $i <= $this->throws; $i++) { | ||
$this->result[$i] = rand(1, $this->sides); | ||
} | ||
return $this->result; | ||
} | ||
|
||
/** | ||
* Constructor for class dice | ||
* | ||
* This is the constructor for the Dice class. It will take as in parameter | ||
* the number of times the dice should be thrown. | ||
* | ||
*/ | ||
public function __construct(int $sides = 6) | ||
{ | ||
$this->sides = $sides; | ||
// echo __METHOD__ . "\n"; | ||
} | ||
|
||
/** | ||
* Destructor for class Dice | ||
*/ | ||
public function __destruct() | ||
{ | ||
// echo __METHOD__ . "\n"; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
namespace Thba17\Dice100; | ||
|
||
/** | ||
* Exception class for DiceException. | ||
*/ | ||
class DiceException extends \Exception | ||
{ | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
namespace Thba17\Dice100; | ||
|
||
/** | ||
* A graphic dice. | ||
*/ | ||
class DiceGraphic extends DiceHand | ||
{ | ||
const SIDES = 6; | ||
// const DICES = 5; | ||
private $dices; | ||
|
||
|
||
/** | ||
* gameInit A function to initialize the game object | ||
* | ||
* @return void | ||
*/ | ||
public function gameInit() | ||
{ | ||
$_SESSION["game"]["disabled"] = "disabled"; | ||
$_SESSION["game"]["stopGame"] = $_SESSION["game"]["stopGame"] ?? null; | ||
$_SESSION["game"]["player"] = $_SESSION["game"]["player"] ?? "Visitor"; | ||
$_SESSION["game"]["winner"] = $_SESSION["game"]["winner"] ?? null; | ||
$_SESSION["game"]["Visitor"]["values"] = $_SESSION["game"]["Visitor"]["values"] ?? 0; | ||
$_SESSION["game"]["Visitor"]["roundSum"] = $_SESSION["game"]["Visitor"]["roundSum"] ?? 0; | ||
$_SESSION["game"]["Visitor"]["roundTotal"] = $_SESSION["game"]["Visitor"]["roundTotal"] ?? 0; | ||
$_SESSION["game"]["Visitor"]["grandTotal"] = $_SESSION["game"]["Visitor"]["grandTotal"] ?? 0; | ||
$_SESSION["game"]["Computer"]["values"] = $_SESSION["game"]["Computer"]["values"] ?? 0; | ||
$_SESSION["game"]["Computer"]["roundSum"] = $_SESSION["game"]["Computer"]["roundSum"] ?? 0; | ||
$_SESSION["game"]["Computer"]["roundTotal"] = $_SESSION["game"]["Computer"]["roundTotal"] ?? 0; | ||
$_SESSION["game"]["Computer"]["grandTotal"] = $_SESSION["game"]["Computer"]["grandTotal"] ?? 0; | ||
} | ||
|
||
/** | ||
* playComputer A function to roll dices for the computer. Called from | ||
* src/route/dice100.php | ||
* | ||
* @return void | ||
*/ | ||
public function playComputer() | ||
{ | ||
// echo "In playComputer"; | ||
$_SESSION["game"]["Computer"]["roundSum"] = 0; | ||
$_SESSION["game"]["Computer"]["roundTotal"] = 0; | ||
$this->roll(); | ||
$_SESSION["game"]["Computer"]["values"] = $this->values(); | ||
|
||
if (!array_search(1, $_SESSION["game"]["Computer"]["values"])) { | ||
$_SESSION["game"]["Computer"]["roundSum"] = $this->sum(); | ||
$_SESSION["game"]["Computer"]["roundTotal"] += $_SESSION["game"]["Computer"]["roundSum"]; | ||
if (($_SESSION["game"]["Computer"]["grandTotal"] + $_SESSION["game"]["Computer"]["roundTotal"]) >= 100) { | ||
$_SESSION["game"]["winner"] = $_SESSION["game"]["player"]; | ||
$_SESSION["game"]["stopGame"] = "disabled"; | ||
} | ||
$_SESSION["game"]["Computer"]["grandTotal"] += $_SESSION["game"]["Computer"]["roundTotal"]; | ||
} | ||
} | ||
|
||
/** | ||
* Constructor to initiate five dices with six number of sides. | ||
* | ||
* @param integer $dices Number of dices to use in the game | ||
*/ | ||
public function __construct(int $dices = 1) | ||
{ | ||
$this->dices = $dices; | ||
parent::__construct($this->dices, self::SIDES); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
namespace Thba17\Dice100; | ||
|
||
/** | ||
* Class for DiceHand | ||
*/ | ||
class DiceHand | ||
{ | ||
private $dices; | ||
private $sides; | ||
private $results = []; | ||
private $value = []; | ||
|
||
/** | ||
* values A function to return an array containing all dice values for one throw | ||
* @return array Containing the values of the thrown dices | ||
*/ | ||
public function values() | ||
{ | ||
return $this->results; | ||
} | ||
|
||
/** | ||
* sum A function to return the sum of all dice values for one throw | ||
* @return int The sum of all dices thrown | ||
*/ | ||
public function sum() | ||
{ | ||
return array_sum($this->results); | ||
} | ||
|
||
/** | ||
* average The average value of all dices thrown rounded to two decimals | ||
* @return int The rounded average of the dices thrown | ||
*/ | ||
public function average() | ||
{ | ||
return round($this->sum()/count($this->results), 2); | ||
} | ||
|
||
/** | ||
* roll A function to roll the dices. The dice values are stored in an array. | ||
* @return void | ||
*/ | ||
public function roll() | ||
{ | ||
$game = new Dice($this->sides); | ||
for ($i = 1; $i <= $this->dices; $i++) { | ||
$this->value = $game->throwDice(1); | ||
$this->results[$i] = $this->value[1]; | ||
} | ||
} | ||
|
||
/** | ||
* Constructor for class DiceHand | ||
* | ||
* This is the constructor for the Dice class. It will take as in parameter | ||
* the number of times the dice should be thrown. | ||
* | ||
* @param integer $dices The number of dices to throw. | ||
* @param integer $sides The number of sides of the dices. | ||
*/ | ||
public function __construct(int $dices = 5, int $sides = 6) | ||
{ | ||
if ($dices < 1 || $sides < 2) { | ||
throw new DiceException("We need at least one (1) dice with at least 2 sides."); | ||
} | ||
$this->dices = $dices; | ||
$this->sides = $sides; | ||
// echo __METHOD__ . "\n"; | ||
} | ||
|
||
/** | ||
* Destructor for class Dice | ||
*/ | ||
public function __destruct() | ||
{ | ||
// echo __METHOD__ . "\n"; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
/** | ||
* App specific routes. | ||
*/ | ||
|
||
/** | ||
* This route is for the game " Dice 100" | ||
*/ | ||
$app->router->any(["GET", "POST"], "dice100/dice100", function () use ($app) { | ||
$data = [ | ||
"title" => "Tärningsspelet 100", | ||
]; | ||
|
||
// Check if user requests a new game | ||
if (isset($_POST['doRestart'])) { | ||
unset($_SESSION['game']); | ||
} | ||
|
||
// Get incoming | ||
if (isset($_POST['doSetDices'])) { | ||
$_SESSION["game"]["dices"] = $_POST["dices"]; | ||
} | ||
$dices = isset($_SESSION["game"]["dices"]) ? $_SESSION["game"]["dices"] : 0; | ||
$player = $_SESSION["game"]["player"] ?? "Visitor"; | ||
|
||
// Set session variables | ||
$game = isset($_SESSION["game"]["game"]) ? $_SESSION["game"]["game"] : null; | ||
|
||
if (isset($_SESSION["game"]["dices"]) && ($dices > 0)) { | ||
//Dices selected, lock field, create game | ||
$game = new \Thba17\Dice100\DiceGraphic($_SESSION["game"]["dices"]); | ||
$game->gameInit(); | ||
$_SESSION["game"]["game"] = $game; | ||
} else { | ||
// No dices selected, unlock field | ||
$_SESSION["game"]["disabled"] = ""; | ||
} | ||
|
||
// Player rolls dices. Calculate sum of round. | ||
if (isset($_POST['doThrow'])) { | ||
$player = $_SESSION["game"]["player"]; | ||
$game->roll(); | ||
$_SESSION["game"]["Visitor"]["values"] = $game->values(); | ||
|
||
if (array_search(1, $_SESSION["game"]["Visitor"]["values"])) { | ||
$_SESSION["game"][$player]["roundSum"] = 0; | ||
$_SESSION["game"][$player]["roundTotal"] = 0; | ||
if ($player == "Visitor") { | ||
$_SESSION["game"]["player"] = "Computer"; | ||
$game->playComputer(); | ||
$_SESSION["game"]["player"] = "Visitor"; | ||
} | ||
} else { | ||
$_SESSION["game"][$player]["roundSum"] = $game->sum(); | ||
$_SESSION["game"][$player]["roundTotal"] += $_SESSION["game"][$player]["roundSum"]; | ||
if (($_SESSION["game"][$player]["grandTotal"] + $_SESSION["game"][$player]["roundTotal"]) >= 100) { | ||
// $_SESSION["game"][$player]["grandTotal"] += $_SESSION["game"][$player]["roundSum"]; | ||
$_SESSION["game"]["winner"] = $_SESSION["game"]["player"]; | ||
$_SESSION["game"]["stopGame"] = "disabled"; | ||
} | ||
} | ||
} | ||
|
||
// Player stops the current round. Switch to next player. Update player score. | ||
if (isset($_POST['doStop'])) { | ||
//Stop current round and add round result to total. | ||
$player = $_SESSION["game"]["player"]; | ||
$_SESSION["game"][$player]["grandTotal"] += $_SESSION["game"][$player]["roundTotal"]; | ||
$_SESSION["game"][$player]["roundSum"] = 0; | ||
$_SESSION["game"][$player]["roundTotal"] = 0; | ||
if ($player == "Visitor") { | ||
$_SESSION["game"]["player"] = "Computer"; | ||
$game->playComputer(); | ||
$_SESSION["game"]["player"] = "Visitor"; | ||
} | ||
} | ||
|
||
// Add to $data array | ||
$data += [ | ||
"game" => $game, | ||
"player" => $player, | ||
"dices" => $dices, | ||
]; | ||
|
||
// Add view and render page | ||
$app->view->add("dice100/dice100", $data); | ||
$app->page->render($data); | ||
}); | ||
|