码迷,mamicode.com
首页 > 编程语言 > 详细

洗牌算法

时间:2016-05-13 01:26:19      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

题目要求: 用计算机模拟洗牌(不包括大小王),分发给四个玩家并将四个玩家的牌显示出来。


思路: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


洗牌算法

标签:

原文地址:http://blog.csdn.net/lvshubao1314/article/details/51345972

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!