问题描述
publicclassPoker{publicstringColor{get;set;}publicstringCard{get;set;}publicintValue{get;set;}}
publicclassPokerCard{privateDictionary<string,List<Poker>>_poker=newDictionary<string,List<Poker>>();publicDictionary<string,List<Poker>>Role=null;privatevoidFillPoker(){_poker=newDictionary<string,List<Poker>>{{"方块",newList<Poker>{newPoker{Color="方块",Card="3",Value=3},newPoker{Color="方块",Card="4",Value=4},newPoker{Color="方块",Card="5",Value=5},newPoker{Color="方块",Card="6",Value=6},newPoker{Color="方块",Card="7",Value=7},newPoker{Color="方块",Card="8",Value=8},newPoker{Color="方块",Card="9",Value=9},newPoker{Color="方块",Card="10",Value=10},newPoker{Color="方块",Card="J",Value=11},newPoker{Color="方块",Card="Q",Value=12},newPoker{Color="方块",Card="K",Value=13},newPoker{Color="方块",Card="A",Value=14},newPoker{Color="方块",Card="2",Value=15}}},{"红桃",newList<Poker>{newPoker{Color="红桃",Card="3",Value=3},newPoker{Color="红桃",Card="4",Value=4},newPoker{Color="红桃",Card="5",Value=5},newPoker{Color="红桃",Card="6",Value=6},newPoker{Color="红桃",Card="7",Value=7},newPoker{Color="红桃",Card="8",Value=8},newPoker{Color="红桃",Card="9",Value=9},newPoker{Color="红桃",Card="10",Value=10},newPoker{Color="红桃",Card="J",Value=11},newPoker{Color="红桃",Card="Q",Value=12},newPoker{Color="红桃",Card="K",Value=13},newPoker{Color="红桃",Card="A",Value=14},newPoker{Color="红桃",Card="2",Value=15}}},{"黑桃",newList<Poker>{newPoker{Color="黑桃",Card="3",Value=3},newPoker{Color="黑桃",Card="4",Value=4},newPoker{Color="黑桃",Card="5",Value=5},newPoker{Color="黑桃",Card="6",Value=6},newPoker{Color="黑桃",Card="7",Value=7},newPoker{Color="黑桃",Card="8",Value=8},newPoker{Color="黑桃",Card="9",Value=9},newPoker{Color="黑桃",Card="10",Value=10},newPoker{Color="黑桃",Card="J",Value=11},newPoker{Color="黑桃",Card="Q",Value=12},newPoker{Color="黑桃",Card="K",Value=13},newPoker{Color="黑桃",Card="A",Value=14},newPoker{Color="黑桃",Card="2",Value=15}}},{"梅花",newList<Poker>{newPoker{Color="梅花",Card="3",Value=3},newPoker{Color="梅花",Card="4",Value=4},newPoker{Color="梅花",Card="5",Value=5},newPoker{Color="梅花",Card="6",Value=6},newPoker{Color="梅花",Card="7",Value=7},newPoker{Color="梅花",Card="8",Value=8},newPoker{Color="梅花",Card="9",Value=9},newPoker{Color="梅花",Card="10",Value=10},newPoker{Color="梅花",Card="J",Value=11},newPoker{Color="梅花",Card="Q",Value=12},newPoker{Color="梅花",Card="K",Value=13},newPoker{Color="梅花",Card="A",Value=14},newPoker{Color="梅花",Card="2",Value=15}}},{"Joker",newList<Poker>{newPoker{Color="Joker",Card="SmallJoker",Value=16},newPoker{Color="Joker",Card="BigJoker",Value=16}}}};}publicvoidShuffleCard(){FillPoker();varrandom=newRandom();Role=newDictionary<string,List<Poker>>();for(vari=0;i<51;i++){//ToDo从4种花色加Joker中随机抽取一种没分配完的花色varcolour=_poker.Where(x=>x.Value.Count!=0).Select(x=>x.Key).ToArray();varselectColor=colour[random.Next(colour.Length)];//ToDo从选定的花色中随机抽取一张没有分配过的牌varselectValue=_poker[selectColor][random.Next(_poker[selectColor].Count)];if((i+1)%3==0){if(Role.ContainsKey("角色C:"))Role["角色C:"].Add(selectValue);elseRole.Add("角色C:",newList<Poker>{selectValue});}elseif((i+1)%2==0){if(Role.ContainsKey("角色B:"))Role["角色B:"].Add(selectValue);elseRole.Add("角色B:",newList<Poker>{selectValue});}else{if(Role.ContainsKey("角色A:"))Role["角色A:"].Add(selectValue);elseRole.Add("角色A:",newList<Poker>{selectValue});}//ToDo移除已被分配的牌_poker[selectColor].Remove(selectValue);}//ToDo取出未分配的牌,存入底牌_poker=_poker.Where(x=>x.Value.Count!=0).ToDictionary(x=>x.Key,y=>y.Value);foreach(vartin_poker.SelectMany(item=>item.Value)){if(Role.ContainsKey("底牌"))Role["底牌"].Add(t);elseRole.Add("底牌",newList<Poker>{t});}}}
调用staticvoidMain(string[]args){varpokerCard=newPokerCard();pokerCard.ShuffleCard();Console.Write("角色A:");pokerCard.Role["角色A:"].OrderBy(x=>x.Value).ToList().ForEach(x=>Console.Write(x.Color+x.Card+""));Console.WriteLine("rn");Console.Write("角色B:");pokerCard.Role["角色B:"].OrderBy(x=>x.Value).ToList().ForEach(x=>Console.Write(x.Color+x.Card+""));Console.WriteLine("rn");Console.Write("角色C:");pokerCard.Role["角色C:"].OrderBy(x=>x.Value).ToList().ForEach(x=>Console.Write(x.Color+x.Card+""));Console.WriteLine("rn");Console.Write("底牌:");pokerCard.Role["底牌"].OrderBy(x=>x.Value).ToList().ForEach(x=>Console.Write(x.Color+x.Card+""));Console.WriteLine("rn");Console.ReadLine();}
结果
解决方案
解决方案二:
看见算法,进来膜拜一下~~
解决方案三:
不要给每个人随机发牌你应该随机打乱整个牌的次序,然后之后就按顺序发牌就好了
解决方案四:
引用2楼Z65443344的回复:
不要给每个人随机发牌你应该随机打乱整个牌的次序,然后之后就按顺序发牌就好了
引用2楼Z65443344的回复:
不要给每个人随机发牌你应该随机打乱整个牌的次序,然后之后就按顺序发牌就好了
我就没想到这样。而且我觉得那代码太长了
解决方案五:
staticvoidMain(string[]args){Func<int,string>getName=p=>{vars=new[]{"黑桃","红桃","梅花","方块"};varn=new[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};returnp<52?s[p/13]+n[p%13]:p==52?"JokerSmallJoker":"JokerBigJoker";};varbuf=newbyte[54];using(varrnd=newRNGCryptoServiceProvider())rnd.GetBytes(buf);varps=Enumerable.Range(0,buf.Length).OrderBy(p=>buf[p]).Select((p,i)=>new{p,i}).GroupBy(t=>t.i>50?3:t.i%3,t=>t.p).OrderBy(g=>g.Key);varr=new[]{"角色A","角色B","角色C","底牌"};foreach(varginps){Console.Write(r[g.Key]+":");foreach(varping)Console.Write(getName(p)+"");Console.WriteLine("rn");}}
解决方案六:
不要随机发牌,应该在每轮发牌中加入权值,如果权值超过设定值,从没发出去的牌中挑一张和权值相匹配的牌。这样你才能给付费用户发更好的牌。
解决方案七:
varlist=newList(Poker);//数据填充list=list.OrderBy(r=>Guid.NewGuid()).ToList();//怎么发就随便你了直接一切三,跳着发,都可以
解决方案八:
staticstring[]PokerValues=newstring[]{"3","4","5","6","7","8","9","10","J","Q","K","A","2"};staticDictionary<byte,string>PokerColors=newDictionary<byte,string>(){{0,"方片"},{1,"红桃"},{2,"梅花"},{3,"黑桃"}};staticvoidR(){List<Poker>list=newList<Poker>();foreach(stringpinPokerValues){foreach(bytecinPokerColors.Keys){list.Add(newPoker(){Color=c,Value=p});}}list.Add(newPoker(){Color=5,Value="JQ"});//小鬼list.Add(newPoker(){Color=6,Value="JQ"});//大鬼list=list.OrderBy(p=>Guid.NewGuid()).ToList();Func<byte,string>colorFunc=(i)=>{switch(i){case5:return"小";case6:return"大";default:returnPokerColors[i];}};foreach(varpinlist){Console.Write(string.Format("Color:{0}Value:{1}",colorFunc(p.Color),p.Value));}}publicclassPoker{publicbyteColor{get;set;}publicstringValue{get;set;}}
只有初始化加打乱顺序的代码
解决方案九:
额,完全多余的写法1.54张牌没必要先分什么花色,就是1--52个数字,判定花色直接取和13商,牌点则是和13模.53,54为大小王2.54张牌直接乱序排序见6楼3.发牌先扣3张底牌,然后按index模3分组
解决方案十:
引用8楼wanghui0380的回复:
额,完全多余的写法1.54张牌没必要先分什么花色,就是1--52个数字,判定花色直接取和13商,牌点则是和13模.53,54为大小王2.54张牌直接乱序排序见6楼3.发牌先扣3张底牌,然后按index模3分组
好办法。