博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

扑克牌的随机发牌程序

Posted on 2006-07-13 17:11  blackzh  阅读(6475)  评论(4编辑  收藏  举报
  打算写一个扑克牌游戏(准确的说就是“斗地主”),本来是打算取0-53的随机数,取过的就做一个标识,循环直至全部完。但是细想一下,效率实在是太低了,最后决定用乱序的方式,感觉还不错。不知道还有没有更好的办法,请大家多多指教!

using
 System;
using System.Collections;
namespace ConsoleApplication11
{
    
/// <summary>
    
/// RandomTEST 的摘要说明。
    
/// </summary>
    public class RandomTest
    {        
        
//0-12表示黑桃A到K;
        
//13-25表示红桃A到K;
        
//26-38表示梅花A到K;
        
//39-51表示方块A到K;
        
//52表示小鬼,53表示大鬼;
        public string man_a;
        
public string man_b;
        
public string man_c;
        
public string remnant;
        
private    int[] array = new int[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53};
        
//乱序算法
        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(int a,int b)
        {
            array[a]
=array[a]+array[b];
            array[b]
=array[a]-array[b];
            array[a]
=array[a]-array[b];
        }
        
//发牌
        public void deal_cards()
        {
            
for (int i=0;i<17;i++)
            {    
                man_a
=man_a+" "+array[i*3].ToString();
                man_b
=man_b+" "+array[i*3+1].ToString();
                man_c
=man_c+" "+array[i*3+2].ToString();
            }
            remnant
=array[51].ToString()+" "+array[52].ToString()+" "+array[53].ToString();
        }
    }
    
public class GameStart
    {
        [STAThread]
        
static void Main(string[] args)
        {
            RandomTest rt
= new RandomTest();
            rt.Random_Sequence();
            rt.deal_cards();
            Console.WriteLine(
"玩家甲的牌型:"+rt.man_a.ToString());
            Console.WriteLine(
"玩家乙的牌型:"+rt.man_b.ToString());
            Console.WriteLine(
"玩家丙的牌型:"+rt.man_c.ToString());
            Console.WriteLine(
"剩余的牌型:"+rt.remnant.ToString());
            Console.ReadLine();
        }
    }
}