C#入门经典第十章例题 - - 卡牌
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#入门经典第十章例题 - - 卡牌的更多相关文章
- C#入门经典第十章接口的实现
- C#入门经典第十章类的成员-1
类成员的访问级别 public 成员可以由任何代码访问,公共的. private 私有的,成员只能有类中的代码访问.(默认的关键字) internal 内部的,成员只能有定义它的程序集(项目)内部 ...
- C链表-C语言入门经典例题
struct student { long num; float score; struct student *next; }; 注意:只是定义了一个struct student类型,并未实际分配存储 ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- 使用UIKit制作卡牌游戏(三)ios游戏篇
译者: Lao Jiang | 原文作者: Matthijs Hollemans写于2012/07/13 转自朋友Tommy 的翻译,自己只翻译了这第三篇教程. 原文地址: http://www.ra ...
- 强烈推荐visual c++ 2012入门经典适合初学者入门
强烈推荐visual c++ 2012入门经典适合初学者入门 此书循序渐进,用其独特.易于理解的教程风格来介绍各个主题,无论是编程新手,还是经验丰富的编程人员,都很容易理解. 此书的目录基本覆盖了Wi ...
- 在WebGL场景中管理多个卡牌对象的实验
这篇文章讨论如何在基于Babylon.js的WebGL场景中,实现多个简单卡牌类对象的显示.选择.分组.排序,同时建立一套实用的3D场景代码框架.由于作者美工能力有限,所以示例场景视觉效果可能欠佳,本 ...
- python实例:解决经典扑克牌游戏 -- 四张牌凑24点 (一)
Hey! Hope you had a great day so far! 今天想和大家讨论的是一道我从这学期cs的期末考试得到灵感的题:Get 24 Poker Game.说到 Get 24 Pok ...
随机推荐
- 判断是否POST提交
if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){} //判断是否POST提交
- 解决Linux 安装python3 .5 解决pip 安装无法成功问题ssl安全拦截无法pip安装库问题
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not avail ...
- echo图片延迟加载js
插件描述:和 Lazy Load 一样,Echo.js 也是一个用于图像延迟加载 JavaScript.不同的是 Lazy Load 是基于 jQuery 的插件,而 Echo.js 不依赖于 jQu ...
- pem文件转p12
p12->pem cer.p12: openssl pkcs12 -clcerts -nokeys -out cer.pem -in cer.p12 key.p12: openssl pkcs1 ...
- vue.js数组追加合并与对象追加合并的
今天在做懒加载的时候遇到的问题,在网上搜索找到的答案不是很清晰,就来写一下,方便以后使用. 直接上图吧 官方连接:https://cn.vuejs.org/v2/guide/reactivity.ht ...
- C++程序设计入门 引用和动态内存管理学习
引用: 引用就是另一个变量的别名,通过引用所做的读写操作实际上是作用于原变量上. 由于引用是绑定在一个对象上的,所以定义引用的时候必须初始化. 函数参数:引用传递 1.引用可做函数参数,但调用时只需 ...
- mac终端 login: login: Could not determine audit condition
手速太快,误操作:sudo chmod -R 777 / 这会导致终端命令用不了了,再次打开终端提示: Last login: Fri Jul 13 10:09:35 on ttys001 login ...
- rpm与yum,at与crontab,sed命令使用
1.简述rpm与yum命令的常见选项,并举例. rpm——软件包管理系统,它使得在Linux下安装.升级.删除软件包的工作变得容易,并且具有查询.验证软件包的功能. 1)安装选项 命令格式: rpm ...
- 使用echart的雷达图的时候,如果文字越界的解决办法记录,标签文字自动换行
使用echart的雷达图的时候,如果文字越界的解决办法记录,标签文字自动换行 前几天项目中有一个图表的是用echart生成的,遇到一个问题,就是在手机端显示的售时候,如果文字太长就会超出div,之前的 ...
- 第五章 C程序结构
一.数值类型 1.实数常量的表示:3.5(双精度),3.5f(单精度),3.5L(长双精度) 2.整数常量:char字符常量(‘a’,‘b’,‘0’)当做一个整型常量参加运算 3.数字字符与英文字母字 ...