羊 狼过河问题模拟过程

/*
羊 狼过河问题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);
?>

你可能感兴趣的:(技術,function,class)