I'm trying to code a blackjack game. Until the last part it was going pretty well. My code is as following:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
//Declarations
void makedeck(int deck[]);
int getTopCard(int deck[]);
void addCardToHand(int hand[], int drewCard);
void firstHand(int pHand[], int dHand[], int deck[]);
void printHands(int pHand[], int dHand[]);
void showCard(int card);
int getHandValue(int hand[]);
int main()
{
int pHand[10]= {0}; //Player's Hand
int dHand[10]= {0}; //Dealer's Hand
int deck[52]; //Deck
makedeck(deck); //Generating an ordered deck
random_shuffle(&deck[0],&deck[52]); //Shuffling the deck
firstHand(pHand,dHand,deck); //Dealing first hands.
printHands(pHand,dHand); // Printing hands
return 0;
}
/*Purpose of this function is to create 4 different suits with 14 different cards in all of them, a complete deck*/
void makedeck(int deck[])
{
int a=0;
for(int x=101; x<114; x++)
{
deck[a]=x;
a++;
}
for(int x=201; x<214; x++)
{
deck[a]=x;
a++;
}
for(int x=301; x<314; x++)
{
deck[a]=x;
a++;
}
for(int x=401; x<414; x++)
{
deck[a]=x;
a++;
}
}
/*This function returns a topcard integer as value, that is the card at top of deck, then nullifies it from deck to prevent
it from being drawn again.*/
int getTopCard(int deck[])
{
int topcard;
for(int x=0; x<52; x++)
{
if(deck[x]!=0)
{
topcard=deck[x];
deck[x]=0;
break;
}
}
return topcard;
}
/*This function draws four cards from top of deck and deals them respectively to the order of rules*/
void firstHand(int pHand[], int dHand[], int deck[] )
{
addCardToHand(pHand , getTopCard(deck) );
addCardToHand(dHand , getTopCard(deck) );
addCardToHand(pHand , getTopCard(deck) );
addCardToHand(dHand , getTopCard(deck) );
}
/*This function is for further Hit actions. Adds the drawnCard to the desired hand. The value of drawnCard is returned
from the getTopCard() function*/
void addCardToHand(int hand[], int drawnCard)
{
for(int x=0; x<=10; x++)
{
if(hand[x]!=0)
{
continue;
}
if(hand[x]==0)
{
hand[x]=drawnCard;
break;
}
}
}
/*This function determines the values of cards. Since suits that reside in the 100s part are not relevant to the value
of the cards and are only required for seperating two cards with same value, in order to remove them from the information
that we'll print, we use % on card values*/
void showCard(int card)
{
if(card==0) cout << " ";
else
switch(card%100)
{
case 1:
cout << "A ";
break;
case 11:
cout << "J ";
break;
case 12:
cout << "Q ";
break;
case 13:
cout << "K ";
break;
default:
cout << card%100 << " ";
}
}
/*Prints both hands*/
void printHands(int pHand[],int dHand[])
{
cout << "Dealer's hand: ";
for(int y=0; y<10; y++)
{
showCard(dHand[y]);
}
cout << getHandValue(dHand) << endl;
cout << endl << "Player's hand: ";
for(int x=0; x<10; x++)
{
showCard(pHand[x]);
}
cout << getHandValue(pHand) << endl;
}
/*Sums all the values of cards in the hand and returns it as an integer*/
int getHandValue(int hand[])
{
int sum=0;
for(int x=0; x<10; x++)
{
if(hand[x]%100==11) sum+=10;
if(hand[x]%100==12) sum+=10;
if(hand[x]%100==13) sum+=10;
else sum+=hand[x]%100;
}
return sum%100;
}
Now when I execute it, I get the following:
Dealer's hand: K 3 13
Player's hand: 5 Q 27
The dealer's hand is correct, however since 5 + Q should be 15 and not 27, there is something wrong that causes a wrong print but I failed to find it so far. Can anyone help?
How to calculate sum of all elements of an array in C# In this post, we will discuss how to calculate sum of all elements in an integer array in C#. 1. Enumerable.Sum Method We can make use the build-in numeric aggregation method Sum()from the System.LinqNamespace to compute the sum of numeric values in a sequence.
Find an element that divides the array into two sub-arrays with equal sums. Recommended: Please solve it on “ PRACTICE ” first, before moving on to the solution. Consider every element starting from the second element. Compute the sum of elements on its left and the sum of elements on its right. If these two sums are the same, return the element.
Sum of given array is 34. Time Complexity: O(n) Auxiliary Space: O(1) Another Method: Using STL Calling inbuilt function for sum of elements of an array in STL. accumulate(first, last, sum); first, last : first and last elements of range whose elements are to be added sum : initial value of the sum. This function returns the array sum.
We can make use of the built-in numeric aggregation method Sum () from the System.Linq namespace to compute the sum of numeric values in a sequence. This is demonstrated below: 2. Using Array.ForEach () method Using the Array.ForEach () method, we can perform the addition operation on each element of the specified array.
You used if
where you needed else if
if(hand[x]%100==11) sum+=10;
if(hand[x]%100==12) sum+=10;
if(hand[x]%100==13) sum+=10;
else sum+=hand[x]%100;
should be
if(hand[x]%100==11) sum+=10;
else if(hand[x]%100==12) sum+=10;
else if(hand[x]%100==13) sum+=10;
else sum+=hand[x]%100;
If you want to also handle ace correctly:
/*Sums all the values of cards in the hand and returns it as an integer*/
int getHandValue(int hand[])
{
bool ace=false;
int sum=0;
for(int x=0; x<10; x++)
{
if(hand[x]%100==1) ace=true;
if(hand[x]%100>=11) sum+=10;
else sum+=hand[x]%100;
}
if ( sum<12 && ace ) sum+=10;
return sum;
}
In the above code you have used 3 if
statements with 1 else
statement where you wanted to use 'else if' statements (Only one of the if
statements are possible for each value of hand[x]
).
if(hand[x]%100==11) sum+=10;
if(hand[x]%100==12) sum+=10;
if(hand[x]%100==13) sum+=10;
else sum+=hand[x]%100;
else if
statements like this will work:
if (hand[x]%100==11)
sum+=10;
else if (hand[x]%100==12)
sum+=10;
else if (hand[x]%100==13)
sum+=10;
else sum+=hand[x]%100;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With