using System;
using System.Collections.Generic;
namespace GameLogic
{
public class CLogicHelp
{
//操作掩码
public enum OPERATE_MASK {
UG_HUA_MASK = 0xF0, // 1111 0000
UG_VAL_MASK = 0x0F, // 0000 1111
};
//扑克牌牌值采用16进制
byte[] pokerArray = {
0x00, //Non
0x01,0x11,0x21,0x31, //方块2~黑桃2
0x02,0x12,0x22,0x32, //方块3~黑桃3
0x03,0x13,0x23,0x33, //方块4~黑桃4
0x04,0x14,0x24,0x34, //方块5~黑桃5
0x05,0x15,0x25,0x35, //方块6~黑桃6
0x06,0x16,0x26,0x36, //方块7~黑桃7
0x07,0x17,0x27,0x37, //方块8~黑桃8
0x08,0x18,0x28,0x38, //方块9~黑桃9
0x09,0x19,0x29,0x39, //方块10~黑桃10
0x0A,0x1A,0x2A,0x3A, //方块J~黑桃J
0x0B,0x1B,0x2B,0x3B, //方块Q~黑桃Q
0x0C,0x1C,0x2C,0x3C, //方块K~黑桃K
0x0D,0x1D,0x2D,0x3D, //方块A~黑桃A
0x4E,0x4F //鬼(王)
};
//获取花色
public static byte GetPokerHua(byte iPoker)
{
return (byte)((iPoker & (int)OPERATE_MASK.UG_HUA_MASK));
}
//获取牌值(满足牌值定义做一个偏移)
public static byte GetPokerNum(byte iPoker)
{
return (byte)((iPoker & (int)OPERATE_MASK.UG_VAL_MASK) + 1);
}
#region//获取对子
public static List<byte[]> TackOutDouble(byte[] iSourcePokers, int iSourcePokersCount)
{
//2张以下不做处理
if (iSourcePokersCount<2)
{
return null;
}
List<byte[]> resultList = new List<byte[]>();//返回结果列表
List<byte> cache = new List<byte>(); //缓存列表
//找到对子加入缓存列表
for (int i = 0; i < iSourcePokersCount - 1; i++)
{
cache.Clear();
for (int j = 0; j < iSourcePokersCount; j++)
{
if (GetPokerNum(iSourcePokers[i]) == GetPokerNum(iSourcePokers[j])) {
cache.Add(iSourcePokers[i]);
cache.Add(iSourcePokers[j]);
}
if (cache.Count>0)
{
resultList.Add(cache.ToArray());
break;
}
}
}
return resultList;
}
#endregion
#region//获取两对
public static List<byte[]> TackOutTwo_Double(byte[] iSourcePokers, int iSourcePokersCount)
{
//4张以下不做处理
if (iSourcePokersCount < 4)
{
return null;
}
List<byte[]> resultList = new List<byte[]>();//返回结果列表
List<byte> cache = new List<byte>(); //缓存列表(放置所有对子)
//找到所有对子加入缓存列表
for (int i = 0; i < iSourcePokersCount - 1; i++)
{
for (int j = 0; j < iSourcePokersCount; j++)
{
if (GetPokerNum(iSourcePokers[i]) == GetPokerNum(iSourcePokers[j]))
{
cache.Add(iSourcePokers[i]);
cache.Add(iSourcePokers[j]);
}
}
}
//缓存列表长度等于4,直接加入结果列表并返回
if (cache.Count == 4)
{
resultList.Add(cache.ToArray());
return resultList;
}
//如果大于4,找到任意组合
List<byte> nCache = new List<byte>(); //缓存列表(放置两对)
if (cache.Count>4)
{
for (int i = 0; i < cache.Count -2; i+=2)
{
for (int j = i+2; j < cache.Count; j+=2)
{
nCache.Clear();
nCache.Add(cache[i]);
nCache.Add(cache[i+1]);
nCache.Add(cache[j]);
nCache.Add(cache[j+1]);
resultList.Add(nCache.ToArray());
}
}
}
return resultList;
}
#endregion
#region//获取三张
public static List<byte[]> TackOutThree(byte[] iSourcePokers, int iSourcePokersCount)
{
//3张以下不做处理
if (iSourcePokersCount < 3)
{
return null;
}
List<byte[]> resultList = new List<byte[]>();//返回结果列表
List<byte> cache = new List<byte>(); //缓存列表
//找到3张加入缓存列表
for (int i = 0; i < iSourcePokersCount - 1; i++)
{
cache.Clear();
cache.Add(iSourcePokers[i]);
for (int j = 0; j < iSourcePokersCount; j++)
{
if (GetPokerNum(iSourcePokers[i]) == GetPokerNum(iSourcePokers[j]))
{
cache.Add(iSourcePokers[j]);
}
if (cache.Count == 3)
{
resultList.Add(cache.ToArray());
break;
}
}
}
return resultList;
}
#endregion
#region//获取顺子
public static List<byte[]> TackOutSequence(byte[] iSourcePokers, int iSourcePokersCount)
{
//5张以下不做处理
if (iSourcePokersCount < 5)
{
return null;
}
//先做一个排序(小到大)
for (int i = 0; i < iSourcePokersCount-1; i++)
{
for (int j = 0; j < iSourcePokersCount; j++)
{
if (GetPokerNum(iSourcePokers[i])>GetPokerNum(iSourcePokers[j]))
{
byte temp = iSourcePokers[i];
iSourcePokers[i] = iSourcePokers[j];
iSourcePokers[j] = temp;
}
}
}
List<byte[]> resultList = new List<byte[]>();//返回结果列表
List<byte> cache = new List<byte>(); //缓存列表
//找到顺子加入缓存列表
for (int i = 0; i < iSourcePokersCount - 1; i++)
{
if (i > 0 && GetPokerNum(iSourcePokers[i])==GetPokerNum(iSourcePokers[i-1]))//元素相同进入下个循环
{
continue;
}
cache.Clear();
cache.Add(iSourcePokers[i]);
int count = 1; //顺子间隔
for (int j = 0; j < iSourcePokersCount; j++)
{
if (GetPokerNum(iSourcePokers[i]) + count == GetPokerNum(iSourcePokers[j]))
{
cache.Add(iSourcePokers[j]);
count++;
if (cache.Count == 5) //只记录5个的顺子
{
resultList.Add(cache.ToArray());
break;
}
}
}
//剩余牌数小于5直接退出
if (iSourcePokersCount - i <= 5)
{
break;
}
}
return resultList;
}
#endregion
#region//获取同花
public static List<byte[]> TackOutFlush(byte[] iSourcePokers, int iSourcePokersCount)
{
//5张以下不做处理
if (iSourcePokersCount < 5)
{
return null;
}
List<byte[]> resultList = new List<byte[]>();//返回结果列表
List<byte> cache = new List<byte>(); //缓存列表
//找到同花加入缓存列表
for (int i = 0; i < iSourcePokersCount - 1; i++)
{
cache.Clear();
cache.Add(iSourcePokers[i]);
for (int j = 0; j < iSourcePokersCount; j++)
{
if (GetPokerHua(iSourcePokers[i]) == GetPokerHua(iSourcePokers[j]))
{
cache.Add(iSourcePokers[j]);
}
if (cache.Count == 5)
{
resultList.Add(cache.ToArray());
break;
}
}
}
return resultList;
}
#endregion
#region//获取葫芦
public static List<byte[]> TackOutThree_Double(byte[] iSourcePokers, int iSourcePokersCount)
{
//5张以下不做处理
if (iSourcePokersCount < 5)
{
return null;
}
List<byte[]> resultList = new List<byte[]>();//返回结果列表
//找到3条、对子加入缓存列表
List<byte[]> threeList = TackOutThree(iSourcePokers, iSourcePokersCount);
List<byte[]> doubleList = TackOutDouble(iSourcePokers, iSourcePokersCount);
if (threeList!=null&&doubleList!=null&&threeList.Count>0&&doubleList.Count>0)
{
//找任意组合成3带2
for (int i = 0; i < threeList.Count; i++)
{
for (int j = 0; j < doubleList.Count; j++)
{
if (GetPokerNum(threeList[i][0]) != GetPokerNum(doubleList[j][0]))//牌值不能相同
{
byte[] nCache = new byte[doubleList[j].Length + threeList[i].Length];
Array.Copy(threeList[i], 0, nCache,0, threeList[i].Length);
Array.Copy(doubleList[j], 0, nCache, threeList[i].Length, doubleList[j].Length);
resultList.Add(nCache);
}
}
}
}
return resultList;
}
#endregion
#region//获取同花顺
public static List<byte[]> TackOutFlush_Sequence(byte[] iSourcePokers, int iSourcePokersCount)
{
//5张以下不做处理
if (iSourcePokersCount < 5)
{
return null;
}
List<byte[]> resultList = new List<byte[]>();//返回结果列表
List<byte[]> sequenceList = TackOutSequence(iSourcePokers, iSourcePokersCount);//5张顺子列表
if (sequenceList!=null&&sequenceList.Count>0)
{
//判断顺子是否同花
for (int i = 0; i < sequenceList.Count; i++)
{
int hua = GetPokerHua(sequenceList[i][0]); //记录第一个花色
for (int j = 0; j < sequenceList[i].Length; j++)
{
if (hua==GetPokerHua(sequenceList[i][j]))
{
if (j == sequenceList.Count - 1)
{
resultList.Add(sequenceList[i]);
}
else
break;
}
}
}
}
return resultList;
}
#endregion
}
}
扑克牌逻辑算法
最新推荐文章于 2025-02-10 18:05:59 发布