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 = 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);
                }
            }
        }
    }
}
时间: 2024-08-03 21:10:35

C# - Poker Sort的相关文章

斗地主相关算法实现

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 <script> 7 var PokerType = { 8 danzhang: 1, 9 duizi: 2, 10 sanzhang: 3, 11 sandaiyi: 4, 12 sandai

ZOJ 1111 Poker Hands

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1111 A poker hand consists of 5 cards dealt from the deck. Poker hands are ranked by the following partial order from lowest to highest High Card. Hands which do not fit any higher cat

Project Euler :Problem 54 Poker hands

In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way: High Card: Highest value card. One Pair: Two cards of the same value. Two Pairs: Two different pairs. Three of a Kind: Three cards of

ZOJ 1111 Poker Hands --复杂模拟

昨天晚上写的,写了一个多小时,9000+B,居然1A了,爽. 题意:玩扑克,比大小.规则如下: 题意很简单,看过赌神的人都知道,每人手中5张排,比牌面大小,牌面由大到小分别是(这里花色无大小),级别从高到低依次为:1.同花顺:牌面一样,只比较最大的看谁大,一样大则平手2.四条:四个一样的,看这四个一样的中谁大谁赢3.葫芦:三条+一对,看三条中谁的牌面大4.同花:都是同花就按从大到小比较,看谁的更大5.顺子:都是顺子看最大的谁大,一样则平手6.三条:三条看三条中谁的牌面大7.两对: 两对就看谁的对

The Preliminary Contest for ICPC Asia Shenyang 2019 H. Texas hold&#39;em Poker

题目链接:https://nanti.jisuanke.com/t/41408 题目意思很简单,就是个模拟过程. 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdio> 5 #include <map> 6 #define rep(i,j,k) for(int i = (j); i <= (k); ++i) 7 #define

经典排序算法 - 冒泡排序Bubble sort

 原文出自于 http://www.cnblogs.com/kkun/archive/2011/11/23/bubble_sort.html 经典排序算法 - 冒泡排序Bubble sort 原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换, 这样一趟过去后,最大或最小的数字被交换到了最后一位, 然后再从头开始进行两两比较交换,直到倒数第二位时结束,其余类似看例子 例子为从小到大排序, 原始待排序数组| 6 | 2 | 4 | 1 | 5 | 9 | 第一趟排序(外循环) 第

Redis学习之Sort Set详解

本文和大家分享的主要是Redis中Sort Set相关内容,一起来看看吧,希望对大家学习redis有所帮助. 游戏服务器需要做一个排行榜实时更新,如果沿用传统的方法,一般是通过后端的定时任务去跑数据来生成排行榜数据,这种方法一方面无法满足产品对功能实时性的要求,另一方面也一定程度上消耗服务器端有限的资源.如果从每次数据库读取数据并进行排名(使用Mysql的sort关键字进行排序),在关卡数据量的级数大时是一种效率低的方法.在查阅大量资料后,发现了Redis中的有序集合(Sort Set). Re

75. Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

[BZOJ1552][Cerc2007]robotic sort

试题描述 输入 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N个用空格隔开的正整数,表示N个物品最初排列的编号. 输出 输出共一行,N个用空格隔开的正整数P1,P2,P3-Pn,Pi表示第i次操作前第i小的物品所在的位置. 注意:如果第i次操作前,第i小的物品己经在正确的位置Pi上,我们将区间[Pi,Pi]反转(单个物品). 输入示例 6 3 4 5 1 6 2 输出示例 4 6 4 5 6 6 数据规模及约定 见"输入" 题解 暴力