Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h> /* printf, NULL */
- #include <stdlib.h> /* srand, rand */
- #include <time.h> /* time */
- #include <iostream>
- #include <string>
- using namespace std;
- class player {
- public:
- int hand, turn;
- int pid;
- bool hit;
- string name;
- string input;
- };
- int draw() {
- int card = ((rand() % 51) + 1);
- return card;
- }
- void concat(int i, int p) {
- string suit;
- string name = "default";
- if (p == 1){ cout << "p1:" << endl; }
- else { cout << "p2:" << endl; };
- if (i > 0 && i < 14){
- suit = "spades";
- }
- else if (i > 13 && i < 27) {
- suit = "clubs";
- }
- else if (i > 26 && i < 40){
- suit = "diamonds";
- }
- else{
- suit = "hearts";
- };
- if (i % 13 > 9){
- if (i % 13 == 0) {
- name = "ace";
- }
- else if (i % 13 == 12){
- name = "king";
- }
- else if (i % 13 == 11) {
- name = "queen";
- }
- else if (i % 13 == 10) {
- name = "jack";
- };
- cout << ("a %d", name) << " of " << suit << " is drawn" << endl;
- }
- else if (i % 13 == 0){
- name = "ace";
- cout << ("a %d", name) << " of " << suit << " is drawn" << endl;
- }
- else
- {
- int name = (i % 13) + 1;
- cout << ("a %d", name) << " of " << suit << " is drawn" << endl;
- //cout << "modulus is " << i % 13 << endl;
- }
- return;
- }
- string inputTurn(player p) {
- // input
- string pname = p.name;
- cout << pname << " hit or stay?" << endl;
- cin >> p.input;
- if (p.input == "stay") { p.hit = 0; }
- else if (p.input == "hit"){ p.hit = 1; }
- else {
- cout << "Please input 'Hit' or 'Stay', try again." << endl;
- inputTurn(p);
- };
- return p;
- }
- int main() {
- while (true){
- player a;
- player b;
- a.pid = 1;
- b.pid = 2;
- a.name = "default";
- b.name = "default";
- string suit;
- string name;
- if (a.name == "default") {
- cout << a.pid << " please enter a name" << endl;
- cin >> a.name;
- };
- if (b.name == "default") {
- cout << b.pid << " please enter a name" << endl;
- cin >> b.name;
- };
- a.hand = 0;
- b.hand = 0;
- srand(time(NULL));
- for (int i = 1; i < 53; i = i + 1){
- if (a.hand < 21)
- {
- inputTurn(a);
- if (a.hit == 1){
- i = draw();
- concat(i, 1);
- i = (i % 13) + 1;
- if (i > 10){ i = 10; };
- a.hand += i;
- //cout << ("%d", name) << " of " << suit << endl;
- cout << "p1 current score = " << a.hand << endl;
- }
- }
- if (b.hand < 21)
- {
- inputTurn(b);
- system("pause");
- if (b.hit == 1){
- i = draw();
- concat(i, 2);
- i = (i % 13) + 1;
- if (i > 10){ i = 10; };
- b.hand += i;
- //cout << ("%d", name) << " of " << suit << endl;
- cout << "p2 current score = " << b.hand << endl;
- };
- }
- if (b.hand > 21 || a.hand > 21){ cout << "BUST" << endl; a.hand = 0; b.hand = 0; };
- if (b.hand == 21 || a.hand == 21){ cout << "WIN" << endl; a.hand = 0; b.hand = 0; };
- system("pause");
- };
- }
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment