本文知識給大家分享一下使用js寫斗地主的思路,代碼寫的不好,還請見諒。
這裡說說斗地主主要包含的功能:洗牌,發牌,玩家出牌、電腦出牌,出牌規則的驗證,輸贏啥的沒有判斷,只是實現了這幾個主要功能,下面依次說說幾個功能的實現:
1.洗牌:
var pukes=this.manage.pukes;//存放撲克牌的數組 //洗牌 for(var i=;i<pukes.length;i++){ var tmp=pukes[i]; var index=util.random(i,pukes.length);//隨機交換兩張牌 pukes[i]=pukes[index]; pukes[index]=tmp; }
2.發牌(簡版默認是玩家自己為地主,電腦為農民),由於前面洗牌時已經將牌的順序打亂,所以發牌只是簡單的循環把pukes中的元素添加到每個玩家實例中的pukes字段中。
//發牌 var start=; for(var i=;i<this.manage.pukes.length-;i++) { if(start==this.manage.players.length){ start=; } this.manage.pukes[i].status=true; this.manage.players[start].pukesLen++; this.manage.players[start++].pukes.push(this.manage.pukes[i]); } for(var i=this.manage.pukes.length-;i<this.manage.pukes.length;i++){ //地主多三張 this.manage.pukes[i].status=true; this.manage.players[this.manage.curPlayerIndex].pukesLen++; this.manage.players[this.manage.curPlayerIndex].pukes.push(this.manage.pukes[i]); }
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 <len&&j<this.pukes.length; i++) { //隨便選一張 可以出就行 if(this.pukes[j].status) { spks.push(this.pukes[j++]); } } if(spks.length) { if (rules.valids(spks, this.manage.deskPukes)) { gz = true; break; } } start++; } } else { //玩家選擇出牌 for (var i = ; i < this.pukes.length; i++) { if (this.pukes[i].selected && this.pukes[i].status) { spks.push(this.pukes[i]); } } if (rules.valids(spks, this.manage.deskPukes)) { gz=true; } else{ alert("出牌不符合規則!"); } } if(gz){ this.manage.curMaxPlayerIndex=this.options.playerIndex; this.manage.deskPukes = []; for (var i = ; i < spks.length; i++) { this.pukesLen--; this.manage.deskPukes.push(spks[i]); spks[i].status = false; } } this.manage.renderPukes(); this.manage.renderCurDiscard(); if(this.isCompute||gz) { this.manage.nextPlayer(); } } else{ alert("沒輪到你出牌!"); }
4.出牌規則的驗證,是很多函數組合起來,然後循環調用,如果遇到返回ture的即為出牌符合規則:
//以下為出牌規則 var rules={ _rules:[ new danzRule(), new duiRule(), new sandRule(), new zandRule(), new shunzRule(), new liandRule() ], valids:function(_pukes,_curPukes){ for(var i=;i<this._rules.length;i++){ if(this._rules[i].valid(_pukes,_curPukes)){ return true; } } return false; } }; function danzRule(){ //單張規則 } danzRule.prototype.valid=function(_pukes,_curPukes){ //校驗 var pukes=_pukes;//玩家的牌 var curPukes=_curPukes;//左面的牌 if(pukes&&pukes.length==){ //比較牌面值 if(!curPukes||!curPukes.length){ return true; } if(curPukes[].dians==&&pukes[].dians<){ //特殊處理 return false; } if(pukes[].dians==&&curPukes[].dians<){ //特殊處理 return true; } return pukes[].dians>curPukes[].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<books.length;i++) { if(books[i]==){ if(i==)return ; sum+=i; } } return sum; } function valid(books){ //驗證三帶是否有效 var counts= ,countsd= ,d=true,start=false,startIndex=-; for(var i=;i<books.length;i++) { if(start&&books[i]==&&startIndex!=(i-)){ return false; }else{ startIndex=i; } if(books[i]==){ if(!start) { start = true; startIndex = i; } counts++; } if(books[i]==){ d=false; } } for(var i=;i<books.length;i++) { if(d&&books[i]==){ countsd++; } else if(!d&&books[i]==){ countsd++; } } return counts>&&counts==countsd; } function getBooks(pukes){ //返回三帶的每個點數的個數 var books=[]; for(var i=;i<pukes.length;i++){ if(!books[pukes[i].dians]){ books[pukes[i].dians]=; }else{ books[pukes[i].dians]++; } } return books; } } function zandRule(){ //炸彈 } zandRule.prototype.valid=function(_pukes,_curPukes){ var pukes=_pukes;//玩家的牌 var curPukes=_curPukes;//左面的牌 if(pukes&&pukes.length==) { if(!allEqual(pukes)){ return false; } if(!curPukes||(curPukes.length>&&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<pukes.length;i++){ if(base!=pukes[i].dians){ return false; } } return true; } } function liandRule(){ //連對 } liandRule.prototype.valid=function(_pukes,_curPukes) { var pukes=_pukes;//玩家的牌 var curPukes=_curPukes;//左面的牌 if(pukes&&pukes.length>=) { 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<pukes.length;i++) { sum+=pukes[i].dians; } return sum; } function verificationCoherence(pukes){ //驗證連貫性 if(!pukes||!pukes.length)return false; var books=[]; for(var i=;i<pukes.length;i++){ if(pukes[i].dians==||pukes[i].dians>){ 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<books.length;i++) { if(books[i]&&books[i]!=){ return false; } if(books[i]==&&!start){ start=true; } if(start&&books[i]!=){ return false; } } return true; } } function shunzRule(){ //順子 } shunzRule.prototype.valid=function(_pukes,_curPukes){ var pukes=_pukes;//玩家的牌 var curPukes=_curPukes;//左面的牌 if(pukes&&pukes.length>=) { 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<pukes.length;i++) { sum+=pukes[i].dians; } return sum; } function verificationCoherence(pukes){ //驗證連貫性 if(!pukes||!pukes.length)return false; var books=[]; for(var i=;i<pukes.length;i++){ if(pukes[i].dians==||pukes[i].dians>){ return false; } if(!books[pukes[i].dians]){ books[pukes[i].dians]=; }else{ return false; } } var start=false; for(var i=;i<books.length;i++) { if(books[i]==&&!start){ start=true; } if(start&&!books[i]){ return false; } } return true; } }
以上4步,是我認為的主要的4個函數,其他的函數,如初始化,事件注冊等,在源碼已有注釋,寫的不好勿噴。
JavaScript實現斗地主游戲的思路就給大家介紹這麼多,希望對大家有所幫助!