洗牌算法

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


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


展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值