1. 程式人生 > 實用技巧 >Silverfish重構【1】-發現卡牌的函式

Silverfish重構【1】-發現卡牌的函式

前言

假期估計時間會比較多,能重構多少是多少,主要還是鍛鍊一下自己的思維和程式設計能力。由於能力和精力有限,可能會有錯誤,希望大家能夠指出。

今天新開始從playfield中抽牌的函式動手吧,很多小白不理解引數的意思,索性全部寫死。廢話不多說,直接上程式碼。

由於我是雲玩家,很多卡牌觸發與否和觸發順序不太清楚,希望大家指出錯誤。

        /// <summary>
        /// 發現卡牌等(凡是不從牌庫抽的都呼叫這個方法)
        /// </summary>
        /// <param name="ss">發現的牌的名字</param>
/// <param name="own">是否是自己發現</param> public void DiscoverACard(CardDB.cardName ss, bool own) { CardDB.cardName s = ss; // cant hold more than 10 cards if (own) { if (s == CardDB.cardName.unknown) {
if (this.owncards.Count >= 10) { this.evaluatePenality += 15; return; } this.owncarddraw++; } else { if (this.owncards.Count >= 10
) { this.evaluatePenality += 5; return; } this.owncarddraw++; } } else { if (s == CardDB.cardName.unknown) { if (this.enemyAnzCards >= 10) { this.evaluatePenality -= (this.turnCounter > 2) ? 20 : 50; return; } this.enemycarddraw++; this.enemyAnzCards++; } else { if (this.enemyAnzCards >= 10) { this.evaluatePenality -= (this.turnCounter > 2) ? 20 : 50; return; } this.enemycarddraw++; this.enemyAnzCards++; } this.triggerCardsChanged(false); //據說克洛瑪古斯不觸發 //if (anzEnemyChromaggus > 0 && s == CardDB.cardName.unknown) //{ // for (int i = 1; i <= anzEnemyChromaggus; i++) // { // if (this.enemyAnzCards >= 10) // { // this.evaluatePenality -= (this.turnCounter > 2) ? 20 : 50; // return; // } // this.enemycarddraw++; // this.enemyAnzCards++; // this.triggerCardsChanged(false); // } //} return; } if (s == CardDB.cardName.unknown) { CardDB.Card c = CardDB.Instance.getCardData(s); Handmanager.Handcard hc = new Handmanager.Handcard { card = c, position = this.owncards.Count + 1, manacost = 1000, entity = this.getNextEntity() }; this.owncards.Add(hc); this.triggerCardsChanged(true); } else { CardDB.Card c = CardDB.Instance.getCardData(s); Handmanager.Handcard hc = new Handmanager.Handcard { card = c, position = this.owncards.Count + 1, manacost = c.calculateManaCost(this), entity = this.getNextEntity() }; this.owncards.Add(hc); this.triggerCardsChanged(true); } //據說克洛瑪古斯不觸發 //if (anzOwnChromaggus > 0 && s == CardDB.cardName.unknown) //{ // CardDB.Card c = CardDB.Instance.getCardData(s); // for (int i = 1; i <= anzOwnChromaggus; i++) // { // if (this.owncards.Count >= 10) // { // this.evaluatePenality += 15; // return; // } // this.owncarddraw++; // Handmanager.Handcard hc = new Handmanager.Handcard { card = c, position = this.owncards.Count + 1, manacost = 1000, entity = this.getNextEntity() }; // this.owncards.Add(hc); // this.triggerCardsChanged(true); // } //} }

        /// <summary>
        /// 發現卡牌等(凡是不從牌庫抽的都呼叫這個方法)
        /// </summary>
        /// <param name="ss">發現的牌的ID</param>
        /// <param name="own">是否是自己發現</param>
        public void drawACard(CardDB.cardIDEnum ss, bool own)
        {
            CardDB.cardIDEnum s = ss;

            // cant hold more than 10 cards

            if (own)
            {
                if (s == CardDB.cardIDEnum.None)
                {
                    if (this.owncards.Count >= 10)
                    {
                        this.evaluatePenality += 15;
                        return;
                    }
                    this.owncarddraw++;
                }
                else
                {
                    if (this.owncards.Count >= 10)
                    {
                        this.evaluatePenality += 5;
                        return;
                    }
                    this.owncarddraw++;
                }
            }
            else
            {
                if (s == CardDB.cardIDEnum.None)
                {
                    if (this.enemyAnzCards >= 10)
                    {
                        this.evaluatePenality -= (this.turnCounter > 2) ? 20 : 50;
                        return;
                    }
                    this.enemycarddraw++;
                    this.enemyAnzCards++;
                }
                else
                {
                    if (this.enemyAnzCards >= 10)
                    {
                        this.evaluatePenality -= (this.turnCounter > 2) ? 20 : 50;
                        return;
                    }
                    this.enemycarddraw++;
                    this.enemyAnzCards++;

                }
                this.triggerCardsChanged(false);

                //if (anzEnemyChromaggus > 0 && s == CardDB.cardIDEnum.None)
                //{
                //    for (int i = 1; i <= anzEnemyChromaggus; i++)
                //    {
                //        if (this.enemyAnzCards >= 10)
                //        {
                //            this.evaluatePenality -= (this.turnCounter > 2) ? 20 : 50;
                //            return;
                //        }
                //        this.enemycarddraw++;
                //        this.enemyAnzCards++;
                //        this.triggerCardsChanged(false);
                //    }
                //}
                return;
            }

            if (s == CardDB.cardIDEnum.None)
            {
                CardDB.Card c = CardDB.Instance.getCardDataFromID(s);
                Handmanager.Handcard hc = new Handmanager.Handcard { card = c, position = this.owncards.Count + 1, manacost = 1000, entity = this.getNextEntity() };
                this.owncards.Add(hc);
                this.triggerCardsChanged(true);
            }
            else
            {
                CardDB.Card c = CardDB.Instance.getCardDataFromID(s);
                Handmanager.Handcard hc = new Handmanager.Handcard { card = c, position = this.owncards.Count + 1, manacost = c.calculateManaCost(this), entity = this.getNextEntity() };
                this.owncards.Add(hc);
                this.triggerCardsChanged(true);
            }

            //if (anzOwnChromaggus > 0 && s == CardDB.cardIDEnum.None && !nopen)
            //{
            //    CardDB.Card c = CardDB.Instance.getCardDataFromID(s);
            //    for (int i = 1; i <= anzOwnChromaggus; i++)
            //    {
            //        if (this.owncards.Count >= 10)
            //        {
            //            this.evaluatePenality += 15;
            //            return;
            //        }
            //        this.owncarddraw++;

            //        Handmanager.Handcard hc = new Handmanager.Handcard { card = c, position = this.owncards.Count + 1, manacost = 1000, entity = this.getNextEntity() };
            //        this.owncards.Add(hc);
            //        this.triggerCardsChanged(true);
            //    }
            //}

        }