C#發牌

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.