Lab 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Lab 3 Fall 2014

To execute the program, copy the executable file BlackJack.


Rules for BlackJack (also called 21)
1) This game will be played with a standard straight deck of 52 cards. Each card has a suit
(heart, diamond, club, or spade) and a rank (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, or
King). The value of a card is 1 for an Ace; ranks 210 have a value equal to their rank,
and the Jack, Queen, and King have a value of 10.
2) This game will be played with seven players, each against the dealer.
3) The game begins by dealing two cards to each of the players and the dealer. One of the
dealer cards is not exposed.
4) Each player in turn can ask for many additional cards as s/he wishes, one at a time. If a
players total exceeds 21, that player automatically loses to the dealer and is no longer in
that game.
5) After each player has received all their cards, the dealer must take additional cards until
the dealers total is at least 17, after which he can take no more cards.
6) If the dealer has a total > 21, all remaining players with total < 22 win. Otherwise, each
player wins if has a greater total than the dealer, or the dealer wins if he has a total greater
than the player. Nobody wins on a tie.
7) More about Win/Lose/Tie
a. The dealer does not get a designation of W/L/T.
b. Each player is playing against the dealer and NOT against other players.
c. A player ties the dealer if the dealer is under 22 and both that player and the
dealer have the same totals.
d. A player wins the dealer if:
1) The dealer is over 21 and the player is under 22.
OR
2) Both player and dealer under 22 and the player's total is higher than the dealer's
total

e. A player lose to the dealer if:


1) The player is over 21.
OR
2) The player's total is less than the dealer's total and the dealer is under 22.
Your assignment is to design and implement a complete C++ program that plays the game
interactively with 7 players. To observe how the program works, you can download and
execute the file BlackJack.exe.

Hints for Lab 3


1) Think how the game is played. If you are not sure about a rule, ask me. Think of me
as your customer and you are writing a program for me.
2) Break down the problem to sub problems. Here are some suggestion:
Shuffle the deck using rand()%52; Remember to use srand(time(Null));
Write a function to print your header.
Write a function to deal two cards to each player. Keep one of the cards dealt
to the dealer covered or only deal one card to the dealer.
Write a function to update on the screen the total for a player. As you deal
them another card, the total should be updated accordingly. Use row 15 for
your totals.
You always give a player a choice to take a card or pass. When a player
passes his turn, her/his total remains for the duration of the game.
When dealing a card, a function is called to generate the next available card.
Gotoxy(column,row) will position your cursor at that location and what you
send using the cout function will print to that location on the screen.
Therefore, you need to keep track for each player what is the next row to write
to. The column can be calculated using 0 to start for dealer and add 7 for each
player. Therefore, after dealing the first two cards, dealers third card when
dealt will be placed in 0,3 using gotoxy(0,3) and the first players third card
will be placed in 7,3 using gotoxy(7,3) and so on.
When printing a card value to the screen, right justify to line up your suit.
A=1, J=10, Q=10, and K=10. Hint: A % operator will work great to know
what card you picked. A switch statement may come in handy to assign
values for these cards. Keep in mind that A, J, Q, K must be displayed as A,
J, Q, and K.

Use an array to hold your cards. There are four suits in a deck and 13 cards
for a suit (A, 210, J, Q, and K). Using a / operator with a switch
statement can pin point the suit of the selected card.
To print a suit after detecting it, char(3), char(4), char(5) and char(6) will print
heart, diamond, club, and spade respectively.
The function Another_Card will ask if you want to receive a card for the
current player. If yes, then call the appropriate function and display the
selected card. Otherwise, move on to the next player.
An additional 10% will be awarded for a program that will allow a user to
play another game without restarting it. Hint a call to a system function cls
will clear the screen for you.
A function to finish the dealer after all players are done. This function will
display the first card to the dealer and will automatically finish the dealer
according to the rules.
A function to calculate users status after the dealer is done. Win Lose, or Tie.

BlackJack starter code...


#include <iostream>
#include <time.h>
#include <iomanip>
#include <windows.h>
using namespace std;
/*
Header file creates a function to move the position of
the cursor in a console window.
simply use "#include <gotoxy.h>" in your *.cpp file and
then you can call the function "gotoxy(n,n);" with "n" being
any integer to move your cursor to that position in your console
window.
windows.h and stdio.h have been include for convenience.
Created: 11/18/04
Modified/Header Author: Jason Olsen
Original code taken from:
"http://www.codeproject.com/cpp/cppforumfaq.asp?msg=729023#cons_gotoxy"
*/
//#include <stdio.h>
void gotoxy(int h, int w)
{
HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
if ( INVALID_HANDLE_VALUE != hConsole )
{
COORD pos = {h, w};
SetConsoleCursorPosition ( hConsole, pos );
}
return;
}
int main()
{
int cards[52], duplicate[52], number, i, cardValue, cardValueSwitch;
char suit, cardFace;
srand(time(NULL));
for(i = 0; i < 52; i++)
duplicate[i] = 0;
//shuffling the deck
for(i = 0; i < 52; i++)
{
number = rand()%52;
while(duplicate[number])
number = rand()%52;

cards[i] = number;
duplicate[number] = 1;
}
for(i = 0; i < 8; i++)
{
suit = char(cards[i]/13+3);
cardValueSwitch = cards[i]%13;
if(cardValueSwitch < 10)
cardValue = cardValueSwitch + 1;
else
cardValue = 10;
cardFace = ' ';
if(cardValueSwitch == 0)
cardFace = 'A';
else if(cardValueSwitch == 10)
cardFace = 'J';
else if(cardValueSwitch == 11)
cardFace = 'Q';
else if(cardValueSwitch == 12)
cardFace = 'K';
//cout << setw(2) << cards[i] << setw(2) << suit << setw(3) << cardValue;
//column,row
gotoxy(i*7, 3);
if(cardFace == ' ')
cout << setw(3) << cardValue;
else
cout << setw(3) << cardFace;
cout << suit;
cout << endl;
}
cout << endl;
system("pause");
return 0;
}

You might also like