Guest User

Untitled

a guest
May 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.10 KB | None | 0 0
  1. <?php
  2.  
  3. class Monster{
  4.     private $health = 0;
  5.    
  6.     function __construct($health) {
  7.         if(!is_numeric($health)){
  8.              throw new Exception('Non numeric health');
  9.         }
  10.         $this->health = $health;
  11.     }
  12.    
  13.     public function __toString() {
  14.         return (string)$this->health;
  15.     }
  16.    
  17.     public function add($ammount){
  18.         if(!is_numeric($ammount)){
  19.              throw new Exception('Non numeric ammount');
  20.         }
  21.         $this->health += $ammount;
  22.         return $this->health;
  23.     }
  24.    
  25.     public function subtract($ammount){
  26.         if(!is_numeric($ammount)){
  27.              throw new Exception('Non numeric ammount');
  28.         }
  29.         $this->health -= $ammount;
  30.         if($this->health <= 0){
  31.             $this->health = 0;
  32.         }
  33.         return $this->health;
  34.     }
  35. }
  36.  
  37. class Encounter{
  38.     private $monsters = array();
  39.    
  40.     function __construct() {
  41.         $args = func_num_args();
  42.         for($i = 0; $i < $args; $i++){
  43.             $this->create_monster(func_get_arg($i));
  44.         }
  45.     }
  46.    
  47.     public function __toString() {
  48.         $value = "Monster List\n";
  49.         $number = 0;
  50.         foreach($this->monsters as &$monster){
  51.              $value .= "Monster : $number | health: $monster \n";
  52.              $number ++;
  53.         }
  54.         return (string)$value;
  55.     }
  56.    
  57.     function create_monster($health){
  58.         try{
  59.             array_push($this->monsters, new Monster($health));
  60.             echo "Monster created!\n";
  61.         }
  62.         catch(Exception $e){
  63.             echo 'Monster could not be created: '.$e->getMessage()." \n";
  64.            
  65.         }
  66.     }
  67.    
  68.     function remove_monster($monster_alias){
  69.         if($this->exists($monster_alias)){
  70.             unset($this->monsters[$monster_alias]);
  71.             $this->monsters = array_values($this->monsters);
  72.             echo "Monster $monster_alias removed\n";
  73.         }
  74.         echo $this;
  75.     }
  76.    
  77.     function add($monster_alias, $ammount){
  78.         if($this->exists($monster_alias)){
  79.             try{
  80.                 $health = $this->monsters[$monster_alias]->add($ammount);
  81.                 echo "Monster $monster_alias gained $ammount health and now has $health hp\n";
  82.             }
  83.             catch(Exception $e){
  84.                 echo 'Health could not be added: '.$e->getMessage()." \n";
  85.             }
  86.         }
  87.     }
  88.    
  89.     function subtract($monster_alias, $ammount){
  90.         if($this->exists($monster_alias)){
  91.             try{
  92.                 $health = $this->monsters[$monster_alias]->subtract($ammount);
  93.                 if($health == 0){
  94.                     echo "Monster $monster_alias has died!\n";
  95.                     $this->remove_monster($monster_alias);
  96.                 }
  97.                 else{
  98.                     echo "Monster $monster_alias lost $ammount health and has $health hp remaining\n";
  99.                 }
  100.             }
  101.             catch(Exception $e){
  102.                 echo 'Health could not be subtracted: '.$e->getMessage().' \n';
  103.             }
  104.         }
  105.     }
  106.    
  107.     private function exists($monster_alias){
  108.         if(!is_numeric($monster_alias) || count($this->monsters) <= $monster_alias || $monster_alias < 0){
  109.             echo "Invalid number! Monster \"$monster_alias\" does not exist\n";
  110.             return false;
  111.         }
  112.         return true;
  113.     }
  114. }
  115.  
  116. class Interpreter{
  117.  
  118.     private $encounter;
  119.    
  120.     public function __construct($encounter){
  121.         $this->encounter = $encounter;
  122.         $this->echo_commands();
  123.     }
  124.    
  125.     function interpret($cmd){
  126.         switch($cmd){
  127.             case 'monster list':
  128.                 echo $this->encounter;
  129.                 break;
  130.             case 'command list':
  131.                 $this->echo_commands();
  132.                 break;
  133.             case 'create':
  134.                 echo 'health: ';
  135.                 $health = trim(fgets(STDIN));
  136.                 $this->encounter->create_monster($health);
  137.                 break;
  138.             case 'remove':
  139.                 echo 'monster number: ';
  140.                 $number = trim(fgets(STDIN));
  141.                 $this->encounter->remove_monster($number);
  142.                 break;
  143.             case 'add':
  144.                 echo 'monster number: ';
  145.                 $number = trim(fgets(STDIN));
  146.                 echo 'ammount to add: ';
  147.                 $ammount = trim(fgets(STDIN));
  148.                 $this->encounter->add($number, $ammount);
  149.                 break;
  150.             case 'sub':
  151.                 echo 'monster number: ';
  152.                 $number = trim(fgets(STDIN));
  153.                 echo 'ammount to subtract: ';
  154.                 $ammount = trim(fgets(STDIN));
  155.                 $this->encounter->subtract($number, $ammount);
  156.                 break;
  157.             case 'exit':
  158.                 exit();
  159.                 break;
  160.             default:
  161.                 "Invalid command!\n";
  162.         }
  163.  
  164.     }
  165.    
  166.     function echo_commands(){
  167.         echo "\nmonster list\ncommand list\ncreate\nremove\nadd\nsub\nexit\n";
  168.     }
  169.  
  170. }
  171.  
  172. //PROGRAM BEGINS
  173. echo "WELCOME\n";
  174. $encounter = new Encounter(200, 300, 400, 500);
  175. $interpreter = new Interpreter($encounter);
  176.  
  177. while(true){
  178.     $interpreter->interpret(trim(fgets(STDIN)));
  179. }
Add Comment
Please, Sign In to add comment