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 ...
随机推荐
- sofar:机器学习检测十题
http://sofasofa.io/forum_main_post.php?postid=1001084 http://sofasofa.io/forum_main_post.php?postid= ...
- 前端基础-CSS是什么?
阅读目录 一. 什么是CSS 二. 为何要用CSS 三. 如何使用CSS 一. 什么是CSS CSS全称Cascading Style Sheet层叠样式表,是专用用来为HTML标签添加样式的. 样式 ...
- 接入Gobelieve IM开发平台的DEMO代码
接入Gobelieve IM开发平台的DEMO代码, 请求头部: Authorization: Basic $base64(appid:$hex_md5(appsecret))意思是 appsecre ...
- The number of sections contained in the collection view after the update (1) must be equal to the number of sections contained in the collection view before the update (0), plus or minus the number of
现象:当删除CollectionView 当中的某个section的时候,报上面的错误 初步分析:当前CollectionView删除前后都不止一个Section,怎么会报那样的错误:猜想可能是相册界 ...
- oracle10g学习笔记
1.简介 1.1.sql:Structured Query Language 结构化查询语言 1.2.windows在目录路径中使用反斜线\,unix和linux使用正斜线/ 1.3.Number(a ...
- java通过IO流复制文件
package kimoji; import java.io.*; public class FileTest { public static void main(String[] args) thr ...
- Mysql-多表连接的操作和用法
一 .介绍 二 .多表连接查询 三 .符合条件连接查询 四 .子查询 一.介绍 本节主题 多表连接查询 复合条件连接查询 子查询 准备表 #建表 create table dep( id int, n ...
- Jquery实现表单动态加减、ajax表单提交
一直在搞Java后台开发,记得刚工作那一两年时间搞过前后端交互开发,用的东西也是五花八门,当时觉得做页面展示给别人看,是很有成就的事情,不过现在感觉自己跟纯前端开发比起来弱爆了,不过如果你的目标是作为 ...
- Python学习笔记十:json序列化,软件结构目录规范,ATM作业讲解,import本质论
json序列化 将系统的某个状态保存为字符串(挂起),序列化. import json json.dumps():序列化 json.loads():反序列化 简单类型数据处理 import pickl ...
- Python学习笔记九:装饰器,生成器,迭代器
装饰器 本质是函数,装饰其他函数,为其他函数添加附加功能 原则: 1不修改原函数的源代码 2不修改原函数的调用方式 知识储备: 1函数即变量 使用门牌号的例子说明函数,调用方式与变量一致 2高阶函数 ...