简体   繁体   中英

Method not being called everytime

I'm working on a poker game at the moment, in which I am just testing out the logic of the game before I begin developing a GUI, and I have noticed that the Winner() method isn't being called everytime.

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);
            }
        }        
    }
}

It is currently being tested using Console.

My problem is that the game does what it is tended to do at this stage in development but the Winner() randomly doesn't call whenever I run the program.

Does anyone know the problem to this.

Note: if you need to see more classes, I can edit my question but I felt that it may clutter the question too much.

Appendix - Rank Class (work in progress)

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;
        }

    }
}

There is only one way Winner isn't getting called - the program is stopping execution before getting there because of an exception. Modify you're code in Main to this:

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

and that will give you all the detail you need to further begin debugging your program. You'll get a line number and everything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM