class TuringException extends Exception {}
class AcceptException extends TuringException {}
class RejectException extends TuringException {}
class State {
// 状态id
private $id;
// 本状态的转移函数
private $action = array();
// 指向本状态所属的图灵机的指针
private $tm;
// 是否是接受状态
private $is_accept;
// 是否是拒绝状态
private $is_reject;
public function __construct($id, array $action, $is_accept, $is_reject, $tm) {
$this->id = $id;
$this->is_accept = $is_accept;
$this->is_reject = $is_reject;
$this->action = $action;
$this->tm = $tm;
}
// 获取状态id
public function get_id() {
return $this->id;
}
// 状态发生动作
public function action() {
if($this->is_accept) {
throw new AcceptException();
} elseif($this->is_reject) {
throw new RejectException();
}
$alpha = current($this->tm->tape);
if(!isset($this->action[$alpha])) {
throw new TuringException("Undefined action for state {$this->id} read \"{$alpha}\".");
}
$act = $this->action[$alpha];
if(!is_array($act) || count($act) !== 3) {
throw new TuringException("Wrong formatted action defined for state {$this->id} read \"{$alpha}\".");
}
$write_down = $act[0];
$move = $act[1];
$next_state = $act[2];
if(!in_array($write_down, $this->tm->tau, true)) {
throw new TuringException("Holy shit! state {$this->id} will write \"{$write_do