Black Jack 21点游戏 C++

╰半夏微凉° 2022-09-07 12:19 317阅读 0赞

Black Jack is a poker game


· Process:

1. Define the card or shuffle the card
2. Player get 2 cards
3. Player get points
4. Player determines whether to continue to ask for cards
5. If player continue ask for cards, player get 1 card, the player points accumulated
6. If player don’t ask for cards, count the player points
7. Dealer get 2 cards
8. Dealer determines whether to continue to ask for cards
9. If dealer continue ask for cards, dealer get 1 card, the dealer points accumulated
10. If dealer don’t ask for cards, count the dealer points
11. Compare the player points with the dealer points
12. The round is end

· Module: class

Poker Card
Player
Dealer
Compare the Points

代码块:

  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ctime>
  5. using namespace std;
  6. #define MAXVALUE 200
  7. //Build the playcard class
  8. class PlayCard
  9. {
  10. public:
  11. //Define the poker and poker points, or reshuffle.
  12. void initSet();
  13. //Output the whole playcard and points
  14. void showCard();
  15. string randomPlay();
  16. //Get the playcard points of the current role
  17. void getValue(string &getPoints, int &userPoints);
  18. protected:
  19. string colors[4];
  20. string numbers[13];
  21. string arr[52];
  22. string currentCard;
  23. int cardPoints[52];
  24. int currentPoints;
  25. int cardCount;
  26. };
  27. //Build the player class
  28. class Player: public PlayCard
  29. {
  30. public:
  31. //return the points of the player
  32. int pointsCount(int sign);
  33. void getResult();
  34. int playerPoints;
  35. int sumCount;
  36. };
  37. //Build the dealer class
  38. class Dealer: public PlayCard
  39. {
  40. public:
  41. //return the points of the dealer
  42. int pointsCount(int sign);
  43. void getResult();
  44. int dealerPoints;
  45. int sumCount;
  46. };
  47. //Build the compare class, used for compare the player points with the dealer points
  48. class Compare: public Player, public Dealer{
  49. public:
  50. void compare(int &playerpoints, int &dealerpoints);
  51. };
  52. int main()
  53. {
  54. PlayCard playCard;
  55. playCard.initSet();
  56. Player player;
  57. Dealer dealer;
  58. Compare result;
  59. for(int i = 0; i < MAXVALUE; i++){
  60. player.getResult();
  61. dealer.getResult();
  62. result.compare(player.sumCount, dealer.sumCount);
  63. int next;
  64. cout<<"\nNext Round? 1.Yes 2.No : ";
  65. cin>>next;
  66. if(next==1) {
  67. system("cls");
  68. continue;
  69. }
  70. else break;
  71. }
  72. system("pause");
  73. return 0;
  74. }
  75. void PlayCard::initSet(){
  76. string col[4] = {
  77. "\003", "\004", "\005", "\006"};
  78. string num[13] = {
  79. "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
  80. cardCount = 52;
  81. //Assign decors and points to the array
  82. for(int i = 0; i < 4; i++){
  83. colors[i]=col[i];
  84. }
  85. for(int i = 0; i < 13; i++){
  86. numbers[i]=num[i];
  87. }
  88. //Mix decors and points into an array
  89. int index = 0;
  90. string tempCombin;
  91. for(int i = 0; i < 4; i++){
  92. for(int j = 0; j < 13; j++){
  93. if(j < 8){
  94. tempCombin = colors[i].append(numbers[j]);
  95. arr[index++] = tempCombin;
  96. colors[i].erase(colors[i].end()-1);
  97. } else if(j == 8){
  98. tempCombin = colors[i].append(numbers[j]);
  99. arr[index++] = tempCombin;
  100. colors[i].erase(colors[i].end()-2);
  101. colors[i].erase(colors[i].end()-1);
  102. } else {
  103. tempCombin = colors[i].append(numbers[j]);
  104. arr[index++] = tempCombin;
  105. colors[i].erase(colors[i].end()-1);
  106. }
  107. }
  108. }
  109. //Convert playcards to integer points
  110. for(int i = 0, number = 2; i < cardCount; i++){
  111. if(i == 0 || i == 13 || i == 26 || i == 39){
  112. number = 2;
  113. }
  114. if(i == 12 || i == 25 || i == 38 || i == 51){
  115. cardPoints[i] = 11;
  116. continue;
  117. }
  118. if(number > 10){
  119. number = 10;
  120. }
  121. cardPoints[i] = number++;
  122. }
  123. }
  124. void PlayCard::showCard(){
  125. for(int i = 0; i < 52; i++){
  126. if(i%13 == 0){
  127. cout<<endl;
  128. }
  129. cout<<arr[i]<<" ";
  130. }
  131. cout<<endl;
  132. for(int i = 0; i < 52; i++){
  133. if(i%13 == 0){
  134. cout<<endl;
  135. }
  136. cout<<cardPoints[i]<<" ";
  137. }
  138. cout<<endl;
  139. }
  140. string PlayCard::randomPlay(){
  141. //Give a random seed as an array subscript for the whole deck of cards
  142. unsigned seed;
  143. seed = time(0);
  144. srand(seed);
  145. int index = rand()%51;
  146. //Obtain the number of points of the current card and set it empty.
  147. //If the current card is empty, re issue the random seed.
  148. //If the number of empty cards is less than the half number of the whole
  149. //deck (i.e. 26), shuffle the cards again.
  150. for(int i = 0; i < MAXVALUE; i++){
  151. if(cardCount >= 26 && arr[index] != " "){
  152. currentCard = arr[index];
  153. currentPoints = cardPoints[index];
  154. arr[index] = " ";
  155. cardCount--;
  156. break;
  157. } else if(cardCount >= 26 && arr[index] == " "){
  158. index = rand()%51;
  159. } else {
  160. initSet();
  161. }
  162. }
  163. return currentCard;
  164. }
  165. void PlayCard::getValue(string &getPoints, int &userPoints){
  166. getPoints = randomPlay();
  167. cout<<getPoints<<" ";
  168. if(currentPoints == 11 && (userPoints+currentPoints)>21){
  169. currentPoints = 1;
  170. userPoints += currentPoints;
  171. } else if(currentPoints == 11 && (userPoints+currentPoints)<=21){
  172. userPoints += currentPoints;
  173. } else {
  174. userPoints += currentPoints;
  175. }
  176. }
  177. int Player::pointsCount(int sign){
  178. string getPoints;
  179. playerPoints = 0;
  180. cout<<"Player Card: ";
  181. //sign=0, first random play
  182. //sign=1, next random play
  183. if(sign == 0){
  184. for(int i = 0; i < 2; i++){
  185. getValue(getPoints, playerPoints);
  186. }
  187. } else if(sign == 1){
  188. getValue(getPoints, playerPoints);
  189. }
  190. cout<<endl;
  191. return playerPoints;
  192. }
  193. void Player::getResult(){
  194. int choice;
  195. sumCount = 0;
  196. sumCount += pointsCount(0);
  197. cout<<"Player Points: "<<sumCount<<endl;
  198. for(int i = 0; i < MAXVALUE; i++){
  199. if(sumCount < 21){
  200. cout<<"Do you want to continue? 1.Yes 2.No : ";
  201. cin>>choice;
  202. if(choice == 1){
  203. sumCount += pointsCount(1);
  204. cout<<"Player Points: "<<sumCount<<endl;
  205. } else if(choice == 2){
  206. cout<<"Player Points: "<<sumCount<<endl;
  207. break;
  208. } else {
  209. cout<<"Error! Retry!\n";
  210. continue;
  211. }
  212. } else if(sumCount == 21){
  213. break;
  214. } else {
  215. break;
  216. }
  217. }
  218. cout<<endl;
  219. }
  220. int Dealer::pointsCount(int sign){
  221. string getPoints;
  222. dealerPoints = 0;
  223. cout<<"Dealer Card: ";
  224. //sign=0, first random play
  225. //sign=1, next random play
  226. if(sign == 0){
  227. for(int i = 0; i < 2; i++){
  228. getValue(getPoints, dealerPoints);
  229. }
  230. } else if(sign == 1){
  231. getValue(getPoints, dealerPoints);
  232. }
  233. cout<<endl;
  234. return dealerPoints;
  235. }
  236. void Dealer::getResult(){
  237. int choice;
  238. sumCount = 0;
  239. sumCount += pointsCount(0);
  240. cout<<"Dealer Points: "<<sumCount<<endl;
  241. for(int i = 0; i < MAXVALUE; i++){
  242. if(sumCount < 21){
  243. cout<<"Do you want to continue? 1.Yes 2.No : ";
  244. cin>>choice;
  245. if(choice == 1){
  246. sumCount += pointsCount(1);
  247. cout<<"Dealer Points: "<<sumCount<<endl;
  248. } else if(choice == 2){
  249. cout<<"Dealer Points: "<<sumCount<<endl;
  250. break;
  251. } else {
  252. cout<<"Error! Retry!\n";
  253. continue;
  254. }
  255. } else if(sumCount == 21){
  256. break;
  257. } else {
  258. break;
  259. }
  260. }
  261. cout<<endl;
  262. }
  263. void Compare::compare(int &playerpoints, int &dealerpoints){
  264. if((playerpoints<=21)&&(dealerpoints<=21)&&(playerpoints>dealerpoints)){
  265. cout<<"Player Win!!!"<<endl;
  266. }
  267. if((playerpoints<=21)&&(dealerpoints<=21)&&(dealerpoints>playerpoints)){
  268. cout<<"Dealer Win!!!"<<endl;
  269. }
  270. if((playerpoints<=21)&&(dealerpoints<=21)&&(playerpoints==dealerpoints)){
  271. cout<<"Draw!"<<endl;
  272. }else if(playerpoints>21){
  273. cout<<"Dealer Win!!!"<<endl;
  274. }else if(dealerpoints>21){
  275. cout<<"Player Win!!!"<<endl;
  276. }
  277. }

发表评论

表情:
评论列表 (有 0 条评论,317人围观)

还没有评论,来说两句吧...

相关阅读

    相关 python小游戏——21

    编写一副扑克牌和一个发牌函数,要求: (1) 创建一副扑克牌,不包含两个Joker,其它牌面每个四张,花色可以用任意特殊符号表示; (2) 按照21点的游戏规则,使用学

    相关 c++简单24游戏

    我的博客已经全部迁移到新地址,欢迎收藏我的[个人博客][Link 1]。 随机生成4个代表扑克牌牌面的数字字母,程序自动列出所有可能算出24的表达式,用擅长的语言(C/C++