每次都不调用方法

时间:2013-07-01 13:38:25

标签: c# .net debugging

我正在制作一个扑克游戏,在我开始开发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);
            }
        }        
    }
}

目前正在使用Console进行测试。

我的问题是游戏在开发的这个阶段做了它倾向于做的事情,但是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;
        }

    }
}

1 个答案:

答案 0 :(得分:1)

只有一种方法Winner没有被调用 - 程序因为异常而在到达之前停止执行。将Main中的代码修改为:

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

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