Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Monster{
- private $health = 0;
- function __construct($health) {
- if(!is_numeric($health)){
- throw new Exception('Non numeric health');
- }
- $this->health = $health;
- }
- public function __toString() {
- return (string)$this->health;
- }
- public function add($ammount){
- if(!is_numeric($ammount)){
- throw new Exception('Non numeric ammount');
- }
- $this->health += $ammount;
- return $this->health;
- }
- public function subtract($ammount){
- if(!is_numeric($ammount)){
- throw new Exception('Non numeric ammount');
- }
- $this->health -= $ammount;
- if($this->health <= 0){
- $this->health = 0;
- }
- return $this->health;
- }
- }
- class Encounter{
- private $monsters = array();
- function __construct() {
- $args = func_num_args();
- for($i = 0; $i < $args; $i++){
- $this->create_monster(func_get_arg($i));
- }
- }
- public function __toString() {
- $value = "Monster List\n";
- $number = 0;
- foreach($this->monsters as &$monster){
- $value .= "Monster : $number | health: $monster \n";
- $number ++;
- }
- return (string)$value;
- }
- function create_monster($health){
- try{
- array_push($this->monsters, new Monster($health));
- echo "Monster created!\n";
- }
- catch(Exception $e){
- echo 'Monster could not be created: '.$e->getMessage()." \n";
- }
- }
- function remove_monster($monster_alias){
- if($this->exists($monster_alias)){
- unset($this->monsters[$monster_alias]);
- $this->monsters = array_values($this->monsters);
- echo "Monster $monster_alias removed\n";
- }
- echo $this;
- }
- function add($monster_alias, $ammount){
- if($this->exists($monster_alias)){
- try{
- $health = $this->monsters[$monster_alias]->add($ammount);
- echo "Monster $monster_alias gained $ammount health and now has $health hp\n";
- }
- catch(Exception $e){
- echo 'Health could not be added: '.$e->getMessage()." \n";
- }
- }
- }
- function subtract($monster_alias, $ammount){
- if($this->exists($monster_alias)){
- try{
- $health = $this->monsters[$monster_alias]->subtract($ammount);
- if($health == 0){
- echo "Monster $monster_alias has died!\n";
- $this->remove_monster($monster_alias);
- }
- else{
- echo "Monster $monster_alias lost $ammount health and has $health hp remaining\n";
- }
- }
- catch(Exception $e){
- echo 'Health could not be subtracted: '.$e->getMessage().' \n';
- }
- }
- }
- private function exists($monster_alias){
- if(!is_numeric($monster_alias) || count($this->monsters) <= $monster_alias || $monster_alias < 0){
- echo "Invalid number! Monster \"$monster_alias\" does not exist\n";
- return false;
- }
- return true;
- }
- }
- class Interpreter{
- private $encounter;
- public function __construct($encounter){
- $this->encounter = $encounter;
- $this->echo_commands();
- }
- function interpret($cmd){
- switch($cmd){
- case 'monster list':
- echo $this->encounter;
- break;
- case 'command list':
- $this->echo_commands();
- break;
- case 'create':
- echo 'health: ';
- $health = trim(fgets(STDIN));
- $this->encounter->create_monster($health);
- break;
- case 'remove':
- echo 'monster number: ';
- $number = trim(fgets(STDIN));
- $this->encounter->remove_monster($number);
- break;
- case 'add':
- echo 'monster number: ';
- $number = trim(fgets(STDIN));
- echo 'ammount to add: ';
- $ammount = trim(fgets(STDIN));
- $this->encounter->add($number, $ammount);
- break;
- case 'sub':
- echo 'monster number: ';
- $number = trim(fgets(STDIN));
- echo 'ammount to subtract: ';
- $ammount = trim(fgets(STDIN));
- $this->encounter->subtract($number, $ammount);
- break;
- case 'exit':
- exit();
- break;
- default:
- "Invalid command!\n";
- }
- }
- function echo_commands(){
- echo "\nmonster list\ncommand list\ncreate\nremove\nadd\nsub\nexit\n";
- }
- }
- //PROGRAM BEGINS
- echo "WELCOME\n";
- $encounter = new Encounter(200, 300, 400, 500);
- $interpreter = new Interpreter($encounter);
- while(true){
- $interpreter->interpret(trim(fgets(STDIN)));
- }
Add Comment
Please, Sign In to add comment