在PHP中实现有界数组

时间:2014-05-07 21:54:27

标签: php

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

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

此时我挣扎着如何编写代码来跟踪玩家在这些领域内前进或后退的动作,同时保持结果与有效的"位置相关联?"?提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

创建一个实现SPL' ArrayAccess的类。作为一个起点,这里是使用SPL实现环形缓冲区I wrote,只是为了弄清楚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更多的智能和行为;例如,你可以做到这一点,以便每个字段&#39;有一个相邻字段的列表,可以从那里移动(&#34;如果你站在2,你可以移动到6,8或14 - 但不是1,3或7&#34;)并且通过数据结构强制执行,而不是通过代码散布在每个地点的移动可能来自。

相关问题