C# - Poker Sort

來源:互聯網
上載者:User

標籤: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 = 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;        }    }}
// --------------------------------------------------------------------------------------------------------------------// <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 = 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;        }    }}
// --------------------------------------------------------------------------------------------------------------------// <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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.