方法不是每次都被调用

Posted

技术标签:

【中文标题】方法不是每次都被调用【英文标题】:Method not being called everytime 【发布时间】:2013-07-01 13:38:24 【问题描述】:

我目前正在开发一款扑克游戏,我只是在开始开发 GUI 之前测试游戏的逻辑,我注意到 Winner() 方法并不是每次都被调用.

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

using CardGamev1;

namespace Poker 
    public class Program 
        static Rank rank = new Rank();
        static void Main(string[] args) 
            SetPlayers();
            WriteCards();
            SeeWhoWins();
            Winner();
            Console.ReadKey();
        

        static void SetPlayers() 
            rank.Deal.InitialisePlayers();
            rank.Deal.DealPlayerCards(2);
            rank.Deal.DealTableCards(5);
        


        static void WriteCards() 
            for (int i = 0; i < rank.Deal.NumberOfPlayers; i++) 
                Console.Write("\nPlayer " + (i + 1) + " Cards:");
                for (int j = 0; j < rank.Deal.Players[i].PlayerCards.Count; j++) 
                    Console.Write("\n\t" + rank.Deal.Players[i].PlayerCards[j].FaceName + " of " + rank.Deal.Players[i].PlayerCards[j].SuitName);
                
            
            Console.Write("\nThe Table Cards:");
            for (int i = 0; i < rank.Deal.TableCards.Count; i++) 
                Console.Write("\n\t" + rank.Deal.TableCards[i].FaceName + " of " + rank.Deal.TableCards[i].SuitName);
            

        

        static void SeeWhoWins() 
            rank.Deal.AddDataToSortLists();
            rank.DetermineWinner(); 
        

        static void Winner()             
            string winningHand = "";
            Console.Write("\n\nThe winner is: ");
            for (int i = 0; i < rank.Winners.Count; i++) 
                winningHand = rank.PlayerHandRank(rank.Deal.Players[rank.Winners[i]].HandRank[0]);
                Console.Write("\nPlayer " + (rank.Winners[i]+1) + " with " + winningHand);
            
                
    

目前正在使用控制台进行测试。

我的问题是游戏在开发的这个阶段做了它倾向于做的事情,但是Winner() 随机不会在我运行程序时调用。

有谁知道这个问题。

注意:如果您需要查看更多课程,我可以编辑我的问题,但我觉得这可能会使问题过于混乱。

附录 - 等级等级(正在进行中)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CardGamev1 
    public class Rank 
        public Deal Deal = new Deal();
        private int firstValue = 0;
        private int secondValue = 0;
        private int thirdValue = 0;
        private int flushCounter = 1;

        private int straightCounter = 1;
        private int firstConsequtiveNum = 1;
        private int secondConsequtiveNum = 1;
        private int thirdConsequtiveNum = 1;
        private bool highCard = false;
        private bool royalFlush = false;
        private bool straightFlush = false;
        private bool straight = false;
        private bool flush = false;

        private bool nextPair = false;
        private bool finalPair = false;
        private bool twoPair = false;
        private bool pair = false;
        private bool threeKind = false;
        private bool fourKind = false;
        private bool fullHouse = false;

        private List<int> winners = new List<int>();
        public List<int> Winners 
            get 
                return winners;
            
        

        public Rank() 

        

        /// <summary>
        /// This method is a higher level algoritm method which will eventually check who is the winner
        /// </summary>
        public void DetermineWinner() 
            for (int i = 0; i < Deal.NumberOfPlayers; i++) 
                firstValue = 0;
                secondValue = 0;
                thirdValue = 0;
                flushCounter = 1;
                straightCounter = 1;
                firstConsequtiveNum = 1;
                secondConsequtiveNum = 1;
                thirdConsequtiveNum = 1;


                royalFlush = false;
                straightFlush = false;
                straight = false;
                flush = false;
                nextPair = false;
                finalPair = false;
                twoPair = false;
                pair = false;
                threeKind = false;
                fourKind = false;
                fullHouse = false;
                Deal.SortDeckByValue(Deal.Players[i]);
                Deal.SortDeckBySuit(Deal.Players[i]);

                CheckIfFlush(Deal.Players[i]);

                CheckForValue(Deal.Players[i]);

                CheckForOutcome(Deal.Players[i]);
            
            CheckForWinner();
        

        /// <summary>
        /// This method will check if the player's card has a flush
        /// </summary>
        /// <param name="who">The player</param>
        private void CheckIfFlush(Player who) 
            for (int j = 0; j < who.SuitSortedHand.Count - 1; j++) 
                if (who.SuitSortedHand[j].SuitValue == who.SuitSortedHand[j + 1].SuitValue) 
                    flushCounter++;
                 else 
                    flushCounter = 1;
                 if (flushCounter == 5) 
                    flush = true;
                
            
        


        /// <summary>
        /// This method will check if the player's card has any
        /// </summary>
        /// <param name="who"></param>
        private void CheckForValue(Player who) 
            int highCardIndex = who.ValueSortedHand.Count - 1;
            for (int j = 0; j < who.ValueSortedHand.Count - 1; j++) 
                if ((who.ValueSortedHand[j].FaceValue + 10) == who.ValueSortedHand[j + 1].FaceValue) 
                    straightCounter++;
                    if (straightCounter == 5) 
                        who.HandRank[1] = who.ValueSortedHand[j + 1].FaceValue;
                        if (flushCounter == 5 && who.HandRank[1] == 140) 
                            royalFlush = true;
                         else if (flushCounter == 5) 
                            straightFlush = true;
                         else 
                            straight = true;
                        
                    
                    SortPairs(who);

                 else if (who.ValueSortedHand[j].FaceValue != who.ValueSortedHand[j + 1].FaceValue) 
                    straightCounter = 1;
                    SortPairs(who);

                 else if (who.ValueSortedHand[j].FaceValue == who.ValueSortedHand[j + 1].FaceValue) 
                    straightCounter = 1;
                    if (!nextPair) 
                        firstConsequtiveNum++;
                        firstValue = who.ValueSortedHand[j].FaceValue;
                     else if (nextPair && !finalPair) 
                        secondConsequtiveNum++;
                        secondValue = who.ValueSortedHand[j].FaceValue;
                     else if (finalPair) 
                        thirdConsequtiveNum++;
                        thirdValue = who.ValueSortedHand[j].FaceValue;
                    
                 if (j == who.ValueSortedHand.Count - 2) 

                    SortPairs(who);
                    CheckIfConsequtive();
                    do 
                        highCard = true;
                        if (secondConsequtiveNum == 1) 
                            who.HandRank[2] = who.ValueSortedHand[j + 1].FaceValue;
                            if (who.HandRank[1] == who.HandRank[2]) 
                                highCardIndex--;
                                highCard = false;
                            
                        
                     while (!highCard);
                
            
        

        /// <summary>
        /// This method will sort out any consequtive cards (i.e. pairs) to be evaluated later
        /// </summary>
        /// <param name="who"></param>
        private void SortPairs(Player who) 
            int temp;
            if (firstConsequtiveNum > 1 && !nextPair) 
                nextPair = true;
                who.HandRank[1] = firstValue;
             else if (secondConsequtiveNum > 1 && !finalPair) 
                finalPair = true;
                if (secondConsequtiveNum > firstConsequtiveNum) 
                    temp = firstConsequtiveNum;
                    who.HandRank[2] = who.HandRank[1];
                    who.HandRank[1] = secondValue;
                    firstConsequtiveNum = secondConsequtiveNum;
                    secondConsequtiveNum = temp;
                 else if (secondValue > firstValue) 
                    who.HandRank[2] = who.HandRank[1];
                    who.HandRank[1] = secondValue;
                    firstConsequtiveNum = secondConsequtiveNum;
                 else 
                    who.HandRank[2] = secondValue;
                
             else if (thirdConsequtiveNum > 1) 
                if (thirdConsequtiveNum > firstConsequtiveNum) 
                    temp = firstConsequtiveNum;
                    who.HandRank[2] = who.HandRank[1];
                    who.HandRank[1] = firstValue;
                    firstConsequtiveNum = thirdConsequtiveNum;
                    secondConsequtiveNum = temp;
                 else if (thirdConsequtiveNum == firstConsequtiveNum) 
                    if (thirdValue > firstValue) 
                        who.HandRank[2] = who.HandRank[1];
                        who.HandRank[1] = thirdValue;
                     else if (thirdValue > secondValue) 
                        who.HandRank[2] = thirdValue;
                        secondConsequtiveNum = thirdConsequtiveNum;
                    
                 else if (thirdConsequtiveNum == secondConsequtiveNum) 
                    if (thirdValue > secondValue) 
                        who.HandRank[2] = thirdValue;
                    
                
            
        

        /// <summary>
        /// This method will check if there is any consequtive cards and determine their ranks.
        /// </summary>
        private void CheckIfConsequtive() 
            if (firstConsequtiveNum == 4) 
                fourKind = true;
             else if (firstConsequtiveNum == 3 && secondConsequtiveNum == 2) 
                fullHouse = true;
             else if (firstConsequtiveNum == 3) 
                threeKind = true;
             else if (firstConsequtiveNum == 2 && secondConsequtiveNum == 2) 
                twoPair = true;
             else if (firstConsequtiveNum == 2) 
                pair = true;
            
        


        /// <summary>
        /// This method will check what the player has in terms of hand rank
        /// </summary>
        /// <param name="who"></param>
        private void CheckForOutcome(Player who) 
            if (royalFlush)  // if royal flush
                who.HandRank[0] = 9;
             else if (straightFlush)  // if straight flush
                who.HandRank[0] = 8;
             else if (fourKind)  // if four of a kind
                who.HandRank[0] = 7;
             else if (fullHouse)  // if full house
                who.HandRank[0] = 6;
             else if (flush)  // if flush
                who.HandRank[0] = 5;
             else if (straight)  // if straight
                who.HandRank[0] = 4;
             else if (threeKind)  // if three of a kind
                who.HandRank[0] = 3;
             else if (twoPair)  // if two pair
                who.HandRank[0] = 2;
             else if (pair)  // if pair
                who.HandRank[0] = 1;
            
        


        /// <summary>
        /// This method will check who is the winner
        /// </summary>
        private void CheckForWinner() 
            //Work in progress
            int BestHand = -1;
            int HighCard = -1;
            for (int i = 0; i < Deal.NumberOfPlayers; i++) 
                if (Deal.Players[i].HandRank[0] > BestHand) 
                    winners.Clear();
                    BestHand = Deal.Players[i].HandRank[0];
                    HighCard = Deal.Players[i].HandRank[2];
                    winners.Add(i);
                 else if (Deal.Players[i].HandRank[0] == BestHand) 
                    if (Deal.Players[i].HandRank[2] == HighCard) 
                        winners.Add(i);
                     else if (Deal.Players[i].HandRank[2] > HighCard) 
                        winners.Clear();
                        BestHand = Deal.Players[i].HandRank[0];
                        HighCard = Deal.Players[i].HandRank[2];
                        winners.Add(i);
                    
                
            
        

        public string PlayerHandRank(int rank) 
            string rankName = "";
            switch (rank) 
                case 9:
                    rankName = "Royal Flush";
                    break;
                case 8:
                    rankName = "Straight Flush";
                    break;
                case 7:
                    rankName = "Four of a Kind";
                    break;
                case 6:
                    rankName = "Full House";
                    break;
                case 5:
                    rankName = "Flush";
                    break;
                case 4:
                    rankName = "Straight";
                    break;
                case 3:
                    rankName = "Three of a Kind";
                    break;
                case 2:
                    rankName = "Two Pair";
                    break;
                case 1:
                    rankName = "Pair";
                    break;
                case 0:
                    rankName = "High Card";
                    break;
            
            return rankName;
        

    

【问题讨论】:

您是否在调试器中单步执行了几次代码,直到出现这种情况?这很可能会告诉你为什么它并不总是被调用。 输出窗口有异常吗? rank.Winners.Count 可能为 0 吗? 不会调用Winner 的唯一原因是您的程序没有完成——要么是因为你没有向我们展示逻辑,要么更可能是因为异常。在启用“中断所有异常”的情况下进行调试,看看你的错误在哪里。 我真是个白痴。有一个无限循环,我没有正确设置值。 【参考方案1】:

只有一种方法不会调用Winner - 由于异常,程序在到达之前停止执行。将Main 中的代码修改为:

static void Main(string[] args) 
    try
    
        SetPlayers();
        WriteCards();
        SeeWhoWins();
        Winner();
        Console.ReadKey();
    
    catch (Exception ex)
    
        Console.WriteLine(ex.ToString());
    

这将为您提供进一步开始调试程序所需的所有详细信息。你会得到一个行号和一切。

【讨论】:

谢谢你给我看,原来我忘记改变程序中的一个变量以防止发生无限循环但多亏了你,问题解决了。 @Brian,很高兴能为您提供帮助!

以上是关于方法不是每次都被调用的主要内容,如果未能解决你的问题,请参考以下文章

在iOS“Spring”动画库中,每次调用“animate()”方法之前是不是需要设置“animation”属性?

面向对象封装案例

PHP常用的魔术方法及规则

03-面向对象封装案例

如何使streamHandler每次都被覆盖

[Python]面向对象封装案例