C# - Poker Sort

版权声明:欢迎转载并请注明出处,谢谢~~ https://blog.csdn.net/chimomo/article/details/40431243
/**
 * Created by Chimomo
 */

namespace PokerSort
{
    using System.Collections.Generic;

    /// <summary>
    /// The program.
    /// </summary>
    public static class Program
    {
        /// <summary>
        /// The main.
        /// </summary>
        public static void Main()
        {
            string pokerFile = Utility.GenerateRandomPokers("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);
        }
    }
}
/**
 * Created by Chimomo
 */

using System;
using System.Collections.Generic;

namespace PokerSort
{
    /// <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)
        {
            PokerList = pokerList;
        }

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

        /// <summary>
        /// Sort.
        /// </summary>
        public void Sort()
        {
            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 = 14;
                    break;
                case "K":
                    pokerValue = 13;
                    break;
                case "Q":
                    pokerValue = 12;
                    break;
                case "J":
                    pokerValue = 11;
                    break;
                default:
                    pokerValue = Convert.ToInt32(rank);
                    break;
            }

            pokerValue *= 100;

            // 给suit赋予相应的权值。花色的排列顺序为:黑、红、梅、方。
            char suit = poker[0];
            switch (suit)
            {
                case '♠':
                    pokerValue += 1;
                    break;
                case '♥':
                    pokerValue += 2;
                    break;
                case '♣':
                    pokerValue += 3;
                    break;
                case '♦':
                    pokerValue += 4;
                    break;
            }

            return pokerValue;
        }
    }
}
/**
 * Created by Chimomo
 */

using System;
using System.Collections.Generic;

namespace PokerSort
{
    /// <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)
        {
            PokerList = pokerList;
        }

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

        /// <summary>
        /// Sort.
        /// </summary>
        public void Sort()
        {
            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 = 0;
            char suit = poker[0];

            // Suit排序首先考虑的是花色,先把花色赋予相应的权值以区分开来。花色的排列顺序为:黑、红、梅、方。
            switch (suit)
            {
                case '♠':
                    suitValue = 100;
                    break;
                case '♥':
                    suitValue = 200;
                    break;
                case '♣':
                    suitValue = 300;
                    break;
                case '♦':
                    suitValue = 400;
                    break;
            }

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

            // 给rank赋予相应的权值。
            switch (rank)
            {
                case "A":
                    rankValue = 14;
                    break;
                case "K":
                    rankValue = 13;
                    break;
                case "Q":
                    rankValue = 12;
                    break;
                case "J":
                    rankValue = 11;
                    break;
                default:
                    rankValue = Convert.ToInt32(rank);
                    break;
            }

            return suitValue + rankValue;
        }
    }
}
/**
 * Created by Chimomo
 */

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

    /// <summary>
    /// The utility.
    /// </summary>
    public static class Utility
    {
        /// <summary>
        /// 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>
        /// 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, false, Encoding.UTF8))
            {
                foreach (var poker in pokerList)
                {
                    streamWriter.WriteLine(poker);
                }
            }
        }

        /// <summary>
        /// Generate random pokers to pokerFile.
        /// </summary>
        /// <param name="pokerFile">
        /// The pokerFile.
        /// </param>
        /// <returns>
        /// The pokerFile.
        /// </returns>
        public static string GenerateRandomPokers(string pokerFile)
        {
            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(pokerFile, 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);
                }
            }

            return pokerFile;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/chimomo/article/details/40431243