Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Map{
- protected $length;
- protected $position;
- //---------------------------------------------
- function __construct(){
- $this->length=100;
- $this->position=1;
- }
- //---------------------------------------------
- public function next_step($val=1){
- if(is_numeric($val)){
- if(($this->position + $val) <= $this->length){
- $this->position += $val;
- }
- }
- if($this->is_snake()){
- $result = $val."-snake".$this->position;
- }
- elseif($this->is_ladder()){
- $result = $val."-ladder".$this->position;
- }
- else{
- $result = $val."-".$this->position;
- }
- return $result;
- }
- //---------------------------------------------
- private function is_snake(){
- if($this->position%9===0){
- $this->position-=3;
- return true;
- }
- return false;
- }
- //---------------------------------------------
- private function is_ladder(){
- if($this->position===25 || $this->position===55){
- $this->position+=10;
- return true;
- }
- return false;
- }
- //---------------------------------------------
- protected function is_finish(){
- if($this->position===100){
- return true;
- }
- return false;
- }
- //---------------------------------------------
- }
- class Gamer extends Map{
- private $name;
- private $step;
- function __construct($name='gamer-1'){
- parent::__construct();
- $this->name=is_string($name)? $name : "gamer-".rand(10-100);
- $this->step=0;
- }
- public function startGame(){
- echo "$this->name >> start\n";
- do{
- $this->step++;
- $step=$this->next_step($this->dice());
- echo "$this->name step-$this->step >> $step\n";
- } while(!$this->is_finish());
- echo "$this->name >> end of the game\n";
- }
- public function dice(){
- return rand(1,6);
- }
- public function getName(){
- return $this->name;
- }
- }
- if(isset($argv[1])){
- $name=$argv[1];
- }
- else{
- $name='gamer-1';
- }
- $gamer=new Gamer($name);
- $gamer->startGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement