JavaScript实现斗地主游戏的思路_javascript技巧

WBOY
Release: 2016-05-16 15:13:12
Original
2123 people have browsed it

本文知识给大家分享一下使用js写斗地主的思路,代码写的不好,还请见谅。

这里说说斗地主主要包含的功能:洗牌,发牌,玩家出牌、电脑出牌,出牌规则的验证,输赢啥的没有判断,只是实现了这几个主要功能,下面依次说说几个功能的实现:

  1.洗牌:

var pukes=this.manage.pukes;//存放扑克牌的数组 //洗牌 for(var i=;i
         
Copy after login

2.发牌(简版默认是玩家自己为地主,电脑为农民),由于前面洗牌时已经将牌的顺序打乱,所以发牌只是简单的循环把pukes中的元素添加到每个玩家实例中的pukes字段中。

//发牌 var start=; for(var i=;i
         
Copy after login

3.玩家出牌,玩家出牌分为电脑和玩家自己两个步骤,电脑出牌是非常傻瓜的出牌方式(有牌就出):

//出牌 if(this.options.playerIndex==this.manage.curPlayerIndex) { var spks = [],gz=false; if (this.manage.curMaxPlayerIndex == this.options.playerIndex) { this.manage.deskPukes = []; } if (this.isCompute) { //电脑自动出牌 var start = ; var len=this.manage.deskPukes.length||; while (start < this.pukes.length) { spks = []; for (var i = ,j=start; i 
         
Copy after login

4.出牌规则的验证,是很多函数组合起来,然后循环调用,如果遇到返回ture的即为出牌符合规则:

//以下为出牌规则 var rules={ _rules:[ new danzRule(), new duiRule(), new sandRule(), new zandRule(), new shunzRule(), new liandRule() ], valids:function(_pukes,_curPukes){ for(var i=;icurPukes[].dians; } return false; } function duiRule(_pukes,_curPukes){ //两张规则 } duiRule.prototype.valid=function(_pukes,_curPukes){ //校验 var pukes=_pukes;//玩家的牌 var curPukes=_curPukes;//左面的牌 if(pukes&&pukes.length==){ //比较牌面值 if(pukes[].dians>&&pukes[].dians>){ return true; } if(pukes[].dians!=pukes[].dians){ return false; } if(!curPukes||!curPukes.length){ return true; }else { if(curPukes.length!=){ return false; } if (curPukes[].dians > && curPukes[].dians > ) { return false; } if (curPukes[].dians != curPukes[].dians) { return false; } if (curPukes[].dians == ) { return false; } } if(pukes[].dians==){ return true; } return pukes[].dians>curPukes[].dians; } return false; } function sandRule(){ //三带 } sandRule.prototype.valid=function(_pukes,_curPukes){ //校验 var pukes=_pukes;//玩家的牌 var curPukes=_curPukes;//左面的牌 if(pukes&&(pukes.length>=)){ //比较牌面值 var books=getBooks(pukes); if(!valid(books))return false; if(!curPukes||!curPukes.length)return true; if(curPukes.length!=books.length)return false; var books=getBooks(curPukes); if(!valid(books))return false; return getSum(books)>getSum(books); } return false; function getSum(books){ var sum=; for(var i=;i&&counts==countsd; } function getBooks(pukes){ //返回三带的每个点数的个数 var books=[]; for(var i=;i&&curPukes.length!=)||!allEqual(curPukes)){ return true; } else{ if(pukes[].dians==){ return true; } if(curPukes[].dians==){ return false; } return pukes[].dians>curPukes[].dians; } } return false; function allEqual(pukes){ if(!pukes||!pukes.length)return false; var base=pukes[].dians; for(var i=;i=) { if(!verificationCoherence(pukes)){ return false; } if(!curPukes||curPukes.length<=){ return true; } if(!verificationCoherence(curPukes)){ return false; } if(pukes.length!=curPukes.length){ return false; } return getSumDians(pukes)>getSumDians(curPukes); } return false; function getSumDians(pukes){ var sum=; for(var i=;i){ return false; } if(!books[pukes[i].dians]){ books[pukes[i].dians]=; }else{ books[pukes[i].dians]++; } if(books[pukes[i].dians]>){ return false; } } var start=false; for(var i=;i=) { if(!verificationCoherence(pukes)){ return false; } if(!curPukes||curPukes.length<=){ return true; } if(!verificationCoherence(curPukes)){ return false; } if(pukes.length!=curPukes.length){ return false; } return getSumDians(pukes)>getSumDians(curPukes); } return false; function getSumDians(pukes){ var sum=; for(var i=;i){ return false; } if(!books[pukes[i].dians]){ books[pukes[i].dians]=; }else{ return false; } } var start=false; for(var i=;i
         
Copy after login

  以上4步,是我认为的主要的4个函数,其他的函数,如初始化,事件注册等,在源码已有注释,写的不好勿喷。

JavaScript实现斗地主游戏的思路就给大家介绍这么多,希望对大家有所帮助!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!