C#扑克洗牌,52个不同数字,随机放到4个13个数的数组里去-(分享)

kingmax54212008 2009-10-01 05:53:20
static void Main(string[] args)
{




List<int> pokerlist = new List<int>();
int index = 0;
int rdindex = 0;

for (int t = 1,s=0; t <= 52; t++,s++)
{
pokerlist.Add(t);
}
foreach (int i in pokerlist)
{
Console.WriteLine(" i= " + i);
}
for(int j=0; j< pokerlist.Count; j++)
{
Console.WriteLine(" j= " + j);
Console.WriteLine("pokerlist[" + j.ToString() + "]=; " + pokerlist[j].ToString());
}

Console.WriteLine("pokerlist length: " + pokerlist.Count);
Console.WriteLine("-----------------------Starting to shuffle-------------------------");

int[] out1 = new int[13];
int[] out2 = new int[13];
int[] out3 = new int[13];
int[] out4 = new int[13];

Random rd = new Random(pokerlist.Count);

while (pokerlist.Count > 0)
{

if (pokerlist.Count <= 52 && pokerlist.Count > 39)
{
rdindex = rd.Next(pokerlist.Count);
out1[index] = pokerlist[rdindex];
pokerlist.RemoveAt(rdindex);
index++;
if (index == 13)
{
index = 0;
}
}

if (pokerlist.Count <= 39 && pokerlist.Count > 26)
{
rdindex = rd.Next(pokerlist.Count);
out2[index] = pokerlist[rdindex];
pokerlist.RemoveAt(rdindex);
index++;
if (index == 13)
{
index = 0;
}
}
if (pokerlist.Count <= 26 && pokerlist.Count > 13)
{
rdindex = rd.Next(pokerlist.Count);
out3[index] = pokerlist[rdindex];
pokerlist.RemoveAt(rdindex);
index++;
if (index == 13)
{
index = 0;
}
}
if (pokerlist.Count <= 13 && pokerlist.Count > 0)
{
rdindex = rd.Next(pokerlist.Count);
out4[index] = pokerlist[rdindex];
pokerlist.RemoveAt(rdindex);
index++;
if (index == 13)
{
index = 0;
}
}
}
foreach (int k in out1)
{
Console.WriteLine("out1 :" + k.ToString());
}
foreach (int k in out2)
{
Console.WriteLine("out2 :" + k.ToString());
}
foreach (int k in out3)
{
Console.WriteLine("out3 :" + k.ToString());
}
foreach (int k in out4)
{
Console.WriteLine("out4 :" + k.ToString());
}

Console.WriteLine("-----------------------Finish shuffling----------------------------");
Console.ReadLine();

}
...全文
296 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
luotuocao9 2010-08-19
  • 打赏
  • 举报
回复
我又更完善的代码。
可实现以下功能:
1、52牌模拟洗牌,随机发给4家;
2、将每家13张牌按花色和大小排序;
3、按东、西、南、北4个玩家显示;
4、循环,产生下一付牌。

这个程序本身就可以作为桥牌的发牌程序。
有需要的加我。
kent4j 2009-10-02
  • 打赏
  • 举报
回复
太复杂 没必要
BlackPointofSun 2009-10-02
  • 打赏
  • 举报
回复
为什么不放在一个1个长度52的数组
shine9027 2009-10-02
  • 打赏
  • 举报
回复
bucuo ......
shine9027 2009-10-02
  • 打赏
  • 举报
回复
bucuo ......heheheh
qqiuzaihui 2009-10-02
  • 打赏
  • 举报
回复
C#入门经典那本书中也有个扑克洗牌的实例, 楼主也可以去看看.
Z782282738 2009-10-02
  • 打赏
  • 举报
回复
没那么复杂吧。
lizheng19860824 2009-10-02
  • 打赏
  • 举报
回复
我是来得十分的
a8352081 2009-10-02
  • 打赏
  • 举报
回复
一共才4个数组,直接随机1-4不就行了么,虽然取出来的数是123456789...,但是取出来的数放在哪个数组是随机的,我觉得这样效率高些,也不用那么长代码...
tian_dao_chou_qin 2009-10-01
  • 打赏
  • 举报
回复
lz这个太复杂了点吧,呵呵

国庆快乐,呵呵

lnwuyaowei 2009-10-01
  • 打赏
  • 举报
回复
支持原创.
threenewbee 2009-10-01
  • 打赏
  • 举报
回复
接分。

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧