提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
一副扑克有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#面向对象的思想,能够给读者比较更好的理解和认识面向对象和面向过程的不同以及它的优越性,希望大家能够从中获得一些感受和认识,欢迎大家的访谈!!!