标签: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); } } } } }
标签:poker sort 扑克排序
原文地址:http://blog.csdn.net/troubleshooter/article/details/40431243