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#入门经典第十章例题 - - 卡牌的更多相关文章

  1. C#入门经典第十章接口的实现

  2. C#入门经典第十章类的成员-1

    类成员的访问级别 public   成员可以由任何代码访问,公共的. private 私有的,成员只能有类中的代码访问.(默认的关键字) internal 内部的,成员只能有定义它的程序集(项目)内部 ...

  3. C链表-C语言入门经典例题

    struct student { long num; float score; struct student *next; }; 注意:只是定义了一个struct student类型,并未实际分配存储 ...

  4. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  5. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  6. 使用UIKit制作卡牌游戏(三)ios游戏篇

    译者: Lao Jiang | 原文作者: Matthijs Hollemans写于2012/07/13 转自朋友Tommy 的翻译,自己只翻译了这第三篇教程. 原文地址: http://www.ra ...

  7. 强烈推荐visual c++ 2012入门经典适合初学者入门

    强烈推荐visual c++ 2012入门经典适合初学者入门 此书循序渐进,用其独特.易于理解的教程风格来介绍各个主题,无论是编程新手,还是经验丰富的编程人员,都很容易理解. 此书的目录基本覆盖了Wi ...

  8. 在WebGL场景中管理多个卡牌对象的实验

    这篇文章讨论如何在基于Babylon.js的WebGL场景中,实现多个简单卡牌类对象的显示.选择.分组.排序,同时建立一套实用的3D场景代码框架.由于作者美工能力有限,所以示例场景视觉效果可能欠佳,本 ...

  9. python实例:解决经典扑克牌游戏 -- 四张牌凑24点 (一)

    Hey! Hope you had a great day so far! 今天想和大家讨论的是一道我从这学期cs的期末考试得到灵感的题:Get 24 Poker Game.说到 Get 24 Pok ...

随机推荐

  1. PAT——1023. 组个最小数

    给定数字0-9各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意0不能做首位).例如:给定两个0,两个1,三个5,一个8,我们得到的最小的数就是1001555 ...

  2. JNI由浅入深_5_基本类型应用

    1.基本类型应用 对于JNI处理基本类型还是比较简单的,下面是Java代码: <span style="font-size:14px;"> public native ...

  3. Angular动态表单生成(一)

    好久不写博客了,手都生了,趁着最近老大让我研究动态表单生成的时机,撸一发博客~~ 开源项目比较 老大丢给我了两个比较不错的开源的动态表单生成工具,这两个项目在github上的star数量基本持平: h ...

  4. oracle的sys和system的默认密码

    oracle的sys和system默认密码system默认:manager sys默认:change_on_install使用PL/SQL Plus登录数据库时,system用户使用密码manager ...

  5. javacript window对象

    Window -- 代表浏览器中一个打开的窗口: 对象属性 window //窗口自身 window.self //引用本窗户window=window.self window.name //为窗口命 ...

  6. MySQL:如何导入导出数据表和如何清空有外建关联的数据表

    1.导入导出 导入数据库:前提:数据库和数据表要存在(已经被创建) (1)将数据表 test_user.sql 导入到test 数据库的test_user 表中 [root@test ~]# mysq ...

  7. async之诗句慢慢出现

    <body> <ul class="oul"></ul> <script> let str = ["君不见,黄河之水天上来 ...

  8. MongoDB数据库 : 基础

    三元素:数据库 集合 文档(json的扩展bson) 服务启动重启停止: sudo service mongodb start(stop,restart) 修改配置文件 /etc/mongodb.co ...

  9. Delphi开发Android的几个注意

    Delphi在Android开发中还不是很完善,也有一些修改,需要注意: 1.不要用IXMLDocument的SaveToStream, XML.Text等,用了OXML,QXML,VerySimpl ...

  10. hive错误排查一:hive中执行 drop table命令卡住,删除表不成功

    起因 公司用的AWS EMR上的hive,突然不能删除表了. 经过 分析来看,估计是元数据那块出了问题.从元数据入手,元数据存在mysql的hive数据库中 直接使用hive配置文件hive-site ...