首页 > 代码库 > C# - Poker Sort

C# - Poker Sort

// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="Chimomo's Company">
//   Respect the work.
// </copyright>
// <summary>
//   Defines the Program type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Poker
{
    using System.Collections.Generic;

    /// <summary>
    /// The program.
    /// </summary>
    public static class Program
    {
        /// <summary>
        /// The main.
        /// </summary>
        public static void Main()
        {
            Utility.GenerateRandomPokers();
            string pokerFile = "Pokers.txt";
            SuitSortTest(pokerFile);
            RankSortTest(pokerFile);
        }

        /// <summary>
        /// The suit sort test.
        /// </summary>
        /// <param name="pokerFile">
        /// The poker file.
        /// </param>
        private static void SuitSortTest(string pokerFile)
        {
            string sortedPokerFile = "SuitSortedPokers.txt";
            List<string> pokers = Utility.ReadPokersFromFile(pokerFile);
            SuitSort suitSort = new SuitSort(pokers);
            suitSort.Sort();
            Utility.WritePokersToFile(sortedPokerFile, suitSort.PokerList);
        }

        /// <summary>
        /// The rank sort test.
        /// </summary>
        /// <param name="pokerFile">
        /// The poker file.
        /// </param>
        private static void RankSortTest(string pokerFile)
        {
            string sortedPokerFile = "RankSortedPokers.txt";
            List<string> pokers = Utility.ReadPokersFromFile(pokerFile);
            RankSort rankSort = new RankSort(pokers);
            rankSort.Sort();
            Utility.WritePokersToFile(sortedPokerFile, rankSort.PokerList);
        }
    }
}
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RankSort.cs" company="Chimomo's Company">
//   Respect the work.
// </copyright>
// <summary>
//   Defines the RankSort type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Poker
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The rank sort.
    /// </summary>
    public class RankSort
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RankSort"/> class.
        /// </summary>
        /// <param name="pokerList">
        /// The poker list.
        /// </param>
        public RankSort(List<string> pokerList)
        {
            this.PokerList = pokerList;
        }

        /// <summary>
        /// Gets the poker list.
        /// </summary>
        public List<string> PokerList { get; private set; }

        /// <summary>
        /// The sort.
        /// </summary>
        public void Sort()
        {
            this.PokerList.Sort(RankComparer);
        }

        /// <summary>
        /// The rank comparer.
        /// </summary>
        /// <param name="pokerA">
        /// The poker a.
        /// </param>
        /// <param name="pokerB">
        /// The poker b.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        private static int RankComparer(string pokerA, string pokerB)
        {
            int a = GetPokerValue(pokerA);
            int b = GetPokerValue(pokerB);
            return a - b;
        }

        /// <summary>
        /// The get poker value.
        /// </summary>
        /// <param name="poker">
        /// The poker.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        private static int GetPokerValue(string poker)
        {
            if (string.IsNullOrEmpty(poker))
            {
                return 0;
            }

            int pokerValue;
            string rank = poker.Substring(1);

            // 给rank赋予相应的权值。
            switch (rank)
            {
                case "A":
                    pokerValue = http://www.mamicode.com/14;>
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SuitSort.cs" company="Chimomo's Company">
//   Respect the work.
// </copyright>
// <summary>
//   Defines the SuitSort type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Poker
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The suit sort.
    /// </summary>
    public class SuitSort
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SuitSort"/> class.
        /// </summary>
        /// <param name="pokerList">
        /// The poker list.
        /// </param>
        public SuitSort(List<string> pokerList)
        {
            this.PokerList = pokerList;
        }

        /// <summary>
        /// Gets the poker list.
        /// </summary>
        public List<string> PokerList { get; private set; }

        /// <summary>
        /// The sort.
        /// </summary>
        public void Sort()
        {
            this.PokerList.Sort(SuitComparer);
        }

        /// <summary>
        /// The suit comparer.
        /// </summary>
        /// <param name="pokerA">
        /// The poker a.
        /// </param>
        /// <param name="pokerB">
        /// The poker b.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        private static int SuitComparer(string pokerA, string pokerB)
        {
            int a = GetPokerValue(pokerA);
            int b = GetPokerValue(pokerB);
            return a - b;
        }

        /// <summary>
        /// The get poker value.
        /// </summary>
        /// <param name="poker">
        /// The poker.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        private static int GetPokerValue(string poker)
        {
            if (string.IsNullOrEmpty(poker))
            {
                return 0;
            }

            int suitValue = http://www.mamicode.com/0;>
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Utility.cs" company="Chimomo's Company">
//   Respect the work.
// </copyright>
// <summary>
//   Defines the Utility type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Poker
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// The utility.
    /// </summary>
    public static class Utility
    {
        /// <summary>
        /// The read pokers from file.
        /// </summary>
        /// <param name="pokerFile">
        /// The poker file.
        /// </param>
        /// <returns>
        /// The poker list.
        /// </returns>
        public static List<string> ReadPokersFromFile(string pokerFile)
        {
            List<string> pokerList = new List<string>();
            using (StreamReader streamReader = new StreamReader(pokerFile, Encoding.UTF8))
            {
                string poker;
                while ((poker = streamReader.ReadLine()) != null)
                {
                    pokerList.Add(poker);
                }
            }

            return pokerList;
        }

        /// <summary>
        /// The write pokers to file.
        /// </summary>
        /// <param name="pokerFile">
        /// The poker file.
        /// </param>
        /// <param name="pokerList">
        /// The poker list.
        /// </param>
        public static void WritePokersToFile(string pokerFile, List<string> pokerList)
        {
            using (StreamWriter streamWriter = new StreamWriter(pokerFile, true, Encoding.UTF8))
            {
                foreach (var poker in pokerList)
                {
                    streamWriter.WriteLine(poker);
                }
            }
        }

        /// <summary>
        /// The generate random pokers.
        /// </summary>
        public static void GenerateRandomPokers()
        {
            string[] suits = { "?", "?", "?", "?" };
            string[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
            List<string> pokerList = new List<string>();
            foreach (string suit in suits)
            {
                pokerList.AddRange(ranks.Select(rank => string.Format("{0}{1}", suit, rank)));
            }

            using (StreamWriter streamWriter = new StreamWriter("Pokers.txt", false, Encoding.UTF8))
            {
                Random random = new Random();
                while (pokerList.Count > 0)
                {
                    int i = random.Next(0, pokerList.Count - 1);
                    streamWriter.WriteLine(pokerList[i]);
                    pokerList.RemoveAt(i);
                }
            }
        }
    }
}

C# - Poker Sort