洗牌演算法

life4711發表於2016-05-08

題目要求: 用計算機模擬洗牌(不包括大小王),分發給四個玩家並將四個玩家的牌顯示出來。


思路:i從0~51遍歷,每次產生i到52之間的隨機數k(i<k<52),交換第i 和第k張牌。

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j;
            Random Rnd = new Random();
            int k;
            int[] Card = new int[52];
            int[,] Player = new int[4, 13];
            for (i = 0; i < 4; i++)
            {
                for (j = 0; j < 13; j++)
                {
                    Card[i * 13 + j] = (i + 1) * 100 + j + 1;
                }
            }
            Console.Write("Please input times:");
            string s = Console.ReadLine();
            int times = Convert.ToInt32(s);
            for (j = 1; j <= times; j++)
            {
                for (i = 0; i < 52; i++)
                {
                    k = Rnd.Next(51 - i + 1) + i;
                    int temp = Card[i];
                    Card[i] = Card[k];
                    Card[k] = temp;
                }
            }
            k = 0;
            for (j = 0; j < 13; j++)
            {
                for (i = 0; i < 4; i++)
                {
                    Player[i, j] = Card[k++];
                }
            }
            for (i = 0; i < 4; i++)
            {
                Console.WriteLine("Player{0}:", i + 1);
                for (j = 0; j < 13; j++)
                {
                    k = (int)Player[i, j] / 100;
                    switch (k)
                    {
                        case 1:
                            s = Convert.ToString("紅桃");
                            break;
                        case 2:
                            s = Convert.ToString("方片");
                            break;
                        case 3:
                            s = Convert.ToString("梅花");
                            break;
                        case 4:
                            s = Convert.ToString("黑桃");
                            break;
                    }
                    k = Player[i, j] % 100;
                    switch (k)
                    {
                        case 1:
                            s = s + "A";
                            break;
                        case 11:
                            s = s + "J";
                            break;
                        case 12:
                            s = s + "Q";
                            break;
                        case 13:
                            s = s + "K";
                            break;
                        default:
                            s = s + Convert.ToString(k);
                            break;
                    }
                    Console.Write(s);
                    if (j < 12)
                        Console.Write(", ");
                    else
                        Console.WriteLine(" ");
                }
            }
            Console.Read();
        }
    }
}

注:


101,102,103...113 分別表示紅桃A,紅桃2,...紅桃K

201,202,203...213分別表示方塊A,方塊2,...方塊K

301,302,303...313 分別表示梅花A,梅花2,...梅花K

401,402,403...413分別表示黑桃A,黑桃2,...黑桃K


相關文章