现在的位置: 首页 > 综合 > 正文

C#发牌

2012年06月07日 ⁄ 综合 ⁄ 共 3407字 ⁄ 字号 评论关闭

之前不太喜欢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();
        }
    }
}

抱歉!评论已关闭.