#include <iostream>
#include <ctime>
#include <cstdlib>
#include <unordered_map>
using namespace std;

class card{
  public:
    char face;
    string symbol;
    
    void printCard(){
      cout << face << " of " << symbol << endl;
    }
    //void shuffleDeck();
    //void printDeck();
};

class deck{
  public:
    void printDeck();
    void shuffleDeck();
};

enum GameState{
  HIT = 0,
  STAND = 1,
  DONE = 3,
};

card cards[52];
card temp;

void deck :: shuffleDeck(){
  int n, m;
  cout << endl << "Shuffling Deck..." << endl;
  for(n=0; n< 52; n++){
    m = rand()%52;
    temp = cards[n];
    cards[n] = cards[m];
    cards[m] = temp;
  }
  cout << "Deck Shuffled." << endl;
}

void deck :: printDeck(){
  int k;
  cout << endl << endl << "New Deck:" << endl;
  for(k=0; k<52; k++){
    cards[k].printCard();
  }
}

int main() 
{
    unordered_map<char, int> m = {{'A',1}, {'2',2}, {'3',3}, {'4',4}, {'5',5}, {'6',6}, {'7',7}, {'8',8}, {'9',9}, {'T',10}, {'J',10}, {'Q',10}, {'K',10}};

    cout << "NEW Hello, World!\n\n";
    char faces[] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'};
    string symbol[] = {"Hearts", "Diamonds", "Spades", "Clubs"};
    
    int i, j, l;
    l=0;
    
    for(i=0; i<4; i++){
      for(j=0; j<13; j++){
        cards[l].face = faces[j];
        cards[l].symbol = symbol[i];
        l++;
      }
    }
    
    deck currDeck;
    //currDeck.printDeck();
    
    currDeck.shuffleDeck();
    //currDeck.printDeck();
    
    int dealerCount = 0;
    int playerCount = 0;
    int initBet = 10;
    int payOut = 0;
    
    srand(time(0)); 
    int idx = rand()%52;
    cout << endl << "Random starting index: " << idx << endl;
    
    playerCount += m[cards[idx].face];
    idx = (idx+1)%52;
    dealerCount += m[cards[idx].face];
    idx = (idx+1)%52;
    playerCount += m[cards[idx].face];
    idx = (idx+1)%52;
    dealerCount += m[cards[idx].face];
    idx = (idx+1)%52;
    
    cout << endl << "PRE-CHECKS" << endl;
    cout << "Player Count:" << playerCount << "; Dealer Count:" << dealerCount << endl;
      
    if(dealerCount == playerCount && playerCount  == 21){
      payOut = initBet;
      cout << "Player won in a glorious tie with the dealer." << endl;
      cout << "Player Count:" << playerCount << "; Dealer Count:" << dealerCount << endl;
      return 0;
    }
    if(dealerCount  == 21){
      payOut = 0;
      cout << "Dealer won" << endl;
      cout << "Player Count:" << playerCount << "; Dealer Count:" << dealerCount << endl;
      return 0;
    }
    
    if(playerCount == 21){
      payOut = initBet*1.5;
      cout << "Player won" << endl;
      cout << "Player Count:" << playerCount << "; Dealer Count:" << dealerCount << endl;
      return 0;        
    }
    
    //GameState currState = userInput();
    GameState currState = HIT;
    
    while(currState!=DONE){
      cout << endl << "IN-LOOP" << endl;
      switch (currState){
        case HIT:
          playerCount += m[cards[idx].face];
          idx = (idx+1)%52;
          if(playerCount > 21){
            cout << "Dealer won" << endl;
            cout << "Player Count:" << playerCount << "; Dealer Count:" << dealerCount << endl;
            return 0;
          }
          //currState = userInput();
          currState = STAND;
          break;
        case STAND:
          while(dealerCount <17){
            dealerCount += m[cards[idx].face];
            idx = (idx+1)%52;
            if(dealerCount > 21){
              cout << "Player won" << endl;
              cout << "Player Count:" << playerCount << "; Dealer Count:" << dealerCount << endl;
              return 0;
            }
          }
          currState = DONE;
          break;
        default:
          break;
        
      }
      cout << "Player Count:" << playerCount << "; Dealer Count:" << dealerCount << endl;
    }
    
    cout << endl << "POST-CHECKS" << endl;
    cout << "Player Count:" << playerCount << "; Dealer Count:" << dealerCount << endl;
      
    if(dealerCount == playerCount && playerCount  == 21){
      payOut = initBet;
      cout << "Player won in a glorious tie with the dealer." << endl;
      cout << "Player Count:" << playerCount << "; Dealer Count:" << dealerCount << endl;
      return 0;
    }
    if(dealerCount  == 21){
      payOut = 0;
      cout << "Dealer won" << endl;
      cout << "Player Count:" << playerCount << "; Dealer Count:" << dealerCount << endl;
      return 0;
    }
    
    if(playerCount == 21){
      payOut = initBet*1.5;
      cout << "Player won" << endl;
      cout << "Player Count:" << playerCount << "; Dealer Count:" << dealerCount << endl;
      return 0;        
    }
      

    /*deck[0].printCard();
    deck[14].printCard();
    deck[28].printCard();
    deck[51].printCard();*/
    
    return 0;
}
 
by

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}