这是我自己定义的一个关于牌的数据结构.
进行洗牌.主要是对牌进行一种随机的交换
在写的过程中,参考了这个贴子.http://www.cnblogs.com/blackzh/archive/2006/07/13/450036.html
public class Poker
{
public Int32 PokerIndex;//这张牌在整副牌中的索引.
public Int32 PokerValue;//这张牌的字面大小.
public Poker() { }
public Poker(Int32 PokerIndex, Int32 PokerValue)
{
this.PokerIndex = PokerIndex;
this.PokerValue = PokerValue;
}
}
然后将牌分成四部分.即三个玩家在未成为地主的时候,手上的17张牌.另外是三张地主牌.{
public Int32 PokerIndex;//这张牌在整副牌中的索引.
public Int32 PokerValue;//这张牌的字面大小.
public Poker() { }
public Poker(Int32 PokerIndex, Int32 PokerValue)
{
this.PokerIndex = PokerIndex;
this.PokerValue = PokerValue;
}
}
protected Poker[] pk = new Poker[54];//所有的牌
protected Poker[] pk1 = new Poker[17];//发给第一个人的牌
protected Poker[] pk2 = new Poker[17];//发给第二个人的牌
protected Poker[] pk3 = new Poker[17];//发给第三个人的牌
protected Poker[] pkTemp = new Poker[3];//最后三张
然后产生整副牌protected Poker[] pk1 = new Poker[17];//发给第一个人的牌
protected Poker[] pk2 = new Poker[17];//发给第二个人的牌
protected Poker[] pk3 = new Poker[17];//发给第三个人的牌
protected Poker[] pkTemp = new Poker[3];//最后三张
//产生牌
public void InitPk()
{
for (Int32 index = 0; index < pk.Length; index++)
{
pk[index] = new Poker();
pk[index].PokerIndex = index;
pk[index].PokerValue = index;
}
}
public void InitPk()
{
for (Int32 index = 0; index < pk.Length; index++)
{
pk[index] = new Poker();
pk[index].PokerIndex = index;
pk[index].PokerValue = index;
}
}
进行洗牌.主要是对牌进行一种随机的交换
//洗牌
public void Random_Sequence()
{
int m, n;
Random ram = new Random();
for (int i = 0; i < 1000; i++)
{
m = ram.Next(0, 54);
n = ram.Next(0, 54);
if (m != n)
{
this.Permute(m, n);
}
}
}
//置换牌
public void Permute(Int32 a, Int32 b)
{
Poker temp;
temp = pk[a];
pk[a] = pk[b];
pk[b] = temp;
}
然后将牌分成四份.public void Random_Sequence()
{
int m, n;
Random ram = new Random();
for (int i = 0; i < 1000; i++)
{
m = ram.Next(0, 54);
n = ram.Next(0, 54);
if (m != n)
{
this.Permute(m, n);
}
}
}
//置换牌
public void Permute(Int32 a, Int32 b)
{
Poker temp;
temp = pk[a];
pk[a] = pk[b];
pk[b] = temp;
}
//发牌
public void Deal_cards()
{
for (int i = 0; i < 17; i++)
{
pk1[i] = pk[i * 3];
pk2[i] = pk[i * 3 + 1];
pk3[i] = pk[i * 3 + 2];
}
pkTemp[0] = pk[51];
pkTemp[1] = pk[52];
pkTemp[2] = pk[53];
}
对生成的牌进行排序.public void Deal_cards()
{
for (int i = 0; i < 17; i++)
{
pk1[i] = pk[i * 3];
pk2[i] = pk[i * 3 + 1];
pk3[i] = pk[i * 3 + 2];
}
pkTemp[0] = pk[51];
pkTemp[1] = pk[52];
pkTemp[2] = pk[53];
}
//发牌
public void Deal_cards()
{
for (int i = 0; i < 17; i++)
{
pk1[i] = pk[i * 3];
pk2[i] = pk[i * 3 + 1];
pk3[i] = pk[i * 3 + 2];
}
pkTemp[0] = pk[51];
pkTemp[1] = pk[52];
pkTemp[2] = pk[53];
}
//对牌排序
public Poker[] Order_cards(Poker[] arr)
{
Int32 c;
for (Int32 index = 0; index < arr.Length; index++)
{
Math.DivRem(arr[index].PokerIndex, 13, out c);
if (arr[index].PokerIndex == 53)
{
arr[index].PokerValue = 16;
}
else if (arr[index].PokerIndex == 52)
{
arr[index].PokerValue = 15;
}
else
{
if (c == 0)
{
arr[index].PokerValue = 13;
}
else if (c == 1)
{
arr[index].PokerValue = 14;
}
else
{
arr[index].PokerValue = c;
}
}
}
Sort(arr, arr.Length);
return arr;
}
//排序
public void Sort(Poker[] arr, Int32 n)
{
Int32 i, j, k;
Poker temp;
for (i = 0; i < n; ++i)
{
j = i;
for (k = i + 1; k < n; ++k)
{
if (arr[j].PokerValue < arr[k].PokerValue)//如果前个数比后个数大,则交换位置。
{
temp = arr[k];
arr[k] = arr[j];
arr[j] = temp;
}
}
}
}
public void AfterSort()
{
pk1 = Order_cards(pk1);
pk2 = Order_cards(pk2);
pk3 = Order_cards(pk3);
}
public void Deal_cards()
{
for (int i = 0; i < 17; i++)
{
pk1[i] = pk[i * 3];
pk2[i] = pk[i * 3 + 1];
pk3[i] = pk[i * 3 + 2];
}
pkTemp[0] = pk[51];
pkTemp[1] = pk[52];
pkTemp[2] = pk[53];
}
//对牌排序
public Poker[] Order_cards(Poker[] arr)
{
Int32 c;
for (Int32 index = 0; index < arr.Length; index++)
{
Math.DivRem(arr[index].PokerIndex, 13, out c);
if (arr[index].PokerIndex == 53)
{
arr[index].PokerValue = 16;
}
else if (arr[index].PokerIndex == 52)
{
arr[index].PokerValue = 15;
}
else
{
if (c == 0)
{
arr[index].PokerValue = 13;
}
else if (c == 1)
{
arr[index].PokerValue = 14;
}
else
{
arr[index].PokerValue = c;
}
}
}
Sort(arr, arr.Length);
return arr;
}
//排序
public void Sort(Poker[] arr, Int32 n)
{
Int32 i, j, k;
Poker temp;
for (i = 0; i < n; ++i)
{
j = i;
for (k = i + 1; k < n; ++k)
{
if (arr[j].PokerValue < arr[k].PokerValue)//如果前个数比后个数大,则交换位置。
{
temp = arr[k];
arr[k] = arr[j];
arr[j] = temp;
}
}
}
}
public void AfterSort()
{
pk1 = Order_cards(pk1);
pk2 = Order_cards(pk2);
pk3 = Order_cards(pk3);
}
在写的过程中,参考了这个贴子.http://www.cnblogs.com/blackzh/archive/2006/07/13/450036.html