Sign in to follow this  
Followers 0
Hellu

I'm going crazy...Black Jack card game c++

5 posts in this topic

Hello there boys and girls. I'm attempting to do a black jack card game.

My problem at the moment is that it won't continute looping through everything, after one loop. I want it to stop looping when the two bool variables, p1Ready and p2Ready, are false(You'll understand if you take a peek at the code). What I'm thinking is that I've never changed the bool variables to false but my while-loop keeps accepting the variables as false(?). I'm not sure if it's the bool variables fault or not but something is up and I'm not sure what it is.

Don't mind the comments, they are merely there to help me keep track of things.

 

This is my code at the moment, be aware that it isn't complete yet. Any comments or thoughts is much appreciated!

#include <iostream>#include <string>#include <cstdlib>#include <ctime>using namespace std;int main(){		srand(time(NULL));	//Variabler	int const CAP = 52;	int const PLAYERCAP = 2;	int cards[CAP];	bool p1Ready = true, p2Ready = true;	bool checkArray[CAP];	string players[PLAYERCAP];	int p1Points = 0, p2Points = 0, n, p1Wins = 0, p2Wins = 0;	char choice;	int counter = 0;	//Sätter värderna i cards - arrayen	for (int i = 0; i < 52; i = i + 4){		cards[i] = counter + 1;		cards[i + 1] = counter + 1;		cards[i + 2] = counter + 1;		cards[i + 3] = counter + 1;		counter++;	}	//Ger bool-arrayen checkArray "värdena" true	for (int i = 0; i < 52; i++){		checkArray[i] = cards[i];	}	cout << "Welcome to the cardgame Black Jack" << endl;	cout << "Please, enter the name of player 1" << endl;	cin >> players[0];	cout << "Please, enter the name of player 2" << endl;	cin >> players[1];	do{		//Här börjar spelare ett med att välja mellan att vilja ta ett kort eller inte		if (p1Ready == true){			cout << "Would you like a card, " << players[0] << "? (J/N)" << endl;			cin >> choice;			if (choice == 'J' || choice == 'j'){				n = rand() % 51;				while (checkArray[n] == false){					n = rand() % 51;					p1Points += cards[n];					cout << cards[n] << " is your cards value!" << endl;					cout << p1Ready;				}				cout << cards[n] << " is your cards value!" << endl;				p1Points += cards[n];				cout << "You now got a total of " << p1Points << " points" << endl;				cout << p1Ready;			}		}		else if (choice == 'N' || choice == 'n'){			cout << players[0] << "you decided that you didn't want to play anymore, " << p1Points << " points" << endl;			p1Ready = false;		}		//Här börjar spelare två med att välja mellan att vilja ta ett kort eller inte		if (p2Ready == true){			cout << "Would you like a card, " << players[1] << "? (J/N)" << endl;			cin >> choice;			if (choice == 'J' || choice == 'j'){				n = rand() % 51;				while (checkArray[n] == false){					n = rand() % 51;					p2Points += cards[n];					cout << cards[n] << " is your cards value!" << endl;					cout << p2Ready;				}				cout << cards[n] << " is your cards value!" << endl;				p2Points += cards[n];				cout << "You now got a total of " << p1Points << " points" << endl;				cout << p2Ready;			}		}		else if (choice == 'N' || choice == 'n'){			cout << players[1] << "you decided that you didn't want to play anymore, " << p2Points << " points" << endl;			p2Ready = false;		}	} while (p2Ready == false || p1Ready == false);	cout << "You decided you did not want to play anymore." << endl << endl;	cout << players[0] << " you won " << p1Wins << " time(s)." << endl << endl;	cout << players[1] << " you won " << p2Wins << " time(s)." << endl << endl;	return 0;}

Share this post


Link to post
Share on other sites

Update:

 

It was as simple as my bool variables p1Ready and p2Ready were set to true, and if they were set to true of course it would end considering my while-loop was set with p1Ready == false & p2Ready == false.

 

Take this as an example to KNOW YOUR BOOLS(heuheu it's almost sounds like boobs), luckily I had a little help discovering this.

Share this post


Link to post
Share on other sites

Nice that you fixed your problem by yourself, after I took a look I was going to tell you the same ;)

I don´t have a clue about BlackJack but thanks for sharing some code with us.

 

greetings

Share this post


Link to post
Share on other sites

I'd love to test your game once it's done, preferably before you add in a "reset card stack" feature ;)

 

I kinda want to break it, by counting xD

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0