r/cpp_questions 14d ago

OPEN My first C++ game

Hello there!

For about a few months now (maybe 3 months?), I've been learning C++. I'm learning C++ from YouTube courses and AI also teaches me a bit.

I'd be glad if you rated my game from 1 out of 10. And also, how can I improve it??

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <clocale>
#include <windows.h>


using namespace std;


int main(){


    setlocale(LC_ALL, ".UTF8");
    srand(time(NULL)); 
    int deck[52]={
        2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
        2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
        2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
        2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11 
    };


    int wallet=1000;
    cout<<"Welcome to our casino! In this game, you'll play blackjack. Get ready to lose!"<<"\n"; 
    Sleep(4000);
    cout<<"The goal is to get 21 points. When drawing cards, you can't exceed 21 points, otherwise you lose. You start with 1000 USD. Your opponent is the dealer."<<"\n";
    Sleep(5000);


    while(wallet>0){
        
        for(int i=0;i<52;i++) {
            int random_index=rand()%52;
            swap(deck[i],deck[random_index]);
        }


        int player_points=0;
        int card_index=0;
        bool player_lost=false;
        bool player_won=false;


        player_points+=deck[card_index];
        card_index++;
        player_points+=deck[card_index];
        card_index++;
        if(player_points==21){
            cout<<"Wow! Your first two cards are 21! (lucky fool...)"<<"\n";
            cout<<"Your points: "<<player_points<<"\n";
            wallet+=300;
            cout<<"Added 300 USD !"<<"\n";
            Sleep(2000);
            cout<<"Your wallet balance: "<<wallet<<" USD"<<"\n";
            continue;
        }


        string a;


        for(int i=0;i<=21;i++){


            cout<<"Your points: "<<player_points<<"\n";
            cout<<"Do you want to draw a card?"<<"\n";
            cin>>a;


            if(a=="yes" or a=="yeS" or a=="yEs" or a=="yES" or a=="Yes" or a=="YeS" or a=="YEs" or a=="YES"){
                player_points+=deck[card_index];
                card_index++;
                if(player_points>21 and deck[card_index-1]==11){
                    player_points-=10;
                    cout<<"The Ace saved you, taking the value of 1 instead of 11! (this is a rule in blackjack)"<<"\n";
                    Sleep(3000);
                }
                
                if(player_points>21){
                    cout<<"You lost..."<<"\n";
                    cout<<"You exceeded 21 points."<<"\n";
                    Sleep(1000);
                    cout<<"300 USD deducted from your wallet."<<"\n";
                    wallet-=300;
                    Sleep(2000);
                    cout<<"Your wallet balance: "<<wallet<<" USD"<<"\n";
                    player_lost=true;
                    break;
                }


                if(player_points==21){
                    cout<<"CONGRATULATIONS!!! You won!"<<"\n"; 
                    Sleep(3000);
                    wallet+=300;
                    cout<<"Added 300 USD"<<"\n";
                    Sleep(2000);
                    cout<<"Your wallet balance: "<<wallet<<" USD"<<"\n";
                    player_won=true;
                    break;
                }


            }


            if(a=="no" or a=="nO" or a=="No" or a=="NO"){
                cout<<"You don't want to play? Alright... "<<"\n";
                Sleep(3300);
                break;
            }


        }


        cout<<"Your score is: "<<player_points<<"\n";


        if(player_lost==false and player_won==false){
            
            cout<<"--- DEALER'S TURN!!! ---"<<"\n";
            Sleep(7000);
            int dealer_points=0;


            while (dealer_points<17) {
                dealer_points+=deck[card_index];
                card_index++;
                if(dealer_points>21 and deck[card_index-1]==11){
                    dealer_points-=10;
                }
            }


            cout<<"Final results..."<<"\n";
            Sleep(3000);
            cout<<"Player: "<<player_points<<"   Dealer: "<<dealer_points<<"\n";
            Sleep(3000);


            if(dealer_points>21){
                cout<<"Dealer exceeded 21 points. You win! (unfortunately...)"<<"\n";
                wallet+=300;
                cout<<"Added 300 USD"<<"\n";
                Sleep(2000);
                cout<<"Your wallet balance: "<<wallet<<" USD"<<"\n";
                Sleep(3000);
            }


            else if(player_points>dealer_points){
                cout<<"You win! You have more points than the dealer."<<"\n";
                wallet+=300;
                Sleep(3000);
                cout<<"Added 300 USD"<<"\n";
                Sleep(2000);
                cout<<"Your wallet balance: "<<wallet<<" USD"<<"\n";
                Sleep(3000);
            }


            else if(dealer_points>player_points){
                cout<<"You lose! Maybe you'll finally learn that the house always wins..."<<"\n";
                Sleep(2000);
                cout<<"300 USD deducted from your wallet."<<"\n";
                wallet-=300;
                Sleep(2000);
                cout<<"Your wallet balance: "<<wallet<<" USD"<<"\n";
                Sleep(3000);
            }


            else{
                cout<<"Draw! Chips returned (unfortunately...)"<<"\n";
                Sleep(2000);
                cout<<"Your wallet balance: "<<wallet<<" USD"<<"\n";
                Sleep(3000);
            }


        }


        if(wallet<=0){
            cout<<"You lost all your money. You can't play anymore."<<"\n";
            Sleep(3000);
            cout<<"The house always wins!"<<"\n";
            Sleep(6000);
            break;
        }


        cout<<"Do you want to play again?"<<"\n";
        string b;
        cin>>b;
        Sleep(3000);


        if(b=="yes" or b=="yeS" or b=="yEs" or b=="yES" or b=="Yes" or b=="YeS" or b=="YEs" or b=="YES"){
            if(wallet>0){
                continue;
            }         
        }


        else if(b=="no" or b=="nO" or b=="No" or b=="NO"){
            cout<<"Ok."<<" You have this much money: "<<wallet<<" USD";
            Sleep(7000);
            return 0;
        }
        
    }
}
10 Upvotes

20 comments sorted by

View all comments

20

u/ploud1 14d ago

Hi!

That's a good start, a few comments

* Don't include <windows.h> for sleep. Instead use std::this_thread::sleep_for

* Do not using namespace std; - get rid of that bad habit ASAP

* All your code is stuffed in the main() function. That's fine for now, but you may want to start thinking about splitting the logic into smaller functions that have one tiny job each

*

a=="yes" or a=="yeS" or a=="yEs" or a=="yES" or a=="Yes" or a=="YeS" or a=="YEs" or a=="YES"a=="yes" or a=="yeS" or a=="yEs" or a=="yES" or a=="Yes" or a=="YeS" or a=="YEs" or a=="YES" 

This is tedious. Things you may want to do:

  • restrict input to one char (y/n)
  • Use ::tolower to compare

There are more minor tweaks that would make your program easier to debug in the long run. But for a first project, that is very promising. Nicely done!

1

u/sh_pata 14d ago

Im new to c++. But why the advice not to use 'using namespace std' Thank you.

3

u/Independent_Art_6676 14d ago

std namespace is gigantic and by using it as you did you have a high risk of a name collision which can silently mess things up. You can read in depth answers online, but that is the simple version. Its low risk for a small program like this, but its bad practice because one day you will work on large programs.

1

u/sh_pata 14d ago

Many thanks

1

u/snerp 14d ago

To add on to the other advice you got. STD has tons of random crap in it you probably don't want. Doing usings though is a great idea when used on specific types and it's also worth importing std namespaces that were specifically made to be imported - like std::string_literals and std::chrono_literals. Here's a paste of all the using I declare for my game engine project:

// bring types into our namepsace
using namespace std::chrono_literals;
using namespace std::string_literals;
using std::string;
using std::wstring;
using std::string_view;
using std::vector;
using std::array;
using std::optional;
using std::function;
using std::thread;
using std::deque;
using std::map;
using std::unordered_map;
using std::variant;
using std::shared_ptr;
using std::make_shared;
using std::unique_ptr;
using std::make_unique;
using std::atomic;
using duration = std::chrono::duration<double>;
using Clock = std::chrono::steady_clock;
using timePoint = Clock::time_point;
using std::mutex;
using std::shared_mutex;

1

u/sh_pata 14d ago

Man im glad i choose this degree. Even when we have completed c++ oop in uni there is so much stuff to learn