dongxi7704 2014-05-07 21:54
浏览 34
已采纳

在PHP中实现有界数组

I was trying to create a simple PHP game and ran into a problem. In this game, a player may stand on one of 18 positions. Position 1 is a start. The player "rolls a dice" - rand(1,6) - and may move that many positions either forward or backward. A simplified version of this "board" may look something like this:

$positions = array(
    1 => 'Start',
    2 => 'Some Field',
    3 => 'Some Field',
    4 => 'Some Field',
    5 => 'Some Field',
    ...
);

At this point I'm struggling how to write code to track the player's movement forward or backward within those fields while keeping the results bound to valid "positions"? Thank you in advance for any help!

  • 写回答

1条回答 默认 最新

  • drelgkxl93433 2014-05-07 22:00
    关注

    Create a class that implements SPL's ArrayAccess. As an starting point, here's an implementation of a ring buffer I wrote using SPL, just to get my hands dirty with the API.

    <?php
    
    class ringBuffer implements ArrayAccess {
        private $data;
        private $current = 0;
    
    
    
        public $size;
    
        public function __construct($sz=10) {
            $this->size = $sz;
            foreach(range(1, $sz) as $k=>$v) {
                /* s: whether this node is set; d: whether this node is "dirty" (has been read since it was last set); v: the value */
                $this->data[$k] = array('s' => false, 'd' => false, 'v' => NULL);
            }
        }
    
        private function offsetSet($key, $value) {
            $this->throwEx(array('Do not directly set indices in a %s, use push() instead', __CLASS__));
            return false;
        }
    
        public function offsetGet($key) {
            if (! is_int($key)) $this->throwEx(array("offset '%s' must be an integer, not %s", $key, gettype($key)));
            if ($key > $this->size) $key %= $this->size;
            if (!isset($this->data[$key])) return false;
            if (!$this->data[$key]['s']) return false;
    
            $this->data[$key]['d'] = false;
            return $this->data[$key]['v'];
        }
    
        public function offsetExists($key) {
            if (! is_int($key))
                throw new Exception();
            if ($key > $this->size)
                return $this->data[$key]['s'];
        }
    
        public function offsetUnset($key) {
            $this->data[$key]['s'] = false;
            $this->data[$key]['v'] = NULL;
        }
    
        private function throwEx() {
            $args = func_get_args();
            if (is_array($args0)) {
                $msg = call_user_func_array('sprintf', $args0);
            }
            throw new Exception($msg);
        }
    
        public function push($value) {
            if ($this->current >= $this->size) $this->current %= $this->size;
            $this->data[$this->current] = array('s'=>true, 'd'=>true, 'v' => $value);
            $this->current++;
            return $value;
        }
    }
    
    
    $a = new ringBuffer(2);
    $a->push("foo");
    $a->push("bar");
    $a->push("baz");
    
    var_dump($a);
    
    /* Will throw an exception because you can't directly set indices in a ringBuffer */
    
    $a0 = 'foo';
    
    /* Will throw an exception because ringBuffer indices must be ints */
    var_dump($a['foo']);
    var_dump($a[1.0]);
    

    Doing this will allow you to build a lot more intelligence and behaviours into the data structure than a standard array will provide; for example, you could make it so that each 'field' has a list of adjacent fields which are possible moves from there ("if you're standing on 2, you can move to 6, 8, or 14 - but not 1, 3, or 7") and have that enforced BY the data structure, instead of littered through the code in every spot a move might come from.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制