斗地主游戲文檔
1.體系結(jié)構
2.邏輯流程圖
3.服務器-客戶端通訊圖
4.數(shù)據(jù)結(jié)構
4.1牌型
每一張撲克都有對應的index,根據(jù)圖中撲克的順序,設置每張撲克對應的index為從左到右以及從上到下遞增.(即左上角的紅桃A為0,右上角的紅桃K為12,方塊A為13,以此類推)
4.2比較大小
撲克大小由其對應的value表示,從3到K對應的value為2到12,A對應13,2對應14,大小王對應16與15,根據(jù)value比較大小
4.3出牌數(shù)組
由int數(shù)組保存出牌的數(shù)據(jù),即一組index
5.系統(tǒng)模塊
5.2消息模型
var socket = io.connect('http://localhost:3000');
var X = window.scriptData; //截取服務器發(fā)送過來的數(shù)據(jù)
//收到服務器發(fā)送的不同的消息類型,調(diào)用對應的出牌模型中的函數(shù)
socket.on("connect",function () {
socket.emit("addUser",X._id); //添加用戶
})
socket.on("playerSit",function (obj) {
MODAL.insertImg($(".seat").eq(obj.index).children(),obj);
})
socket.on("leave",function (index) {
MODAL.removeImg($(".seat").eq(index).children());
})
socket.on("seatsInfo",function (obj) {
console.log("seatsInfo"+obj);
for(var key in obj){
console.log(key);
MODAL.insertImg($(".seat").eq(obj[key].index).children(),obj[key]);
}
})
socket.on("gameStart",function (obj,turn) {
MODAL.startGame(obj,turn);
})
socket.on("postCards",function (obj) {
MODAL.justifyWhich(obj);
})
socket.on("reStart",function (array,turn) {
MODAL.reStart(array,turn);
})
socket.on("giveup",function (giupCount) {
MODAL.giveUpReply(giupCount);
})
socket.on("renshu",function (seats) {
MODAL.someOneTouXiang(seats);
})
var socketFun = {
//出牌對象通過socketFun調(diào)用相關函數(shù)與服務器通信
sit:function ($this) {
var obj = {
id:X._id,
index:$this.parent().index()
}
socket.emit("sitSeat",obj);
},
sendCards:function (array) {
var sendOut;
if(($(".cardsLine .cards").children().length-array.length)==0){
sendOut = true;
}else{
sendOut = false;
}
var obj = {
array:array,
posterIndex:MODAL.default.myIndex,
sendOut:sendOut
}
socket.emit("postCards",obj);
},
readyMsg:function (obj) {
socket.emit("readyMsg",obj);
},
giveUp:function () {
socket.emit("giveup");
},
touxiang:function (index) {
socket.emit("touxiang",index)
}
}
5.3壓牌邏輯
邏輯框圖的array[i].value(下面為了簡化改為arr[i].val)表示撲克牌數(shù)組中的第i張撲克牌(從0計數(shù))的實際value,array已經(jīng)被排過序,以下的狀態(tài)機邏輯圖省略對length的判斷
function compCards(array) {
//ptr指向array的下標
var ptr;
//end標志狀態(tài)機是否結(jié)束
var end = false;
//data存儲著每一張撲克的value,避免多次運算value
var box = {
cardsType:{
count:array.length,
type:"ONE",
value:data[array[0]].value
},
setType:function (type) {
this.cardsType.type = type;
},
statusOne:function () {
if(this.cardsType.count==1){
end = true;
return ;
}
if(data[array[0]].value==data[array[1]].value){ //如果第一個和第二個數(shù)字相同
this.setType("TWO");
return ;
}
if(data[array[0]].value==data[array[1]].value+1){
this.setType("STRAIGHT");
}else{
this.setType("ERR");
}
return ;
},
statusTwo:function () {
if(this.cardsType.count==2){
end = true;
return ;
}
if(data[array[1]].value==data[array[2]].value){
this.setType("THREE");
return ;
}
if(data[array[1]].value==data[array[2]].value+1){
this.setType("TWO-ONE");
}else{
this.setType("ERR");
}
},
statusThree:function () {
if(this.cardsType.count==3){
end = true;
return ;
}
if(data[array[2]].value==data[array[3]].value){
this.setType("BOMB");
return ;
}
if(data[array[2]].value==data[array[3]].value+1){
this.setType("THREE-ONE");
}else{
this.setType("ERR");
}
return ;
},
statusStraight:function () {
if(this.cardsType.count< 5){
this.setType("ERR");
end = true;
return ;
}
if(ptr< this.cardsType.count-1){
if(data[array[ptr]].value!=data[array[ptr+1]].value+1){
this.setType("ERR");
end = true;
return ;
}
}else{
end = true;
return ;
}
},
statusTwoOne:function () {
if(ptr==this.cardsType.count-1){ //TwoOne處于中間狀態(tài),結(jié)束則出錯
this.setType("ERR");
return ;
}
if(data[array[ptr]].value==data[array[ptr+1]].value){
this.setType("TWO-TWO");
}else{
this.setType("ERR");
}
return ;
},
statusTwoTwo:function () {
if(ptr==this.cardsType.count-1){
end = true;
return ;
}
if(data[array[ptr]].value==data[array[ptr]].value+1){
this.setType("TWO-ONE");
}else{
this.setType("ERR");
}
return ;
},
statusThreeOne:function () {
if(ptr==this.cardsType.count-1){
this.setType("ERR");
return ;
}
if(data[array[ptr]].value==data[array[ptr+1]].value){
this.setType("THREE-TWO");
}else{
this.setType("ERR");
}
return ;
},
statusThreeTwo:function () {
if(ptr==this.cardsType.count-1){
this.setType("ERR");
return ;
}
if(data[array[ptr]].value==data[array[ptr+1]].value){
this.setType("THREE-THREE");
}else{
this.setType("ERR");
}
return ;
},
statusThreeThree:function () {
if(ptr==this.cardsType.count-1){
end = true;
return ;
}
if(data[array[ptr]].value==data[array[ptr+1]].value+1){
this.setType("THREE-ONE");
}else{
this.setType("ERR");
}
return ;
},
statusBomb:function () {
if(ptr==this.cardsType.count-1){
end = true;
return ;
}
if(data[array[ptr]].value!=data[array[ptr+1]].value){
this.setType("ERR");
}
},
ERR:function () {
end = true;
return ;
}
};
for(ptr = 0;ptr< box.cardsType.count;++ptr){
console.log("END:"+end);
console.log(box.cardsType);
if(end){
break;
}
switch(box.cardsType.type){
//ONE表示單張牌,這個ONE狀態(tài)結(jié)束有效
case "ONE":
box.statusOne();
break;
//TWO表示一對,結(jié)束有效
case "TWO":
box.statusTwo();
break;
//THREE表示三張一樣的牌,結(jié)束有效
case "THREE":
box.statusThree();
break;
//STRAIGHT表示順子,根據(jù)array長度判斷是否有效
case "STRAIGHT":
box.statusStraight();
break;
//TWO-ONE表示形如xx(x+1)(x+1)(x+2)的牌型,結(jié)束無效,返回類型ERR
case "TWO-ONE":
box.statusTwoOne();
break;
case "TWO-TWO":
//TWO-TWO表示形如xx(x+1)(x+1)(x+2)(x+2)的牌型,結(jié)束有效
box.statusTwoTwo();
break;
//THREE-ONE表示形如xxx(x+1)(x+1)(x+1)(x+2)的牌型,結(jié)束無效,返回類型ERR
case "THREE-ONE":
box.statusThreeOne();
break;
//THREE-TWO表示形如xxx(x+1)(x+1)(x+1)(x+2)(x+2)的牌型,結(jié)束無效,返回類型ERR
case "THREE-TWO":
box.statusThreeTwo();
break;
//THREE-THREE表示形如xxx(x+1)(x+1)(x+1)(x+2)(x+2)(x+2)的牌型,結(jié)束有效
case "THREE-THREE":
box.statusThreeThree();
break;
//BOMB表示炸彈,返回有效
case "BOMB":
box.statusBomb();
break;
//ERR表示牌型不合邏輯,無效
case "ERR":
box.ERR();
break;
}
}
return box.cardsType;
}