C#发牌

之前不太喜欢C#,无意中发现linq,用来实现排序是如此简单,所以又用C#写了遍发牌程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
       
        static void Main(string[] args)
        {  

            //IEnumerable<string> expr = from s in cards
            //                           where s.Length == 5
            //                           orderby s
            //                           select s.ToUpper();

            //foreach (string item in expr)
            //Console.WriteLine(item);
            //Console.ReadKey();
            Program o = new Program();
            List<string> cards = o.newCards();
            o.exchangeCards(30, cards);
            List<List<string>> all = o.dispatchCards(6, cards);
            o.display(all);
        }


        /**
         * a random list of cards
         */
        void exchangeCards(int exchangeTimes, List<string> cards)
        {
            Random random = new Random();
            for (int i = 0; i < exchangeTimes; i++)
            {
                int r1 = random.Next(cards.Count);
                int r2 = random.Next(cards.Count);
                string tmp = cards[r1];
                cards[r1] = cards[r2];
                cards[r2]= tmp;
            }
        }

        /**
         * dispatch cards
         */
        List<List<string>> dispatchCards(int peopleCount, List<string> cards)
        {

            int pageCount = cards.Count / peopleCount;
            int residue = cards.Count % peopleCount;
            List<List<string>> contents = new List<List<string>>();

            for (int j = 0; j < peopleCount; j++)
            {
                List<string> eachContent = new List<string>();

                for (int k = 0; k < pageCount; k++) 
                {
                    eachContent.Add(cards[j * pageCount + k]);
                }
                contents.Add(eachContent);
            }

            int i;
            for (i = 0; i < residue; i++)
            {
                contents[i].Add(cards[pageCount * peopleCount + i]);

                contents[i] = sortCards(contents[i]);
            }

            for (; i < peopleCount; i++)
            {
                contents[i] = sortCards(contents[i]);
            }
            return contents;
        }

     /**
         * a sorted list of cards
         */
        private List<string> newCards()
        {
            List<string> cards = new List<string>();
         // four types
         String[] card1 = { "A", "B", "C", "D" };
         // 13 pages for every type
         String[] card2 = {"03", "04", "05", "06", "07", "08", "09", "10","11", "12", "13", "14", "15"};
         // input fixed value to the list
         for (int i = 0; i <= 3; i++) {
          for (int j = 0; j <= 12; j++) {
           cards.Add(card1[i] + ":" + card2[j]);
          }
         }
            return cards;
        }


       
     /**
         * sort cards
         */
        private List<string> sortCards(List<string> content)
        {
            List<string> ret = new List<string>();

            IEnumerable<string> sorted = from s in content
                                       orderby s[0],s[2],s[3]
                                       select s;

            foreach (string tmp in sorted)
            {
                ret.Add(tmp);
            }
            return ret;
        }


        /**
         * show every person's cards
         */
        private void display(List<List<string>> all)
        {
            for (int i = 0; i < all.Count; i++)
            {

                Console.WriteLine("the person" + (i + 1) + "'s cards are:");
                foreach (string tmp in all[i])
                {
                    Console.Write(tmp + ",");
                }
                Console.WriteLine();
            }

            Console.ReadKey();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值