zoukankan      html  css  js  c++  java
  • App:Poker Deck Sort

    A deck pokers have 52 pieces except small king and big king, which with four different suit. We want to generate 52 pieces of pokers

    and sort them by suit first or by value first.

    We make a rule that is Spade>Heart>Diamond>Club, assign values (4,3,2,1) to each of them.

    And anthter rule is code '2'<'3'<'4',...,'10'<'J'<'Q'<'K'<'A' with related values 2,3,4,...,10,11,12,13,14.

    We should design architecture of the application. We need Poker entity class, PokerManager utility class and the invoker program class.

    Details is below.

    And now, we look into the detail implemental of each class.

    First shows Poker class, which contains four constant values of suit(Spade,Heart,Diamond,Club),properties and some methods.

    The primary memthods are CompareSuitFirstTo and CompareValueFirstTo, which supply how to compare two pokers.

    using System;
    namespace PokerDeck
    {
        /// <summary>
        
    /// Poker entity
        
    /// </summary>
        class Poker
        {
            public const char Spade = '';
            public const char Heart = '';
            public const char Diamond = '';
            public const char Club = '';
            public int Suit { getprivate set; }
            public int Value { getprivate set; }
            public string Mark { getprivate set; }

            /// <summary>
            
    /// Initializes a new instance of Poker class
            
    /// </summary>
            
    /// <param name="mark"></param>
            public Poker(string mark)
            {
                if (mark == null || mark.Length < 2)
                {
                    throw new ArgumentException();
                }

                this.Mark = mark;
                switch (mark[0])
                {
                    case Spade:
                        this.Suit = 4;
                        break;
                    case Heart:
                        this.Suit = 3;
                        break;
                    case Diamond:
                        this.Suit = 2;
                        break;
                    case Club:
                        this.Suit = 1;
                        break;
                }

                if (mark[1] >= '2' && mark[1] <= '9')
                {
                    this.Value = mark[1] - '2' + 2;
                }
                else if (mark[1] == '1' && mark.Length > 2 && mark[2] == '0')
                {
                    this.Value = 10;
                }
                else
                {
                    switch (mark[1])
                    {
                        case 'J':
                            this.Value = 11;
                            break;
                        case 'Q':
                            this.Value = 12;
                            break;
                        case 'K':
                            this.Value = 13;
                            break;
                        case 'A':
                            this.Value = 14;
                            break;
                    }
                }

                if (this.Suit <= 0 || this.Value <= 0)
                {
                    throw new ArgumentException();
                }
            }

            /// <summary>
            
    /// Compares two Pokers, suit first.
            
    /// </summary>
            
    /// <param name="other"></param>
            
    /// <returns></returns>
            public int CompareSuitFirstTo(Poker other)
            {
                int result = this.Suit - other.Suit;
                if (0 == result)
                {
                    result = this.Value - other.Value;
                }

                return result;
            }

            /// <summary>
            
    /// Compares two Pokers, value first
            
    /// </summary>
            
    /// <param name="other"></param>
            
    /// <returns></returns>
            public int CompareValueFirstTo(Poker other)
            {
                int result = this.Value - other.Value;
                if (0 == result)
                {
                    result = this.Suit - other.Suit;
                }

                return result;
            }

        }
    }

    Second, PokerManager utility class conatins serveral functions, such as CreatePoker,WritePoker,ReadPoker,SortBySuitFirst,SortByValueFirst.

    I choose the QuickSort algorithms to sort pokers.

    using System;
    using System.Collections.Generic;
    using System.IO;

    namespace PokerDeck
    {
        /// <summary>
        
    /// Poker manager
        
    /// </summary>
        class PokerManager
        {
            /// <summary>
            
    /// Creates a deck of pokers
            
    /// </summary>
            
    /// <returns></returns>
            public static Poker[] CreatePoker()
            {
                Poker[] pokers = new Poker[52];
                int n = 0;
                for (int i = 2; i <= 14; i++)
                {
                    string valueCode = i.ToString();
                    switch (i)
                    {
                        case 11:
                            valueCode = "J";
                            break;
                        case 12:
                            valueCode = "Q";
                            break;
                        case 13:
                            valueCode = "K";
                            break;
                        case 14:
                            valueCode = "A";
                            break;
                    }

                    pokers[n++] = new Poker(string.Format("{0}{1}", Poker.Spade, valueCode));
                    pokers[n++] = new Poker(string.Format("{0}{1}", Poker.Heart, valueCode));
                    pokers[n++] = new Poker(string.Format("{0}{1}", Poker.Diamond, valueCode));
                    pokers[n++] = new Poker(string.Format("{0}{1}", Poker.Club, valueCode));
                }

                return pokers;
            }

            /// <summary>
            
    /// Writes a deck of pokers to text file
            
    /// </summary>
            
    /// <param name="pokers"></param>
            
    /// <param name="filename"></param>
            public static void WritePoker(Poker[] pokers,string filename)
            {
                if (pokers == null)
                {
                    return;
                }

                using (StreamWriter sw = File.CreateText(filename))
                {
                    foreach (Poker p in pokers)
                    {
                        if (p != null)
                        {
                            sw.WriteLine(p.Mark);
                        }
                    }
                }
            }

            /// <summary>
            
    /// Reads a deck of pokers from text file
            
    /// </summary>
            
    /// <param name="filename"></param>
            
    /// <returns></returns>
            public static List<Poker> ReadPoker(string filename)
            {
                if (!File.Exists(filename))
                {
                    throw new FileNotFoundException();
                }

                List<Poker> pokerList = new List<Poker>();

                string[] pokers = File.ReadAllLines(filename);
                if (pokers != null)
                {
                    foreach (string poker in pokers)
                    {
                        pokerList.Add(new Poker(poker));
                    }
                }

                return pokerList;
            }

            /// <summary>
            
    /// Sorts pokers by suit first
            
    /// </summary>
            
    /// <param name="pokers"></param>
            public static void SortBySuitFirst(Poker[] pokers)
            {
                if (pokers != null)
                {
                    PokerQuickSort(pokers, 0, pokers.Length - 1true);
                }
            }

            /// <summary>
            
    /// Sorts pokers by value first
            
    /// </summary>
            
    /// <param name="pokers"></param>
            public static void SortByValueFirst(Poker[] pokers)
            {
                if (pokers != null)
                {
                    PokerQuickSort(pokers, 0, pokers.Length - 1false);
                }
            }

            /// <summary>
            
    /// Details to sorting pokers by suitFirst parameter, using Quick Sort algorithms
            
    /// </summary>
            
    /// <param name="pokers"></param>
            
    /// <param name="left"></param>
            
    /// <param name="right"></param>
            
    /// <param name="suitFirst"> if true, suit first; false, value first </param>
            private static void PokerQuickSort(Poker[] pokers, int left, int right, bool suitFirst)
            {
                if (pokers == null || pokers.Length <= 1)
                {
                    return;
                }

                if (right > pokers.Length - 1 || left < 0)
                {
                    throw new ArgumentOutOfRangeException();
                }

                int i = left;
                int j = right;
                Poker key = pokers[left];
                do
                {
                    while ((pokers[i].CompareSuitFirstTo(key) < 0 && suitFirst) || (pokers[i].CompareValueFirstTo(key) < 0 && !suitFirst))
                    {
                        i++;
                    }

                    while ((pokers[j].CompareSuitFirstTo(key) > 0 && suitFirst) || (pokers[j].CompareValueFirstTo(key) > 0 && !suitFirst))
                    {
                        j--;
                    }

                    if (i < j)
                    {
                        if (pokers[i].CompareSuitFirstTo(pokers[j]) == 0)
                        {
                            i++;
                            continue;
                        }

                        Poker temp = pokers[i];
                        pokers[i] = pokers[j];
                        pokers[j] = temp;
                    }

                } while (i < j);

                if (i - 1 > left)
                {
                    PokerQuickSort(pokers, left, i - 1, suitFirst);
                }

                if (i + 1 < right)
                {
                    PokerQuickSort(pokers, i + 1, right, suitFirst);
                }

            }
        }
    }

    All finished expect implementing how to run, that the program does.

    namespace PokerDeck
    {
        class Program
        {
            static void Main(string[] args)
            {
                Poker[] pokers = PokerManager.CreatePoker();
                PokerManager.SortBySuitFirst(pokers);
                PokerManager.WritePoker(pokers, "suitfirst.txt");
                PokerManager.SortByValueFirst(pokers);
                PokerManager.WritePoker(pokers, "valuefirst.txt");
            }
        }
    }

    When run the PokerDeck.exe, you will generate two files(suitfirst.txt and valuefirst.txt) in the same directory of the PokerDeck.exe.

  • 相关阅读:
    HTTP协议【详解】——经典面试题
    原生JS的地区二级联动,很好理解的逻辑
    js操作字符串的常用方法
    移除input框type="number"在部分浏览器的默认上下按钮
    atom
    解决gitHub下载速度慢的问题
    ATOM常用插件推荐
    脚踝扭伤肿了怎么办
    这才是真正的电子科大
    月入 7000,怎么存钱?
  • 原文地址:https://www.cnblogs.com/ericwen/p/PokerDeck.html
Copyright © 2011-2022 走看看