[CareerCup] 8.1 Implement Blackjack 实现21点纸牌
8.1 Design the data structures for a generic deck of cards. Explain how you would subclass the data structures to implement blackjack.
这道题让我们设计一个21点纸牌游戏的数据结构,用面向对象的思想来设计。那么既然21点是一种特定的纸牌游戏,它可以是从普通纸牌的基础上派生出来的。所以我们先实现最基本的纸牌类Card,里面包括值和花色,还有一些基本的判断或标记可用性的函数。然后就是基本的牌堆类Deck,可以用来加入牌,洗牌,发牌以及算剩余牌数。还需要一个当前手牌类Hand,可以计算当前分数,可以加牌等。然后就是它们的派生类21点纸牌类BlackJackCard,包括计算值,计算最大最小值,判断是不是Ace等等,然后就是21点手牌类BlackjackHand,计算当前得分,判断是否爆了,是否是21点等等。以下代码为书上代码,有些函数体写实现,所以暂时无法用具体运行。
// C++ defination
enum Suit {Club, Diamond, Heart, Spade}; template<class T>
class Deck {
public:
void setDeckOfCards(vector<T> deckOfCards) {}; // ...
void shuffle() {}; // ...
int remainingCards() {
return _cards.size() - _dealtIndex;
}
vector<T> dealHead(int number) {}; // ...
T dealCard() {}; // ...
private:
vector<T> _cards;
int _dealtIndex = ;
}; class Card {
public:
Card(int c, Suit s): _faceValue(c), _suit(s) {};
virtual int value() = ;
Suit suit() { return _suit; };
bool isAvailable() { return _available; };
void markUnavailable() { _available = false; };
void markAvailable() { _available = true; };
protected:
int _faceValue;
Suit _suit;
private:
bool _available = true;
}; template<class T>
class Hand {
public:
int score() {
int score = ;
for (T card : cards) {
score += card.value();
}
return score;
}
void addCard(T card) {
cards.add(card);
}
protected:
vector<T> cards;
}; class BlackJackCard: public Card {
public:
BlackJackCard(int c, Suit s): Card(c,s) {};
int value() {
if (isAce()) return ;
else if (_faceValue >= && _faceValue <= ) return ;
else return _faceValue;
}
int minValue() {
if (isAce()) return ;
else return value();
}
int maxValue() {
if (isAce()) return ;
else return value();
}
bool isAce() {
return _faceValue == ;
}
bool isFaceCard() {
return _faceValue >= && _faceValue <= ;
}
}; class BlackjackHand: public Hand<BlackJackCard> {
public:
int score() {
vector<int> scores = possibleScores();
int maxUnder = INT_MIN, minOver = INT_MAX;
for (auto a : scores) {
if (a > && a < minOver) {
minOver = a;
} else if (a <= && a > maxUnder) {
maxUnder = a;
}
}
return maxUnder == INT_MIN ? minOver : maxUnder;
}
bool busted() { return score() > ; };
bool is21() { return score() == ; };
bool isBlackJack() {}; // ...
private:
vector<int> possibleScores() {}; // ...
};
[CareerCup] 8.1 Implement Blackjack 实现21点纸牌的更多相关文章
- [CareerCup] 3.1 Implement Three Stacks using Array 使用数组来实现三个栈
3.1 Describe how you could use a single array to implement three stacks. 这道题让我们用一个数组来实现三个栈,书上给了两种方法, ...
- [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列
3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...
- [CareerCup] 7.4 Implement Multiply Subtract and Divide 实现乘法减法和除法
7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only ...
- [CareerCup] 8.10 Implement a Hash Table 实现一个哈希表
8.10 Design and implement a hash table which uses chaining (linked lists) to handle collisions. 这道题让 ...
- WinForm-简单21点纸牌小游戏
纸牌游戏有很多种玩法,C#代码写的纸牌游戏,网上也能找到不少,从中也能学习到不少知识,自己动手也写一个,算是记录下学习过程吧. 纸牌21点的玩法也比较简单,就是看谁手中的所有牌相加是21点,或是离21 ...
- C++和Java中枚举enum的用法
在C++和java中都有枚举enum这个关键字,但是它们之间又不太一样.对于C++来说,枚举是一系列命名了的整型常量,而且从枚举值转化为对应的整型值是在内部进行的.而对于Java来说,枚举更像一个类的 ...
- python面对对象编程----1:BlackJack(21点)
昨天读完了<Mastering Object-oriented Python>的第一部分,做一些总结. 首先,第一部分总过八章,名字叫Pythonic Classes via Specia ...
- CareerCup All in One 题目汇总 (未完待续...)
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
- BlackJack Strategy
GAME SPEC: 2-deck, 104 cards total. Bellagio has 2-deck and 6-deck games. based on hard 17, dealer h ...
随机推荐
- scanf 用法及陷阱(转)
函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]); scanf()函数是通用终端格式化输入函数,它从标准输入设备( ...
- python arguments *args and **args ** is for dictionaries, * is for lists or tuples.
below is a good answer for this question , so I copy on here for some people need it By the way, the ...
- Swift 2.0初探
转眼间,Swift已经一岁多了,这门新鲜.语法时尚.类型安全.执行速度更快的语言已经渐渐的深入广大开发者的心. 今年6月,一年一度的WWDC大会如期而至,在大会上Apple发布了Swift 2.0,引 ...
- JSON转换类(一)--过滤特殊字符,格式化字符型、日期型、布尔型
/// <summary> /// 过滤特殊字符 /// </summary> private static string String2Json(String s) { St ...
- Spring学习笔记之 Spring IOC容器(二) 之注入参数值,自动组件扫描方式,控制Bean实例化方式,使用注解方式
本节主要内容: 1. 给MessageBean注入参数值 2. 测试Spring自动组件扫描方式 3. 如何控制ExampleBean实例化方式 4. 使用注解方式重构Jdb ...
- hdu 4607 Park Visit 求树的直径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...
- Go
一.简介 https://zh.wikipedia.org/wiki/Go 二.安装 1)低版本 http://blog.sina.com.cn/s/blog_59cc90640102xm8r.htm ...
- DimDate populate data
日期维度 任何一个数据仓库都应该有一个日期维度. 因为很少有不需要通过日期维度看数据的情况存在. 日期维度的好处是,你可以通过他连接各个事实表,然后在报表端传送报表参数的时候, 直接自动过滤日期维度的 ...
- [转]IOS上路_01-Win7+VMWare9+MacOSX10.8+XCode4.6.3
本文转自:http://my.oschina.net/vigiles/blog/141689 目录[-] 1. 资源准备: 1)实体机: 2)VMWare9: 3)VM for MacOS 补丁: 4 ...
- 【ASP.NET 基础】表单和控件
1.HTML表单的提交方式 对于一个普通HTML表单来说,它有两个重要的属性:action 和 method.action属性指明当前表单提交之后由哪个程序来处理,这个处理程序可以是任何动态网页或者 ...