(德州撲克誕生記)2、我是怎麼來滴?

/*
 * 源代碼
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 德州撲克
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 是否重複,應用在發牌過程

        /// 後來沒有用上
        /// </summary>
        /// <param name="ir">牌碼</param>
        /// <returns></returns>
        bool IsRepeat(string[] ir)
        {
            for (int i = 0; i < ir.Length - 1; i++)
            {
                for (int j = i + 1; j < ir.Length; j++)
                {
                    if (i == j) continue;
                    if (ir[i] == ir[j]) return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 牌圖設置
        /// </summary>
        void evaluate()
        {
            ptb1.ImageLocation = path + pic1 + extend;
            ptb2.ImageLocation = path + pic2 + extend;
            ptb3.ImageLocation = path + pic3 + extend;
            ptb4.ImageLocation = path + pic4 + extend;
            ptb5.ImageLocation = path + pic5 + extend;
        }
       
        /// <summary>
        /// 發牌函數庫,你能看懂麼,看不懂不怪你,我都覺得太亂了,水平太菜了,相信你也知道如何優化了。
        /// </summary>
        void rand()
        {

            int s = unchecked((int)DateTime.Now.Ticks);//unchecked知道幹什麼嗎?不要僞隨機數

            string[] temp = new string[5];
            string[] pics = new string[5];
            //生成第1張隨機,首先判斷是否已經翻開
            if (ptb1.ImageLocation == path + "bg" + extend)
            {
                Ti += 1050;
                pics[0] = pic1;
                Random ra = new Random(++Ti + (++s));
                pic1 = ra.Next(1, 5).ToString();//第一張牌獲取隨機數
               
                while (true)
                {
                start:
                    ;
                    Ti += 3;
                    ra = new Random(++Ti + (++s));
                    temps[0] = ra.Next(1, 14).ToString();//任取一張牌
                    switch (temps[0])
                    {
                        case "10": temp[0] = "t"; break;
                        case "11": temp[0] = "j"; break;
                        case "12": temp[0] = "q"; break;
                        case "13": temp[0] = "k"; break;
                        default: temp[0] = temps[0]; break;
                    }
                    pic1 = pic1[0].ToString() + temp[0];//第一張預生成
                   //重複性判斷,比較弱智
                    if (temps[0] == temps[1])
                    {
                        if (pic1 == pic2)
                        {
                            goto start;
                        }
                    }
                    if (temps[0] == temps[2])
                    {
                        if (pic1 == pic3)
                        {
                            goto start;
                        }
                    }
                    if (temps[0] == temps[3])
                    {
                        if (pic1 == pic4)
                        {
                            goto start;
                        }
                    }
                    if (temps[0] == temps[4])
                    {
                        if (pic1 == pic5)
                        {
                            goto start;
                        }
                    }
                    if (pics[0] != " " && pics[1] != " " && pics[2] != " " && pics[3] != " " && pics[4] != " ")
                    {
                        if (pics[0] == pics[1] || pics[0] == pics[2] || pics[0] == pics[3] || pics[0] == pics[4])
                        {
                            goto start;//如果都不爲空,且只要一個相同就重複發牌。goto我一定會扔掉
                        }
                    }
                    break;
                }
               
            }
            //生成第2張隨機
            if (ptb2.ImageLocation == path + "bg" + extend)
            {
                Ti += 850;
                pics[1] = pic2;
                Random ra = new Random(++Ti + (++s));
                pic2 = ra.Next(1, 5).ToString();
                while (true)
                {
                start:
                    ;
                    Ti += 570;
                    ra = new Random(++Ti + (++s));
                    temps[1] = ra.Next(1, 14).ToString();
                    switch (temps[1])
                    {
                        case "10": temp[1] = "t"; break;
                        case "11": temp[1] = "j"; break;
                        case "12": temp[1] = "q"; break;
                        case "13": temp[1] = "k"; break;
                        default: temp[1] = temps[1]; break;
                    }
                    pic2 = pic2[0].ToString() + temp[1];
                    if (temps[1] == temps[0])
                    {
                        if (pic2 == pic1)
                        {
                            goto start;
                        }
                    }
                    if (temps[1] == temps[2])
                    {
                        if (pic2 == pic3)
                        {
                            goto start;
                        }
                    }
                    if (temps[1] == temps[3])
                    {
                        if (pic2 == pic4)
                        {
                            goto start;
                        }
                    }
                    if (temps[1] == temps[4])
                    {
                        if (pic2 == pic5)
                        {
                            goto start;
                        }
                    }
                    if (pics[0] != " " && pics[1] != " " && pics[2] != " " && pics[3] != " " && pics[4] != " ")
                    {
                        if (pics[1] == pics[0] || pics[1] == pics[2] || pics[1] == pics[3] || pics[1] == pics[4])
                        {
                            goto start;
                        }
                    }
                    break;
                }
               
            }
          
            //生成第3張隨機
            if (ptb3.ImageLocation == path + "bg" + extend)
            {
                Ti += 790;
                pics[2] = pic3;
                Random ra = new Random(++Ti + (++s));
                pic3 = ra.Next(1, 5).ToString();//獲取牌型
                while (true)
                {
                start:
                    ;
                    Ti += 210;
                    ra = new Random(++Ti + (++s));
                    temps[2] = ra.Next(1, 14).ToString();//獲取牌號
                    switch (temps[2])
                    {
                        case "10": temp[2] = "t"; break;
                        case "11": temp[2] = "j"; break;
                        case "12": temp[2] = "q"; break;
                        case "13": temp[2] = "k"; break;
                        default: temp[2] = temps[2]; break;
                    }
                    pic3 = pic3[0].ToString() + temp[2];
                    if (temps[2] == temps[0])
                    {
                        if (pic3 == pic1)
                        {
                            goto start;
                        }
                    }
                    if (temps[2] == temps[1])
                    {
                        if (pic3 == pic2)
                        {
                            goto start;
                        }
                    }
                    if (temps[2] == temps[3])
                    {
                        if (pic3 == pic4)
                        {
                            goto start;
                        }
                    }
                    if (temps[2] == temps[4])
                    {
                        if (pic3 == pic5)
                        {
                            goto start;
                        }
                    }
                    if (pics[0] != " " && pics[1] != " " && pics[2] != " " && pics[3] != " " && pics[4] != " ")
                    {
                        if (pics[2] == pics[1] || pics[2] == pics[0] || pics[2] == pics[3] || pics[2] == pics[4])
                        {
                            goto start;
                        }
                    }
                    break;
                }
            }
            //生成第4張隨機
            if (ptb4.ImageLocation == path + "bg" + extend)
            {
                Ti += 530;
                pics[3] = pic4;
                Random ra = new Random(++Ti + (++s));
                pic4 = ra.Next(1, 5).ToString();
                while (true)
                {
                start:
                    ;
                    Ti += 150;
                    ra = new Random(++Ti + (++s));
                    temps[3] = ra.Next(1, 14).ToString();
                    switch (temps[3])
                    {
                        case "10": temp[3] = "t"; break;
                        case "11": temp[3] = "j"; break;
                        case "12": temp[3] = "q"; break;
                        case "13": temp[3] = "k"; break;
                        default: temp[3] = temps[3]; break;
                    }
                    pic4 = pic4[0].ToString() + temp[3];
                    if (temps[3] == temps[0])
                    {
                        if (pic4 == pic1)
                        {
                            goto start;
                        }
                    }
                    if (temps[3] == temps[1])
                    {
                        if (pic4 == pic2)
                        {
                            goto start;
                        }
                    }
                    if (temps[3] == temps[2])
                    {
                        if (pic4 == pic3)
                        {
                            goto start;
                        }
                    }
                    if (temps[3] == temps[4])
                    {
                        if (pic4 == pic5)
                        {
                            goto start;
                        }
                    }
                    if (pics[0] != " " && pics[1] != " " && pics[2] != " " && pics[3] != " " && pics[4] != " ")
                    {
                        if (pics[3] == pics[1] || pics[3] == pics[2] || pics[3] == pics[0] || pics[3] == pics[4])
                        {
                            goto start;
                        }
                    }
                    break;
                }
              
            }
            //生成第5張隨機
            if (ptb5.ImageLocation == path + "bg" + extend)
            {
                Ti += 270;
                pics[4] = pic5;
                Random ra = new Random(++Ti + (++s));
                pic5 = ra.Next(1, 5).ToString();
                while (true)
                {
                start:
                    ;
                    Ti += 0;
                    ra = new Random(++Ti + (++s));
                    temps[4] = ra.Next(1, 14).ToString();
                    switch (temps[4])
                    {
                        case "10": temp[4] = "t"; break;
                        case "11": temp[4] = "j"; break;
                        case "12": temp[4] = "q"; break;
                        case "13": temp[4] = "k"; break;
                        default: temp[4] = temps[4]; break;
                    }
                    pic5 = pic5[0].ToString() + temp[4];
                    if (temps[4] == temps[0])
                    {
                        if (pic5 == pic1)
                        {
                            goto start;
                        }
                    }
                    if (temps[4] == temps[1])
                    {
                        if (pic5 == pic2)
                        {
                            goto start;
                        }
                    }
                    if (temps[4] == temps[2])
                    {
                        if (pic5 == pic3)
                        {
                            goto start;
                        }
                    }
                    if (temps[4] == temps[3])
                    {
                        if (pic5 == pic4)
                        {
                            goto start;
                        }
                    }
                    if (pics[0] != " " && pics[1] != " " && pics[2] != " " && pics[3] != " " && pics[4] != " ")
                    {
                        if (pics[4] == pics[1] || pics[4] == pics[2] || pics[4] == pics[3] || pics[4] == pics[0])
                        {
                            goto start;
                        }
                    }
                    break;
                }
            }          
        }

        //一大堆的變量,看到了吧,根本沒有高內聚,不封裝,不靈活,改個業務你試試,累死你,新手一般容易犯這個毛病,手比腦袋快。
        string df = ""; //得分
        int Ti = 50; //種子
        int time = 0;//已玩次數
        string[] temps = new string[5];
        int marking = 10000; //初始化初時分數
        string extend = ".jpg";//擴展名
        string path = "image//";//路徑
        static string pic1 = " ", pic2 = " ", pic3 = " ", pic4 = " ", pic5 = " ";//5變量,存儲圖片的代碼
        static bool p1 = true, p2 = true, p3 = true, p4 = true, p5 = true;


        #region 界面操作事件處理
        private void Form1_Load(object sender, EventArgs e)
        {

            //初始加載
            txtmarking.Text = marking.ToString(); //初始化得分
            lbllevel.Text = "局數:" + (++time).ToString() + "局"; //初始化局數
            ptb1.ImageLocation = path + "bg" + extend;
            ptb2.ImageLocation = path + "bg" + extend;
            ptb3.ImageLocation = path + "bg" + extend;
            ptb4.ImageLocation = path + "bg" + extend;
            ptb5.ImageLocation = path + "bg" + extend;
            rand();
            p1 = p2 = p3 = p4 = p5 = false;
        }
       

        
        /// <summary>
        /// 點擊第一張牌
        ///代碼重用啊,太杯具了

        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ptb1_Click(object sender, EventArgs e)
        {
            if (ptb1.ImageLocation != path + pic1 + extend)
            {
                ptb1.ImageLocation = path + pic1 + extend;
                p1 = false;
            }
            else
            {
                ptb1.ImageLocation = path + "bg" + extend;
                p1 = true;
            }
        
        }

        /// <summary>
        /// 點擊第二張牌
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ptb2_Click(object sender, EventArgs e)
        {
            if (ptb2.ImageLocation != path + pic2 + extend)
            {
                ptb2.ImageLocation = path + pic2 + extend;
                p2 = false;
            }
            else
            {
                ptb2.ImageLocation = path + "bg" + extend;
                p2 = true;
            }

        }

        /// <summary>
        /// 點擊第三張牌
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ptb3_Click(object sender, EventArgs e)
        {
            if (ptb3.ImageLocation != path + pic3 + extend)
            {
                ptb3.ImageLocation = path + pic3 + extend;
                p3 = false;
            }
            else
            {
                ptb3.ImageLocation = path + "bg" + extend;
                p3 = true;
            }

        }

        /// <summary>
        /// 點擊第四張牌
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ptb4_Click(object sender, EventArgs e)
        {
            if (ptb4.ImageLocation != path + pic4 + extend)
            {
                ptb4.ImageLocation = path + pic4 + extend;
                p4 = false;
            }
            else
            {
                ptb4.ImageLocation = path + "bg" + extend;
                p4 = true;
            }
        }


        /// <summary>
        /// 點擊第五張牌
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ptb5_Click(object sender, EventArgs e)
        {
            if (ptb5.ImageLocation != path + pic5 + extend)
            {
                ptb5.ImageLocation = path + pic5 + extend;
                p5 = false;
            }
            else
            {
                ptb5.ImageLocation = path + "bg" + extend;
                p5 = true;
            }
        }

        /// <summary>
        /// 下一輪
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnnext_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(txtmarking.Text) <= 0)
            {
                lblresult.Text = "抱歉,你已經沒有金幣了!";
                btnnext.Enabled = false;
            }
            else
            {
                btnok.Enabled = true;


                txtmultiple.Text = "";
                txtmoney.Text = "";


                p1 = p2 = p3 = p4 = p5 = true;

                ptb1.ImageLocation = path + "bg" + extend;
                ptb2.ImageLocation = path + "bg" + extend;
                ptb3.ImageLocation = path + "bg" + extend;
                ptb4.ImageLocation = path + "bg" + extend;
                ptb5.ImageLocation = path + "bg" + extend;
                rand();
                p1 = p2 = p3 = p4 = p5 = false;
                lbllevel.Text = "局數:" + time++.ToString() + "局";
                lblresult.Text = "";
                btnnext.Enabled = false;
                nummoney.Enabled = true;
            }
        }
        /// <summary>
        /// 換牌
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnend_Click(object sender, EventArgs e)
        {

            rand();
            evaluate();
            ptb1.Enabled = false;
            ptb2.Enabled = false;
            ptb3.Enabled = false;
            ptb4.Enabled = false;
            ptb5.Enabled = false;

            btnend.Enabled = false;
            System.Threading.Thread.Sleep(100);

            //首先獲取圖片序列,準備開牌了,格式化數據輸入
            string s = "";
            // s = pic1[1].ToString() + "," + pic2[1].ToString() + "," + pic3[1].ToString() + "," + pic4[1].ToString() + "," + pic5[1].ToString();
            s = s + "," + parserType(pic1[0].ToString()) + parserValue(pic1[1].ToString());
            s = s + "," + parserType(pic2[0].ToString()) + parserValue(pic2[1].ToString());
            s = s + "," + parserType(pic3[0].ToString()) + parserValue(pic3[1].ToString());
            s = s + "," + parserType(pic4[0].ToString()) + parserValue(pic4[1].ToString());
            s = s + "," + parserType(pic5[0].ToString()) + parserValue(pic5[1].ToString());

            s = "P" + s;

           //開牌
            Fwin(wins(s), s);


            btnnext.Enabled = true;
        }

        /// <summary>
        /// 開牌,一組五張牌,隨機分,不能有重複
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnok_Click(object sender, EventArgs e)
        {
            if (nummoney.Value > Convert.ToInt32(txtmarking.Text))
            {
                nummoney.Value = Convert.ToInt32(txtmarking.Text);
            }
            btnok.Enabled = false;
            btnend.Enabled = true;
            nummoney.Enabled = false;

            evaluate();

            ptb1.Enabled = true;
            ptb2.Enabled = true;
            ptb3.Enabled = true;
            ptb4.Enabled = true;
            ptb5.Enabled = true;
        }

        /// <summary>
        /// 排名算法,專門爲合作推广部使用;

        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSort_Click(object sender, EventArgs e)
        {
            frmSort fr = new frmSort();
            fr.ShowDialog();
        }


        #endregion

        /// <summary>
        /// 多態wins,對於輸入的五張牌進行類型的判斷
        /// 和上面的函數對比,封裝類算法,調用新的類庫,看看這是如何封裝的,程序員如果沒有抽象概念,就不會理解封裝,也就不能體會到重用的妙處
        /// </summary>
        /// <param name="s">五張牌序列</param>
        /// <returns>類型</returns>
        string wins(string s)
        {
            string ret = "";
            ret = (Lib.DeZhouPuKe.checkPaiType(s)).ToString();
            return ret;
        }
        /// <summary>
        /// 翻牌之後的處理,投注以及界面元素的邏輯處理,規則可以繼續抽象出來,形成接口,再形成服務,規則可以配置,規則引擎就是這麼來的,根本沒有那麼玄乎。
        /// </summary>
        /// <param name="win">牌的類型</param>
        /// <param name="sLabel">輔助顯示文字</param>
        void Fwin(string win,string sLabel)
        {
            if (win != "高牌")
            {
                lblresult.Text = "恭喜你,本次牌爲:" + win;
                if (win == "皇家同花順")
                {
                    txtmultiple.Text = "90";//投注的90倍獎勵
                    txtmoney.Text = (nummoney.Value * 90).ToString();
                    df = "+" + txtmoney.Text;
                    txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
                }
                if (win == "同花順")
                {
                    txtmultiple.Text = "80";//投注的80倍獎勵
                    txtmoney.Text = (nummoney.Value * 80).ToString();
                    df = "+" + txtmoney.Text;
                    txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
                }
                if (win == "四條")
                {
                    txtmultiple.Text = "70";//投注的70倍獎勵
                    txtmoney.Text=(nummoney.Value*70).ToString();
                    df = "+" + txtmoney.Text;
                    txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
                }
                if (win == "葫蘆")
                {
                    txtmultiple.Text = "60";//投注的60倍獎勵
                    txtmoney.Text = (nummoney.Value * 60).ToString();
                    df = "+" + txtmoney.Text;
                    txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
                }

                if (win == "同花")
                {
                    txtmultiple.Text = "50";//投注的50倍獎勵
                    txtmoney.Text = (nummoney.Value * 50).ToString();
                    df = "+" + txtmoney.Text;
                    txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
                }

                if (win == "順子")
                {
                    txtmultiple.Text = "40";//投注的40倍獎勵
                    txtmoney.Text = (nummoney.Value * 40).ToString();
                    df = "+" + txtmoney.Text;
                    txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
                }
                if (win == "三條")
                {
                    txtmultiple.Text = "30";//投注的30倍獎勵
                    txtmoney.Text = (nummoney.Value * 30).ToString();
                    df = "+" + txtmoney.Text;
                    txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
                }
                if (win == "兩對")
                {
                    txtmultiple.Text = "20";//投注的20倍獎勵
                    txtmoney.Text = (nummoney.Value * 20).ToString();
                    df = "+" + txtmoney.Text;
                    txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
                }
                if (win == "一對")
                {
                    txtmultiple.Text = "10";//投注的10倍獎勵
                    txtmoney.Text = (nummoney.Value * 10).ToString();
                    df = "+" + txtmoney.Text;
                    txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
                }
            }
            else
            {
                //如果是高牌
                lblresult.Text =  win;
                txtmultiple.Text = "0";//沒有獎勵,並扣除投注數量的50倍
                txtmoney.Text = (nummoney.Value * 50).ToString();
                txtmoney.Text = "-" + nummoney.Value.ToString();
                df = txtmoney.Text;
                txtmarking.Text = (Convert.ToInt32(txtmarking.Text) + Convert.ToInt32(txtmoney.Text)).ToString();
            }
            lblmarking.Text = "上局得分:" + df;
        }

        /// <summary>
        /// 牌型的轉換,用來在計算翻牌之後的數據解析處理,小工具一個
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private string parserType(string s)
        {
            string ret = "";
            if (s.Equals("1"))
            {
                ret = "a";
            }
            else if (s.Equals("2"))
            {
                ret = "b";
            }
            else if (s.Equals("3"))
            {
                ret = "c";
            }

            else if (s.Equals("4"))
            {
                ret = "d";
            }
            else
            {
                ret = "m";
            }

            return ret;
        }
        /// <summary>
        /// 牌點的轉換,用來在計算翻牌之後的數據解析處理,小工具一個
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private string parserValue(string s)
        {
            string ret = "";
            if (s.Equals("j"))
            {
                ret = "11";
            }
            else if (s.Equals("q"))
            {
                ret = "12";
            }
            else if (s.Equals("k"))
            {
                ret = "13";
            }
            else if (s.Equals("t"))
            {
                ret = "10";
            }
            else
            {
                ret = s;
            }
            return ret;
        }      
        }

 

}

 

評價,基本功能實現了,但是代碼一看就是堆上去的,效率很差,關鍵是沒有面向對象的思想,看不到軟件複用的痕跡,除了,除了,除了。。。那個函數:Lib.DeZhouPuKe.checkPaiType(s)

參看系列3 我的心哪

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章