羊 狼过河问题模拟过程

<?php
/*
羊 狼过河问题1.00版
*/
define('LEFT',0);
define('RIGHT',1);
class An{
var $yang=0;
var $lang=0;
function pop($type){

if($type=='yang')
$this->yang--;
else
$this->lang--;
if($this->yang<0||$this->lang<0){
echo "err";
exit();
}
}
function push($type){
if($this->type=='yang')
$this->yang++;
else{
$this->lang++;
}
}
function isEmpty(){
if($this->yang==0&&$this->lang==0)
return true;
else
return false;
}
function getYang(){
return $this->yang;
}
function getLang(){
return $this->lang;
}
}
class Chuan{
var $yang=0;
var $lang=0;
var $counts=0;//船上成员数;
var $size=2;//船容量
var $stoppingAt=LEFT;//0 在左岸 1在右岸
function pop($type){
if($type=='yang')
$this->yang--;
else
$this->lang--;
$this->counts--;
if($this->counts<0){
echo "err";
exit();
}
}
function push($type){
$this->counts++;
if($this->counts>=$this->size){
exit("err");
}
if($type=='yang')
$this->yang++;
else{
$this->lang++;
}
}
function getStoppingAt(){
return $this->stoppingAt;
}
function running(){
$this->stoppingAt++;
$this->stoppingAt=$this->stoppingAt%2;
}
function getYang(){
return $this->yang;
}
function getLang(){
return $this->lang;
}
}
class T{
var $chuan;
var $an_left;
var $an_right;
function __construct(){
$this->chuan=new Chuan();
$this->an_left=new An();
$this->an_right=new An();
for($i=0;$i<3;$i++){
$this->an_left->push('yang');
$this->an_left->push('lang');
}
}
function isWin(){
return $this->an_left->isEmpty();
}
function isLost(){
if($this->an_left->getLang()>$this->an_left->getYang()){
return true;
}
if((this->an_right->getLang()>$this->an_right->getYang()){
return true;
}
return false;
}
function pop($type,&$an){
if($type==1){
$this->chuan->pop('yang');
$this->chuan->pop('yang');
$this->an->push('yang');
$this->an->push('yang');
}
if($type==2){
$this->chuan->pop('lang');
$this->chuan->pop('lang');
$this->an->push('lang');
$this->an->push('lang');
}
if($type==3){
$this->chuan->pop('yang');
$this->chuan->pop('lang');
$this->an->push('yang');
$this->an->push('lang');
}
if($type==4){
$this->chuan->pop('yang');
$this->an->push('yang');
}
if($type==5){
$this->chuan->pop('lang');
$this->an->push('lang');
}
}
function push($type,&$an){
if($type==1){
$this->chuan->push('yang');
$this->chuan->push('yang');
$this->an->pop('yang');
$this->an->pop('yang');
}
if($type==2){
$this->chuan->push('lang');
$this->chuan->push('lang');
$this->an->pop('lang');
$this->an->pop('lang');
}
if($type==3){
$this->chuan->push('yang');
$this->chuan->push('lang');
$this->an->pop('yang');
$this->an->pop('lang');
}
if($type==4){
$this->chuan->push('yang');
$this->an->pop('yang');
}
if($type==5){
$this->chuan->push('lang');
$this->an->pop('lang');
}
}
function runing($a){
while($opt=array_shift($a)){
$an=&$this->getStopAn();
$this->push($opt,$an);
$this->chuan->running();
$an=&$this->getStopAn();
$this->pop($opt,$an);
if($this->isLost()){
echo "lost game!";
break;
}elseif($this->isWin()){
echo "win game!";
break;
}
}
echo "lost game!";
}
function getStopAn(){
if($this->chuan->getStoppingAt()==LEFT){
return $this->an_left;
}else{
return $this->an_right;
}
}on
}
$a=array(1,2,3,4,5,1);
$t=new T();
$t->runing($a);
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值