C#入门经典第十章例题 - - 卡牌

1.库

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace CardLib
 7 {
 8     public enum Suit
 9     {
10         Club,
11         Diamond,
12         Heart,
13         Spade
14     }
15 }
Suit
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace CardLib
 7 {
 8     public 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     }
25 }
Rank
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace CardLib
 7 {
 8     public class Card
 9     {
10         public readonly Rank rank;
11         public readonly Suit suit;
12 
13 
14         private Card()
15         {
16 
17         }
18         public Card(Suit newSuit, Rank newRank)
19         {
20             suit = newSuit;
21             rank = newRank;
22         }
23 
24         
25 
26         public override string ToString()
27         {
28             return "The " + rank + " of " + suit + "s\n";
29         }
30     }
31 }
Card
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace CardLib
 8 
 9 {
10     public class Deck
11     {
12         private Card[] cards;
13 
14         public Deck()
15         {
16             cards = new Card[52];
17             for(int suitVal=0;suitVal<4;suitVal++)
18             {
19                 for(int rankVal=1;rankVal<14;rankVal++)
20                 {
21                     cards[suitVal * 13 + rankVal - 1] = new Card((Suit)suitVal, (Rank)rankVal);
22                 }
23             }
24         }
25 
26         public Card GetCard(int cardNum)
27         {
28             if (cardNum >= 0 && cardNum <= 51)
29                 return cards[cardNum];
30             else
31                 throw (new System.ArgumentOutOfRangeException("cardNum", cardNum, "Value must be between 0 and 51."));
32         }
33 
34         public void Shuffle()
35         {
36             Card[] newDeck = new Card[52];
37             bool[] assigned = new bool[52];
38             Random sourceGen = new Random();
39             for (int i = 0 ;i < 52;i++)
40             {
41                 int destCard = 0;
42                 bool foundCard = false;
43                 while(foundCard==false)
44                 {
45                     destCard = sourceGen.Next(52);
46                     if (assigned[destCard] == false)
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.输出卡牌

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using CardLib;
 7 
 8 namespace CardClient
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             Deck myDeck = new Deck();
15             myDeck.Shuffle();
16             for(int i=0;i<52;++i)
17             {
18                 Card tempCard = myDeck.GetCard(i);
19                 Console.Write(tempCard.ToString());
20                 //if (i != 51)
21                 //    Console.Write(",");
22                 //else
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 
 6 using System;
 7 using System.Collections.Generic;
 8 using System.Linq;
 9 using System.Text;
10 using System.Threading.Tasks;
11 using System.Collections;       //ArrayList类
12 using CardLib;
13 
14 
15 namespace ConsoleApp1
16 {
17     class Exercise
18     {
19         static void Main(string[] args)
20         {
21             
22             Random randomCard = new Random();      //随机卡号
23             Card[] handCard=new Card[5];           // 五张手牌
24             Deck myDeck = new Deck();
25             bool isFlush = true;
26 
27             List<int> remainCard = new List<int>();     //创建List存放52张卡号
28 
29             for (int i = 0; i < 52; ++i)
30             {
31                remainCard.Add(i);
32             }
33 
34             //输出洗牌后的结果
35             Console.WriteLine("The Result after shuffle : ");
36             myDeck.Shuffle();
37             for(int i=0;i<52;++i)
38             {
39                 Console.WriteLine((i+1) + " : "+ myDeck.GetCard(i).ToString());
40             }
41 
42 
43             //循环抽卡,每次五张,童叟无欺
44             for (int j = 0; j < 10; j++)
45             {
46                 Console.WriteLine("Round : " + (j+1) + " Draw! ");
47 
48                 for (int i = 0; i < 5; ++i)
49                 {
50                     int 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                     //判断花色
56                     if (handCard[i].suit != handCard[0].suit)
57                     {
58                         isFlush = false;
59                     }
60                 }
61 
62                 //判断是否Flush
63                 if(isFlush)
64                 {
65                     Console.WriteLine("Flush !");
66                     break;
67 
68                 }
69                 else
70                 {
71                     Console.WriteLine("No Flush");
72                 }
73             }
74             Console.ReadKey();
75         }
76     }
77 }
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(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();
        }
    }
}
remove & removeAt

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

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

 

拙见如图:

 可以理解为把这个小方格直接biu掉(误|||),全都往前移

转载于:https://www.cnblogs.com/QQW123/p/9087837.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值