2 sgyimkf sgyimkf 于 2015.06.05 19:45 提问

代码没提示有错误,点调试什么也没有,一个比较扑克牌大小的程序
  public class Poker
    {
        public string Suit { get; set; }
        public string Value { get; set; }
        public  Poker()
        {
            int m;
            Random r1 = new Random();
            m = r1.Next(1, 5);
            int n;
            Random r2 = new Random();
            n = r2.Next(1, 14);
            if (m == 1)
                Suit = "黑桃";
            else if (m == 2)
                Suit = "红桃";
            else if (m == 3)
                Suit = "方块";
            else
                Suit = "梅花";
            if (n == 1)
                Value = "A";
            if (n == 2)
                Value = "2";
            if (n == 3)
                Value = "3";
            if (n == 4)
                Value = "4";
            if (n == 5)
                Value = "5";
            if (n == 6)
                Value = "6";
            if (n == 7)
                Value = "7";
            if (n == 8)
                Value = "8";
            if (n == 9)
                Value = "9";
            if (n == 10)
                Value = "10";
            if (n == 11)
                Value = "J";
            if (n == 12)
                Value = "Q";
            else
                Value = "K";
        }       
    }
 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;

        }
        public int GetValue(Poker ob)
        {
            if (ob.Value.Equals("A"))
                return 1;
            else if (ob.Value.Equals("2"))
                return 2;
            else if (ob.Value.Equals("3"))
                return 3;
            else if (ob.Value.Equals("4"))
                return 4;
            else if (ob.Value.Equals("5"))
                return 5;
            else if (ob.Value.Equals("6"))
                return 6;
            else if (ob.Value.Equals("7"))
                return 7;
            else if (ob.Value.Equals("8"))
                return 8;
            else if (ob.Value.Equals("9"))
                return 9;
            else if (ob.Value.Equals("10"))
                return 10;
            else if (ob.Value.Equals("J"))
                return 11;
            else if (ob.Value.Equals("Q"))
                return 12;
            else
                return 13;
        }//方法:获取牌的大小

        public static Poker[] Shuffle()
        {           
            Poker[] pokers = new Poker[52];           
            for (int i = 0; i < 52; i++)
            {            
                pokers[i] = new Poker();                
                    for (int u = 0; u < i; u++)
                    {
                        while (pokers[u].Suit.Equals(pokers[i].Suit) == true && pokers[u].Value.Equals(pokers[i].Value) == true)
                        {
                            pokers[i] = null;
                            pokers[i] = new Poker();
                            u = 0;
                            continue;
                        }
                    }
            }              
            return pokers;            
        }//方法:洗牌,生成一副牌

        Poker[] pokers = Shuffle();   
        int click1_time = 1;
        Poker[] gamerpoker = new Poker[26];
        Poker[] AIpoker = new Poker[26];

        private void button1_Click(object sender, EventArgs e)
        {
            if (click1_time == 26)
                MessageBox.Show("没有牌了!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (click1_time < 26)
            {
                listBox1.Items.Add("???");
                AIpoker[click1_time] = pokers[2 * click1_time - 2];
                gamerpoker[click1_time] = pokers[2 * click1_time-1];
                listBox2.Items.Add(gamerpoker[click1_time-1].Suit + gamerpoker[click1_time-1].Value);
                click1_time++;
            }
        }//按顺序摸牌
        int click2_time = 0;
        int AIpoint = 0;
        int gamerpoint = 0;
        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.SelectedIndex = click2_time;
            listBox2.SelectedIndex = click2_time;
            listBox1.Items[click2_time] = AIpoker[click2_time].Suit + AIpoker[click2_time].Value;

            if (GetValue(AIpoker[click2_time]) > GetValue(gamerpoker[click2_time]))
            {
                AIpoint += 1;
                label5.Text = "电脑+1";
                label3.Text += "{0},AIpoint";
            }
            if (GetValue(AIpoker[click2_time]) < GetValue(gamerpoker[click2_time]))
            {
                gamerpoint += 1;
                label5.Text = "玩家+1";
                label4.Text += "{0},gamerpoint";
            }
            click2_time++;
        }//比较大小并计分    
    }

2个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.06.05 20:56
已采纳
生成牌的效率太低了,这重复率得多高,运行个几小时可能能初始化完52张牌
 public static Poker[] Shuffle()
        {           
            Poker[] pokers = new Poker[52];           
            for (int i = 0; i < 52; i++)
            {            
                pokers[i] = new Poker();                
                    for (int u = 0; u < i; u++)
                    {
                        while (pokers[u].Suit.Equals(pokers[i].Suit) == true && pokers[u].Value.Equals(pokers[i].Value) == true)
                        {
                            pokers[i] = null;
                            pokers[i] = new Poker();
                            u = 0;
                            continue;
                        }
                    }
            }              
            return pokers;            
        }//方法:洗牌,生成一副牌

改改方法

 class PokerContainer
    {
        private static PokerContainer pc = new PokerContainer();
        private List<string> pokerList = new List<string>();
        private string[] hsArr = new string[] { "黑桃", "红桃", "方块", "梅花" };
        private string[] numArr = new string[] { "A", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
        public static PokerContainer getInstance(){
            return pc;
        }
        public void init()
        {
            for (int i = 1; i < 5; i++)
            {
                for (int j = 1; j <= 13; j++)
                {
                    pokerList.Add(hsArr[i - 1] + "," + numArr[j]);
                }
            }
        }
        public string[] getOne()
        {
            int m;
            Random r1 = new Random(10);
            m = r1.Next(1, pokerList.Count + 1);
            string[] arr = getInfo(pokerList[m-1]);
            pokerList.RemoveAt(m-1);
            return arr;
        }
        private string[] getInfo(string info)
        {
            string[] arr = info.Split(',');
            return arr;
            //Suit = arr[0];
            //Value = arr[1];
        }
        public PokerContainer()
        {
            init();
        }
    }

        public class Poker
    {
        public string Suit { get; set; }
        public string Value { get; set; }

        public Poker()
        {
            string[] arr = PokerContainer.getInstance().getOne();
            Suit = arr[0];
            Value = arr[1];
        }
    }

        最后去掉while比较
        public static Poker[] Shuffle()
        {
            Poker[] pokers = new Poker[52];
            for (int i = 0; i < 52; i++)
            {
                pokers[i] = new Poker();
            }
            return pokers;
        }//方法:洗牌,生成一副牌
sgyimkf
sgyimkf 谢谢,后来也发现了,只要把生成牌的那段代码注释掉就可以正常调试了
2 年多之前 回复
danielinbiti
danielinbiti 自己再优化随机生成方法
2 年多之前 回复
sgyimkf
sgyimkf   2015.06.05 19:52

图片说明

Csdn user default icon
上传中...
上传图片
插入图片