Guest User

Untitled

a guest
Mar 25th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1.  
  2.  
  3. #include "stdafx.h"
  4.  
  5. #include <stdio.h> /* printf, NULL */
  6. #include <stdlib.h> /* srand, rand */
  7. #include <time.h> /* time */
  8. #include <iostream>
  9. #include <string>
  10. using namespace std;
  11.  
  12. class player {
  13. public:
  14. int hand, turn;
  15. int pid;
  16. bool hit;
  17. string name;
  18. string input;
  19. };
  20.  
  21. int draw() {
  22. int card = ((rand() % 51) + 1);
  23. return card;
  24. }
  25.  
  26. void concat(int i, int p) {
  27. string suit;
  28. string name = "default";
  29. if (p == 1){ cout << "p1:" << endl; }
  30. else { cout << "p2:" << endl; };
  31. if (i > 0 && i < 14){
  32. suit = "spades";
  33. }
  34. else if (i > 13 && i < 27) {
  35. suit = "clubs";
  36. }
  37. else if (i > 26 && i < 40){
  38. suit = "diamonds";
  39. }
  40. else{
  41. suit = "hearts";
  42. };
  43.  
  44. if (i % 13 > 9){
  45.  
  46. if (i % 13 == 0) {
  47.  
  48. name = "ace";
  49. }
  50. else if (i % 13 == 12){
  51.  
  52. name = "king";
  53. }
  54. else if (i % 13 == 11) {
  55.  
  56. name = "queen";
  57.  
  58. }
  59. else if (i % 13 == 10) {
  60.  
  61. name = "jack";
  62. };
  63.  
  64. cout << ("a %d", name) << " of " << suit << " is drawn" << endl;
  65.  
  66.  
  67. }
  68. else if (i % 13 == 0){
  69. name = "ace";
  70. cout << ("a %d", name) << " of " << suit << " is drawn" << endl;
  71. }
  72. else
  73. {
  74. int name = (i % 13) + 1;
  75. cout << ("a %d", name) << " of " << suit << " is drawn" << endl;
  76. //cout << "modulus is " << i % 13 << endl;
  77.  
  78. }
  79. return;
  80. }
  81.  
  82. string inputTurn(player p) {
  83.  
  84.  
  85.  
  86. // input
  87.  
  88.  
  89. string pname = p.name;
  90.  
  91.  
  92.  
  93. cout << pname << " hit or stay?" << endl;
  94.  
  95. cin >> p.input;
  96.  
  97. if (p.input == "stay") { p.hit = 0; }
  98. else if (p.input == "hit"){ p.hit = 1; }
  99. else {
  100. cout << "Please input 'Hit' or 'Stay', try again." << endl;
  101. inputTurn(p);
  102. };
  103.  
  104. return p;
  105.  
  106. }
  107.  
  108. int main() {
  109. while (true){
  110.  
  111. player a;
  112. player b;
  113.  
  114. a.pid = 1;
  115. b.pid = 2;
  116. a.name = "default";
  117. b.name = "default";
  118.  
  119. string suit;
  120. string name;
  121.  
  122. if (a.name == "default") {
  123. cout << a.pid << " please enter a name" << endl;
  124. cin >> a.name;
  125. };
  126.  
  127. if (b.name == "default") {
  128. cout << b.pid << " please enter a name" << endl;
  129. cin >> b.name;
  130. };
  131.  
  132. a.hand = 0;
  133. b.hand = 0;
  134.  
  135. srand(time(NULL));
  136.  
  137.  
  138.  
  139. for (int i = 1; i < 53; i = i + 1){
  140.  
  141.  
  142.  
  143.  
  144. if (a.hand < 21)
  145. {
  146. inputTurn(a);
  147.  
  148.  
  149. if (a.hit == 1){
  150. i = draw();
  151. concat(i, 1);
  152. i = (i % 13) + 1;
  153.  
  154. if (i > 10){ i = 10; };
  155. a.hand += i;
  156. //cout << ("%d", name) << " of " << suit << endl;
  157. cout << "p1 current score = " << a.hand << endl;
  158. }
  159. }
  160.  
  161. if (b.hand < 21)
  162. {
  163. inputTurn(b);
  164. system("pause");
  165. if (b.hit == 1){
  166. i = draw();
  167. concat(i, 2);
  168. i = (i % 13) + 1;
  169. if (i > 10){ i = 10; };
  170. b.hand += i;
  171. //cout << ("%d", name) << " of " << suit << endl;
  172. cout << "p2 current score = " << b.hand << endl;
  173. };
  174. }
  175.  
  176. if (b.hand > 21 || a.hand > 21){ cout << "BUST" << endl; a.hand = 0; b.hand = 0; };
  177.  
  178. if (b.hand == 21 || a.hand == 21){ cout << "WIN" << endl; a.hand = 0; b.hand = 0; };
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186. system("pause");
  187. };
  188.  
  189. }
  190. return 0;
  191.  
  192. };
Advertisement
Add Comment
Please, Sign In to add comment