Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. <?php
  2.  
  3. class Map{
  4. protected $length;
  5. protected $position;
  6.  
  7. //---------------------------------------------
  8.  
  9. function __construct(){
  10. $this->length=100;
  11. $this->position=1;
  12. }
  13.  
  14. //---------------------------------------------
  15.  
  16. public function next_step($val=1){
  17. if(is_numeric($val)){
  18. if(($this->position + $val) <= $this->length){
  19. $this->position += $val;
  20. }
  21. }
  22.  
  23. if($this->is_snake()){
  24. $result = $val."-snake".$this->position;
  25. }
  26. elseif($this->is_ladder()){
  27. $result = $val."-ladder".$this->position;
  28. }
  29. else{
  30. $result = $val."-".$this->position;
  31. }
  32.  
  33. return $result;
  34.  
  35. }
  36.  
  37. //---------------------------------------------
  38.  
  39. private function is_snake(){
  40. if($this->position%9===0){
  41. $this->position-=3;
  42. return true;
  43. }
  44. return false;
  45. }
  46.  
  47. //---------------------------------------------
  48.  
  49. private function is_ladder(){
  50. if($this->position===25 || $this->position===55){
  51. $this->position+=10;
  52. return true;
  53. }
  54. return false;
  55. }
  56.  
  57. //---------------------------------------------
  58.  
  59. protected function is_finish(){
  60. if($this->position===100){
  61. return true;
  62. }
  63. return false;
  64.  
  65. }
  66.  
  67. //---------------------------------------------
  68. }
  69.  
  70.  
  71. class Gamer extends Map{
  72. private $name;
  73. private $step;
  74.  
  75. function __construct($name='gamer-1'){
  76. parent::__construct();
  77. $this->name=is_string($name)? $name : "gamer-".rand(10-100);
  78. $this->step=0;
  79. }
  80.  
  81.  
  82.  
  83. public function startGame(){
  84. echo "$this->name >> start\n";
  85.  
  86. do{
  87. $this->step++;
  88. $step=$this->next_step($this->dice());
  89. echo "$this->name step-$this->step >> $step\n";
  90.  
  91. } while(!$this->is_finish());
  92.  
  93. echo "$this->name >> end of the game\n";
  94. }
  95.  
  96. public function dice(){
  97. return rand(1,6);
  98. }
  99.  
  100. public function getName(){
  101. return $this->name;
  102. }
  103. }
  104.  
  105. if(isset($argv[1])){
  106. $name=$argv[1];
  107. }
  108. else{
  109. $name='gamer-1';
  110. }
  111.  
  112.  
  113. $gamer=new Gamer($name);
  114. $gamer->startGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement