1.库

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace CardLib
{
public enum Suit
{
Club,
Diamond,
Heart,
Spade
}
}

Suit

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace CardLib
{
public enum Rank
{ Ace=,
Deuce,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King
}
}

Rank

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace CardLib
{
public class Card
{
public readonly Rank rank;
public readonly Suit suit; private Card()
{ }
public Card(Suit newSuit, Rank newRank)
{
suit = newSuit;
rank = newRank;
} public override string ToString()
{
return "The " + rank + " of " + suit + "s\n";
}
}
}

Card

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CardLib {
public class Deck
{
private Card[] cards; public Deck()
{
cards = new Card[];
for(int suitVal=;suitVal<;suitVal++)
{
for(int rankVal=;rankVal<;rankVal++)
{
cards[suitVal * + rankVal - ] = new Card((Suit)suitVal, (Rank)rankVal);
}
}
} public Card GetCard(int cardNum)
{
if (cardNum >= && cardNum <= )
return cards[cardNum];
else
throw (new System.ArgumentOutOfRangeException("cardNum", cardNum, "Value must be between 0 and 51."));
} public void Shuffle()
{
Card[] newDeck = new Card[];
bool[] assigned = new bool[];
Random sourceGen = new Random();
for (int i = ;i < ;i++)
{
int destCard = ;
bool foundCard = false;
while(foundCard==false)
{
destCard = sourceGen.Next();
if (assigned[destCard] == false)
foundCard = true;
}
assigned[destCard] = true;
newDeck[destCard] = cards[i];
} newDeck.CopyTo(cards, );
} }
}

Deck

2.输出卡牌

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CardLib; namespace CardClient
{
class Program
{
static void Main(string[] args)
{
Deck myDeck = new Deck();
myDeck.Shuffle();
for(int i=;i<;++i)
{
Card tempCard = myDeck.GetCard(i);
Console.Write(tempCard.ToString());
//if (i != 51)
// Console.Write(",");
//else
// Console.WriteLine();
}
Console.ReadKey(); }
}
}

Program

3.练习

为 Ch10CardLib 库编写一个控制台客户程序,从洗牌后的 Deck 对象中一次取出 5 张牌。如果这 5 张牌都是相同花色,客户程序就应在屏幕上显示这 5 张牌,以及文本 "Flush!",否则在取出 50 张牌以后就输出文本 “No flush”,并退出。

 /* 为 CardLib 库编写一个控制台客户程序,从洗牌后的 Deck 对象中一次取出 5 张牌。如果这 5 张牌都是相同花色,
客户程序就应在屏幕上显示这 5 张牌,以及文本 "Flush!",否则在取出 50 张牌以后就输出文本 “No flush”,并退出。*/ //改成了随机抽取五张牌 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; //ArrayList类
using CardLib; namespace ConsoleApp1
{
class Exercise
{
static void Main(string[] args)
{ Random randomCard = new Random(); //随机卡号
Card[] handCard=new Card[]; // 五张手牌
Deck myDeck = new Deck();
bool isFlush = true; List<int> remainCard = new List<int>(); //创建List存放52张卡号 for (int i = ; i < ; ++i)
{
remainCard.Add(i);
} //输出洗牌后的结果
Console.WriteLine("The Result after shuffle : ");
myDeck.Shuffle();
for(int i=;i<;++i)
{
Console.WriteLine((i+) + " : "+ myDeck.GetCard(i).ToString());
} //循环抽卡,每次五张,童叟无欺
for (int j = ; j < ; j++)
{
Console.WriteLine("Round : " + (j+) + " Draw! "); for (int i = ; i < ; ++i)
{
int num = randomCard.Next(, - i - j * );
handCard[i] = myDeck.GetCard(remainCard[num]);
remainCard.RemoveAt(num); //RemoveAt()方法是按照index移除
Console.WriteLine("card " + (i+) + " " + handCard[i]); //判断花色
if (handCard[i].suit != handCard[].suit)
{
isFlush = false;
}
} //判断是否Flush
if(isFlush)
{
Console.WriteLine("Flush !");
break; }
else
{
Console.WriteLine("No Flush");
}
}
Console.ReadKey();
}
}
}

Exercise

在这里大致说下Remove()和RemoveAt()的区别

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; namespace ConsoleApp28
{
class Program
{
static void Main(string[] args)
{ //Remove()是按照内容移除
List<int> listRemove = new List<int>(); //添加两个数据5和15
listRemove.Add();
listRemove.Add();
listRemove.Remove(); //没实际效果,因为listRemove中没有0
Console.WriteLine("listRemove[0] = " + listRemove[]);
Console.WriteLine("listRemove[1] = " + listRemove[]);
listRemove.Remove();
Console.WriteLine("listRemove.Remove(5)后,listRemove[0] = " + listRemove[]); //RemoveAt()是按照index移除
List<int> listRemoveAt = new List<int>(); //添加两个数据5和15
listRemoveAt.Add();
listRemoveAt.Add();
//listRemoveAt.RemoveAt(2); // 报错,因为超出范围
Console.WriteLine("listRemoveAt[0] = " + listRemoveAt[]);
Console.WriteLine("listRemoveAt[1] = " + listRemoveAt[]);
listRemoveAt.RemoveAt();
Console.WriteLine("listRemove.RemoveAt(0)后,listRemoveAt[0] = " + listRemoveAt[]); Console.ReadKey();
}
}
}

remove & removeAt

此外这篇博文里写的很详细,在使用后要注意长度和内容会改变

https://blog.csdn.net/weixin_39800144/article/details/77915981

拙见如图:

可以理解为把这个小方格直接biu掉(误|||),全都往前移C#入门经典第十章例题 - - 卡牌-LMLPHP

05-11 04:40