Okay, everything works except for one strange bug. Whenever I hit option 2 without first drawing a card I get a debug assertion error "vector subscript out of range." If I draw a card whether or not I win or lose it gives me a win/lose case. But when I don't draw a cad it breaks.
Any ideas?
Code:
#include "Dealer.h"
using namespace std;
class Game
{
public:
Dealer dealer;
Player player;
void playgame()
{
int gameloop = 1;
InitValues();
dealer.ShuffleDeck();
dealer.Deal(2, dealer.Hand);
dealer.Deal(1, player.Hand);
while(gameloop != 0)
{
gameloop = BlackJack(dealer.Turn(player));
}
}
int BlackJack(int option)
{
if (option == 1) // check player hand, over 21?
{
if (Hand(player.Hand) > 21)
{
cout << "You Lose!!!!" << endl;
return 0;
}
else { return 1; }
}
if (option == 2) // compare hands and declare winner
{
if(Hand(player.Hand) > Hand(dealer.Hand))
{
cout << "You win\n!!!!";
return 0;
}
else { cout << " You Lose\n!!!"; return 0; }
}
if (option == 3) // player folds, remove them from game.
{
cout << "You Lose!!!\n";
return 0;
}
else{ return 0; }
}
int Hand(vector<Card> & hand)
{
sum = 0; int loop = 0;
for (loop; loop < hand.size(); loop++)
{
sum += values[player.Hand[loop].Value];
}
return sum;
}
void InitValues()
{
values.push_back(2);
values.push_back(3);
values.push_back(4);
values.push_back(5);
values.push_back(6);
values.push_back(7);
values.push_back(8);
values.push_back(9);
values.push_back(10);
values.push_back(10);
values.push_back(10);
values.push_back(10);
values.push_back(11);
}
int sum;
vector<int> values;
};
Code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "Deck.h"
#include "Player.h"
using namespace std;
class Dealer
{
public:
Dealer()
{
InitLookups();
}
void ShuffleDeck()
{
random_shuffle(deck.cards.begin(), deck.cards.end());
}
int Turn(Player & player)
{
int result = player.GetInput();
if (result == 1)
{
Deal(1, player.Hand);
return 1; // is player's hand > 21?
}
if (result == 2)
{
return 2; //compare cards declare winner
}
if (result == 3)
{
return 3; //player folded, remove player from game.
}
result = player.GetInput();
}
void Deal(int howmanycards, vector<Card> & hand)
{
for (int loop = 0; loop != howmanycards; loop++)
{
hand.push_back(deck.cards.back());
ReadCards(hand.back().Value, hand.back().Suit);
deck.cards.pop_back();
}
}
void ReadCards(int value,int suit)
{
v = value;
s = suit;
cout << valuelookup[value].c_str() << " of "
<< suitlookup[suit].c_str() << " ";
//card value and suit as an index...
}
// Creates Lookup tables for outputting card suits and values
void InitLookups()
{
string suit;
suit = "Clubs"; Add(suit);
suit = "Diamonds"; Add(suit);
suit = "Hearts"; Add(suit);
suit = "Spades"; Add(suit);
string value;
value = "Two"; Add2(value);
value = "Three"; Add2(value);
value = "Four"; Add2(value);
value = "Five"; Add2(value);
value = "Six"; Add2(value);
value = "Seven"; Add2(value);
value = "Eight"; Add2(value);
value = "Nine"; Add2(value);
value = "Ten"; Add2(value);
value = "Jack"; Add2(value);
value = "Queen"; Add2(value);
value = "King"; Add2(value);
value = "Ace"; Add2(value);
}
private:
void Add(string suit)
{
suitlookup.push_back(suit);
}
void Add2(string value)
{
valuelookup.push_back(value);
}
int v; int s;
//string lookups
vector<string> suitlookup;
vector<string> valuelookup;
vector<Card> Hand;
Deck deck;
friend class Game;
};
Code:
#include "Dealer.h"
using namespace std;
class Game
{
public:
Dealer dealer;
Player player;
void playgame()
{
int gameloop = 1;
InitValues();
dealer.ShuffleDeck();
dealer.Deal(2, dealer.Hand);
dealer.Deal(1, player.Hand);
while(gameloop != 0)
{
gameloop = BlackJack(dealer.Turn(player));
}
}
int BlackJack(int option)
{
if (option == 1) // check player hand, over 21?
{
if (Hand(player.Hand) > 21)
{
cout << "You Lose!!!!" << endl;
return 0;
}
else { return 1; }
}
if (option == 2) // compare hands and declare winner
{
if(Hand(player.Hand) > Hand(dealer.Hand))
{
cout << "You win\n!!!!";
return 0;
}
else { cout << " You Lose\n!!!"; return 0; }
}
if (option == 3) // player folds, remove them from game.
{
cout << "You Lose!!!\n";
return 0;
}
else{ return 0; }
}
int Hand(vector<Card> & hand)
{
sum = 0; int loop = 0;
for (loop; loop < hand.size(); loop++)
{
sum += values[player.Hand[loop].Value];
}
return sum;
}
void InitValues()
{
values.push_back(2);
values.push_back(3);
values.push_back(4);
values.push_back(5);
values.push_back(6);
values.push_back(7);
values.push_back(8);
values.push_back(9);
values.push_back(10);
values.push_back(10);
values.push_back(10);
values.push_back(10);
values.push_back(11);
}
int sum;
vector<int> values;
};
Code:
using namespace std;
class Player
{
int GetInput()
{
int choice = 0;
cout << "What would you like to do?" << endl;
cout << "1: Hit Me" << endl;
cout << "2: Stay" << endl;
cout << "3: Fold" << endl;
cin >> choice;
return choice;
}
friend class Dealer;
friend class Game;
vector<Card> Hand;
};
Also to note, when I set a breakpoint for debugging before I call the turn function it says
"failed to create breakpoint"
unable to parse function int Turn(player)