C# - Poker Sort
来源:互联网 发布:淘宝店面怎样装修模板 编辑:程序博客网 时间:2024/05/16 14:24
// --------------------------------------------------------------------------------------------------------------------// <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() { 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); } }}
// --------------------------------------------------------------------------------------------------------------------// <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="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="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, 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; } }}
Pokers.txt:
♦9♣7♣K♠Q♠9♠J♥2♣6♣10♣3♠2♥7♦10♦J♦8♦Q♦7♣Q♥J♣8♥8♥10♦6♥6♦5♠10♠7♠5♥Q♦2♥4♠K♣A♦3♥K♣9♥3♦4♠6♣2♥5♥A♠3♠8♣4♠A♠4♦K♣5♥9♣J♦A
SuitSortedPokers.txt
♠2♠3♠4♠5♠6♠7♠8♠9♠10♠J♠Q♠K♠A♥2♥3♥4♥5♥6♥7♥8♥9♥10♥J♥Q♥K♥A♣2♣3♣4♣5♣6♣7♣8♣9♣10♣J♣Q♣K♣A♦2♦3♦4♦5♦6♦7♦8♦9♦10♦J♦Q♦K♦A
RankSortedPokers.txt:
♠2♥2♣2♦2♠3♥3♣3♦3♠4♥4♣4♦4♠5♥5♣5♦5♠6♥6♣6♦6♠7♥7♣7♦7♠8♥8♣8♦8♠9♥9♣9♦9♠10♥10♣10♦10♠J♥J♣J♦J♠Q♥Q♣Q♦Q♠K♥K♣K♦K♠A♥A♣A♦A
4 0
- C# - Poker Sort
- poker
- C# Sort
- C# 排序 List.Sort
- C# List排序Sort
- C# Array.sort
- C#中的 sort排序
- C# sort 委托使用
- C# List.Sort()用法
- C# Sort排序
- C# list sort 排序
- C# List Sort 排序
- C# Array.sort排序
- Poker游戏?
- poker 打包
- Poker Face
- Poker大小
- Poker program
- JDBC 操作数据库
- R:对象的管理
- 多线程分段读取数据库
- [Android实例] android数据库升级
- android面试题目(二)
- C# - Poker Sort
- HDU 2489 Minimal Ratio Tree (暴力枚举+最小生成树)
- 如何获取一个未在php中定义的类
- 打印程序堆栈起始地址
- 代码大全读书笔记
- R:向量
- ZOJ 3819Average Score (2014牡丹江现场赛A)
- Google Cloud Message for Android(GCM云推送)(译文和开发流程)
- QScrollBar 保持在最下端