这几天又做了牛牛游戏,在这里把相关算法写一下,与前几天写的赢三张的基本类似,所以大家也可以参考前几篇博客。
牛牛是一种简单却又非常有意思的棋牌游戏,主要是采用一副扑克牌其中的52张(除去大小王);第一局的庄家是随机产生,而后每局的庄家是上一轮游戏中的赢家,每局游戏系统自动洗牌后将5张牌均匀分给给各位玩家。玩家拿到牌后根据一定的游戏规则进行排列组合,庄家和闲家依次进行大小比较从而决定胜负。
牛牛的牌型比较里考虑到了花色的大小,即:
单牌比较: k>q>j>10>9>8>7>6>5>4>3>2>a。 花色比较:黑桃>红桃>梅花>方块。
同时不同的牌有一个算“牛”的数值含义,K Q J的数值是10,其他的等于自身数值。
所以为了方便计算,在定义牌信息类里面设三个参数,分别表示花色、牌值、数值
- CardColor =
- {
- Spade = 4, --黑桃
- Heart = 3, --红桃
- Plum = 2, --梅花
- Block = 1, --方块
- }
-
- CardValue =
- {
- card_A = 1,
- card_2 = 2,
- card_3 = 3,
- card_4 = 4,
- card_5 = 5,
- card_6 = 6,
- card_7 = 7,
- card_8 = 8,
- card_9 = 9,
- card_10 = 10,
- card_J = 11,
- card_Q = 12,
- card_K = 13,
- }
-
- CardCount =
- {
- card_A = 1,
- card_2 = 2,
- card_3 = 3,
- card_4 = 4,
- card_5 = 5,
- card_6 = 6,
- card_7 = 7,
- card_8 = 8,
- card_9 = 9,
- card_10 = 10,
- card_J = 10,
- card_Q = 10,
- card_K = 10,
- }
扑克的数据同样采用16进制双位数,一位表示花色,一位表示牌值
- --扑克数据
- CardData=
- {
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
- 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,
- 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,
- 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,
- }
-
- g_CardsCount = 52 --扑克数目
- g_PerPlaCardCount = 5 --每个玩家牌数
所以获取单牌信息的方法为:
- local cardColor = luabit.band(cardBuffer[i] , 0xF0)/16+1
- local cardValue = luabit.band(cardBuffer[i] , 0x0F)
- local cardCount = cardTool.getCountbyValue(cardValue)
-
- local cardinfo =
- {
- card_value = cardValue,
- card_color = cardColor,
- card_count = cardCount,
- }
- function cardTool.getCountbyValue(value)
- if value > 10 then
- return 10
- else
- return value
- end
- end
洗牌:
- function cardTool.RandCardList()
-
- local bufferCount = #CardData;
- local randCount = 1;
- local position = 1;
-
-
- math.randomseed(os.time())
-
-
-
- for i = 1,#CardData do
- local ranOne = math.random(1,#CardData+1-i)
- CardData[ranOne], CardData[#CardData+1-i] = CardData[#CardData+1-i],CardData[ranOne]
- end
-
- local cardBuffer = tabletool.deepcopy(CardData);
- return cardBuffer;
- end
发牌:
- local cardBuffer =cardTool.RandCardList()
-
- --[[
- cardBuffer[1]=2
- cardBuffer[2]=4+16
- cardBuffer[3]=6
- cardBuffer[4]=10+16
- cardBuffer[5]=12+32
-
- cardBuffer[6]=6+48
- cardBuffer[7]=7+16
- cardBuffer[8]=9
- cardBuffer[9]=10+32
- cardBuffer[10]=12+48
- ]]
-
- local cards1={}
- local cards2={}
-
- for i=1,10,1 do
- local cardColor = luabit.band(cardBuffer[i] , 0xF0)/16+1
- local cardValue = luabit.band(cardBuffer[i] , 0x0F)
- local cardCount = cardTool.getCountbyValue(cardValue)
-
- local cardinfo =
- {
- card_value = cardValue,
- card_color = cardColor,
- card_count = cardCount,
- }
- if i >5 then
- cards2[i-5] = cardinfo
- else
- cards1[i] = cardinfo
- end
-
-
- end
两个cards的数据为:
- table: 0072B4F8 {
- [1] => table: 0072B4F8 {
- [card_color] => 2
- [card_value] => 3
- [card_count] => 3
- }
- [2] => table: 0072B4F8 {
- [card_color] => 2
- [card_value] => 6
- [card_count] => 6
- }
- [3] => table: 0072B4F8 {
- [card_color] => 3
- [card_value] => 6
- [card_count] => 6
- }
- [4] => table: 0072B4F8 {
- [card_color] => 3
- [card_value] => 1
- [card_count] => 1
- }
- [5] => table: 0072B4F8 {
- [card_color] => 2
- [card_value] => 7
- [card_count] => 7
- }
- }
-
- table: 0072B520 {
- [1] => table: 0072B520 {
- [card_color] => 2
- [card_value] => 9
- [card_count] => 9
- }
- [2] => table: 0072B520 {
- [card_color] => 2
- [card_value] => 11
- [card_count] => 10
- }
- [3] => table: 0072B520 {
- [card_color] => 2
- [card_value] => 13
- [card_count] => 10
- }
- [4] => table: 0072B520 {
- [card_color] => 2
- [card_value] => 8
- [card_count] => 8
- }
- [5] => table: 0072B520 {
- [card_color] => 3
- [card_value] => 13
- [card_count] => 10
- }
- }
我们写一个牌的翻译函数
- function cardTool.getCardNamebyCard(Card)
-
- local string=""
- if Card.card_color ==4 then
- string=string.."黑桃"
- elseif Card.card_color ==3 then
- string=string.."红桃"
- elseif Card.card_color ==2 then
- string=string.."梅花"
- elseif Card.card_color ==1 then
- string=string.."方块"
- else
- string="ERROR"
- end
-
- if Card.card_value==13 then
- string=string.."K"
- elseif Card.card_value==12 then
- string=string.."Q"
- elseif Card.card_value==11 then
- string=string.."J"
- else
- string=string..Card.card_value
- end
- return string
- end
-
- function cardTool.getCardNamebyCards(Cards)
- local string=""
- for i = 1,#Cards do
- string=string..cardTool.getCardNamebyCard(Cards[i])
- end
- return string
- end
上述牌信息转换的牌为:
- 梅花3梅花6红桃6红桃1梅花7
-
- 梅花9梅花J梅花K梅花8红桃K
以上就是牌值数据相关的处理函数,下一章我们实现判定牌型的相关函数。
|
请发表评论