1

私は現在ポーカー ゲームに取り組んでおり、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;
        }

    }
}
4

1 に答える 1

1

呼び出されない方法Winnerが 1 つだけあります。例外のために、プログラムがそこに到達する前に実行を停止しています。Mainこれにコードを変更します。

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

これにより、プログラムのデバッグをさらに開始するために必要なすべての詳細が得られます。行番号とすべてを取得します。

于 2013-07-01T13:54:42.100 に答える