C#⼊门经典第⼗章例题--卡牌1.库
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace CardLib
7 {
8public enum Suit
9 {
10 Club,
11 Diamond,
roster
12 Heart,
13 Spade
14 }
15 }
Suit
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace CardLib
7 {
8public enum Rank
9 {
10
11 Ace=1,
12 Deuce,
13 Three,
14 Four,
15 Five,
16 Six,
17 Seven,
18 Eight,
19 Nine,
20 Ten,
21 Jack,
22 Queen,
23 King
24 }ashlynn
25 }
Rank
1using System;
什么是抑郁症2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace CardLib
7 {
8public class Card
9 {
10public readonly Rank rank;
11public readonly Suit suit;
12
13
14private Card()
15 {
16
17 }
18public Card(Suit newSuit, Rank newRank)
19 {
20 suit = newSuit;
21 rank = newRank;
22 }
23
24
25
26public override string ToString()
lenovo是什么意思27 {
28return"The " + rank + " of " + suit + "s\n";
29 }
30 }
31 }
Card
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6
7namespace CardLib
8
9 {
10public class Deck
11 {
12private Card[] cards;
13
14public Deck()
15 {
16 cards = new Card[52];
17for(int suitVal=0;suitVal<4;suitVal++)
18 {
19for(int rankVal=1;rankVal<14;rankVal++)
20 {
21 cards[suitVal * 13 + rankVal - 1] = new Card((Suit)suitVal, (Rank)rankVal);
22 }
23 }
24 }
25
26public Card GetCard(int cardNum)
27 {
28if (cardNum >= 0 && cardNum <= 51)
29return cards[cardNum];
30el
31throw (new System.ArgumentOutOfRangeException("cardNum", cardNum, "Value must be between 0 and 51."));
奥普拉脱口秀
32 }
33
34public void Shuffle()
35 {
36 Card[] newDeck = new Card[52];
37bool[] assigned = new bool[52];
38 Random sourceGen = new Random();
39for (int i = 0 ;i < 52;i++)
40 {
41int destCard = 0;
42bool foundCard = fal;
43while(foundCard==fal)
44 {
45 destCard = sourceGen.Next(52);
46if (assigned[destCard] == fal)
47 foundCard = true;
48 }
49 assigned[destCard] = true;
50 newDeck[destCard] = cards[i];
51 }
52
53 newDeck.CopyTo(cards, 0);
54 }
55
56
57 }
58 }
Deck
2.输出卡牌
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using CardLib;
7
8namespace CardClient
9 {
10class Program
11 {
12static void Main(string[] args)
13 {
14 Deck myDeck = new Deck();
15 myDeck.Shuffle();
16for(int i=0;i<52;++i)
afver17 {
18 Card tempCard = myDeck.GetCard(i);
19 Console.Write(tempCard.ToString());
20//if (i != 51)
21// Console.Write(",");
22//el
23// Console.WriteLine();
24 }
25 Console.ReadKey();
26
27
28
29 }
30 }
31 }
Program
3.练习
为Ch10CardLib库编写⼀个控制台客户程序,从洗牌后的Deck对象中⼀次取出 5 张牌。如果这 5 张牌都是相同花⾊,客户程序就应在屏幕上显⽰这 5 张牌,以及⽂本 "Flush!",否则在取出 50 张牌以后就输出⽂本 “No flush”,并退出。
1/* 为 CardLib 库编写⼀个控制台客户程序,从洗牌后的 Deck 对象中⼀次取出 5 张牌。如果这 5 张牌都是相同花⾊,
2客户程序就应在屏幕上显⽰这 5 张牌,以及⽂本 "Flush!",否则在取出 50 张牌以后就输出⽂本 “No flush”,并退出。*/
3
4//改成了随机抽取五张牌
5
6using System;
7using System.Collections.Generic;
8using System.Linq;
9using System.Text;
10using System.Threading.Tasks;
11using System.Collections; //ArrayList类
12using CardLib;
13
14
15namespace ConsoleApp1
16 {
17class Exerci
18 {
19static void Main(string[] args)
20 {
21
22 Random randomCard = new Random(); //随机卡号
23 Card[] handCard=new Card[5]; // 五张⼿牌
24 Deck myDeck = new Deck();
25bool isFlush = true;
26
27 List<int> remainCard = new List<int>(); //创建List存放52张卡号
28
29for (int i = 0; i < 52; ++i)
30 {
31 remainCard.Add(i);
32 }
33
34//输出洗牌后的结果
35 Console.WriteLine("The Result after shuffle : ");
36 myDeck.Shuffle();
37for(int i=0;i<52;++i)
ywq38 {
39 Console.WriteLine((i+1) + " : "+ myDeck.GetCard(i).ToString());
40 }
41
42
43//循环抽卡,每次五张,童叟⽆欺
44for (int j = 0; j < 10; j++)
45 {
46 Console.WriteLine("Round : " + (j+1) + " Draw! ");
47
48for (int i = 0; i < 5; ++i)
49 {
50int num = randomCard.Next(0, 51 - i - j * 5);
51 handCard[i] = myDeck.GetCard(remainCard[num]);
52 remainCard.RemoveAt(num); //RemoveAt()⽅法是按照index移除
53 Console.WriteLine("card " + (i+1) + "" + handCard[i]);
54
55//判断花⾊
56if (handCard[i].suit != handCard[0].suit)
57 {
58 isFlush = fal;
59 }
60 }
61
62//判断是否Flush
63if(isFlush)
64 {
特索罗65 Console.WriteLine("Flush !");
66break;
67
68 }
69el
70 {
71 Console.WriteLine("No Flush");
72 }
73 }
74 Console.ReadKey();
75 }
76 }
77 }
Exerci
在这⾥⼤致说下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(5);
listRemove.Add(15);
listRemove.Remove(0); //没实际效果,因为listRemove中没有0
Console.WriteLine("listRemove[0] = " + listRemove[0]);
Console.WriteLine("listRemove[1] = " + listRemove[1]);
listRemove.Remove(5);
Console.WriteLine("listRemove.Remove(5)后,listRemove[0] = " + listRemove[0]);
//RemoveAt()是按照index移除
List<int> listRemoveAt = new List<int>();
//添加两个数据5和15
listRemoveAt.Add(2);
listRemoveAt.Add(12);
//listRemoveAt.RemoveAt(2); // 报错,因为超出范围
Console.WriteLine("listRemoveAt[0] = " + listRemoveAt[0]);
Console.WriteLine("listRemoveAt[1] = " + listRemoveAt[1]);
listRemoveAt.RemoveAt(0);
Console.WriteLine("listRemove.RemoveAt(0)后,listRemoveAt[0] = " + listRemoveAt[0]); Console.ReadKey();
certain
}
}
}
remove & removeAt
此外这篇博⽂⾥写的很详细,在使⽤后要注意长度和内容会改变
拙见如图:
可以理解为把这个⼩⽅格直接biu掉(误|||),全都往前移