在PHP中实现有界数组


Implementing a bounded Array in PHP

我试图创建一个简单的PHP游戏,遇到了一个问题。在这个游戏中,玩家可以站在18个位置中的一个。位置1是起点。玩家"掷骰子"(rand(1,6)),可以向前或向后移动多个位置。这个"板"的简化版本可能看起来像这样:

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

在这一点上,我正在努力如何编写代码来跟踪球员的运动向前或向后在这些领域,同时保持结果绑定到有效的"位置"?提前感谢您的任何帮助!

创建一个实现SPL的ArrayAccess的类。作为起点,这里是我使用SPL编写的一个环缓冲区的实现,只是为了让我亲自动手使用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]);

这样做将允许你在数据结构中构建比标准array所提供的更多的智能和行为;例如,你可以让每个"字段"都有一个相邻字段的列表,这些字段可能从那里移动("如果你站在2上,你可以移动到6、8或14 -但不能移动到1、3或7"),并通过数据结构强制执行,而不是在每个移动可能来自的位置分散在代码中。