c#洗牌(面向对象)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录



前言

一副扑克有52张牌,那么使用数组存储这52张牌,然后打乱这些牌。


提示:以下是本篇文章正文内容,下面案例可供参考


一、c#面向对象是什么?

c#是一个高度面向对象的编程语言,适合读者认真研读


二、使用步骤


1.引入库

代码如下(示例):

using System;
using System.Collections;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] cardColor = { "\x0003  ", "◇", "☆", "●" };
            Card[] cardList = new Card[52];
            int colorIndex = 0;
            int tempValue = 0;
            for (int i = 0; i < cardList.Length; i++)
            {
                if (i % 13 == 0 && i != 0)
                {
                    colorIndex++;
                }
                Char a = 'A';
                tempValue = i % 13;
                switch (tempValue)
                {
                    case 0:
                        a = 'A';
                        break;
                    case 9:
                        a = '0';
                        break;
                    case 10:
                        a = 'J';
                        break;
                    case 11:
                        a = 'Q';
                        break;
                    case 12:
                        a = 'K';
                        break;
                    default:
                        a = (Char)(tempValue + (int)'1');
                        break;
                }

                cardList[i] = new Card()
                {
                    value = a,
                    color = cardColor[colorIndex]
                };
            }

            Console.WriteLine("洗牌之前:");
            ShowCard(cardList);

            ShuffleCard(cardList);

            Console.WriteLine("\n\n洗牌之后:(其中-----------◎:红桃   ◇:黑桃   ☆:方块    ●:梅花)");
            ShowCard(cardList);

            Console.ReadLine();
        }

        static void ShuffleCard(Card[] cardList)
        {
            Random random = new Random();
            int tempIndex = 0;
            Card temp = null;
            for (int i = 0; i < 52; i++)
            {
                tempIndex = random.Next(52);
                temp = cardList[tempIndex];
                cardList[tempIndex] = cardList[i];
                cardList[i] = temp;
            }
        }

        static void ShowCard(Card[] cardList)
        {
            Console.Write("玩家1的牌:");
            for (int i = 0; i < cardList.Length; i++)
            {
                if (i % 13 == 0 && i != 0)
                {
                    Console.WriteLine("\n");
                    Console.Write("玩家{0}的牌:", (i / 13) + 1);
                }

                Console.Write(cardList[i].color + "" + cardList[i].value + " ");
            }
        }
    }

public class Card
    {
        public char value;
        public string color;
    }

}

 

该处使用的url网络请求的数据。



总结

此篇比较低级的扑克牌洗牌的算法,运用了c#面向对象的思想,能够给读者比较更好的理解和认识面向对象和面向过程的不同以及它的优越性,希望大家能够从中获得一些感受和认识,欢迎大家的访谈!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值