1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/*
*/
//Libraries
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <fstream>
using namespace std;
//Global Constants
int score, block;
int pickedCard;
string winner, answer;
int activePlayer = 0;
const int playerSize = 2;
string name[playerSize];
bool endGame = false;
const int size = 52;
int cardDeck[size] = {1,2,3,4,5,6,7,8,9,10,11,12,13,
1,2,3,4,5,6,7,8,9,10,11,12,13,
1,2,3,4,5,6,7,8,9,10,11,12,13,
1,2,3,4,5,6,7,8,9,10,11,12,13};
bool usedCard[size];
int totalScore[playerSize] = {0, 0};
//Function Prototypes
void game();
int deal();
void nameFunction();
void playAgain();
void saveResult(string);
void swapPlayer();
void checkWin();
void handlePlayer();
int drawCard(bool, const int);
//Begin Execution
int main(){
cout << "Welcome to blackjack" << endl;
nameFunction();
endGame = false;
while (endGame==false) {
game();
}
return 0;
}
//main function runs the game
void game(){
for (int i=0; i<size; i++) {
usedCard[i]=false;
}
srand(time(0));
handlePlayer();
}
//names the player and sends to array
void nameFunction()
{
string playername;
for (int i = 0; i<2; i++)
{
cout << "\nEnter player name " << i+1 << ": ";
getline(cin,playername);
name[i] = playername;
}
}
//function that handles
void handlePlayer()
{
string answer;
cout << name[activePlayer] << ", you have " <<totalScore[activePlayer] << " points" << endl;
cout << "hit or stand (H/S)?";
cin >> answer;
if(answer == "H"||answer=="h")
{
pickedCard = deal();
cout << "Dealer has given " << name[activePlayer] << " a card worth " << pickedCard << " points." << endl;
cout << endl;
}
checkWin();
swapPlayer();
}
//function shuffles cards
int deal()
{
block = rand()%51;
if(usedCard[block] == true)
{
deal();
}
else
{
score = cardDeck[block];
totalScore[activePlayer]+= score;
usedCard[block] = true;
}
return score;
}
//function for alternating players
void swapPlayer()
{
if(activePlayer==0)
{
activePlayer=1;
}
else
{
activePlayer=0;
}
}
//function to find a winner
void checkWin()
{
if(totalScore[0] > 21)
{
cout << name[0] << " bust over 21." << endl;
cout << name[1] << "has won!" << endl;
winner = name[1];
saveResult(winner);
endGame = true;
cout << "play again?(Y/N)?";
cin >> answer;
if (answer=="Y"||answer=="y") {
playAgain();
}
else
{
cout << "thanks for playing" << endl;
}
}
if(totalScore[1] > 21)
{
cout << name[1] << "over 21 bust!." << endl;
cout << name[0] << " has won!" << endl;
winner = name[1];
saveResult(winner);
endGame = true;
cout << "play again? (Y/N)?";
cin >> answer;
if (answer=="Y"||answer=="y") {
playAgain();
}
else
{
cout << "thank you for playing" << endl;
}
}
}
void playAgain()
{
totalScore[0] = 0;
totalScore[1] = 0;
endGame = false;
swapPlayer();
game();
}
//writes results to a file
void saveResult(string winner)
{
ofstream file;
file.open ("result.txt", ios::app);
file << winner << " has won" << endl;
file.close();
}
|