Hey all, thanks for all the help. I'm still having at least one problem, but I've "finished" the program. I'm sure that all the ifs could be cleaned up, and I'm sure there are logic errors, but right now I'm mainly just working on getting the program just to work and run. Afterwards I will work on cleaning it up. Here is code I am getting, with the line the compiler error is giving to me in bold... Specific error is...
1>c:\users\mike\documents\visual studio 2005\projects\assignment9\assignment9\assignment9. cpp(78) : error C2106: '=' : left operand must be l-value
1>c:\users\mike\documents\visual studio 2005\projects\assignment9\assignment9\assignment9. cpp(79) : error C2106: '=' : left operand must be l-value
I'm guessing it has to do with assigning an array value to each other, which isn't possible, but I I guess I only understand the error for the first line it's associated with, not the second one.
Code:
#include "stdafx.h"
#include <ctime>
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <fstream>
using namespace std;
class Card{
enum Suit
{
clubs, diamonds, spades, hearts
};
enum Rank
{
two = 2, three = 3, four = 4, five = 5, six = 6, seven = 7, eight = 8, nine = 9, ten = 10,
jack = 10, queen = 10, king = 10, ace = 11
};
public:
Suit color;
Rank value;
Card(){
}
Card(int csuit, int crank){
csuit = color;
crank = value;
}
};
class CardDeck{
public:
Card deck[51];
CardDeck(){
//I think that it would be much easier to have the function initialize the cards rather than the constructor,
// as it eases the process considerably when it comes time to create a new deck once all 52 cards have been used
initializeDeck();
}
void initializeDeck(){
int d = 0;
for (int i = 0; i < 4; i++){
for (int j=0; i < 13; j++){
deck[d] = Card(i,j);
d++;
}
}
}
void shuffleDeck(){
srand(time(0));
int time = rand();
int temp = 0;
for (int i = 0; i < 51; i++){
time = i + (rand() % (52-i));
temp = (int)(deck[i].value);
(int)deck[i].value = (int)(deck[time].value);
(int)deck[time].value = temp;
}
}
int drawCard(){
int cardVal = 0;
static int _deckSize = 51;
if(_deckSize == 0){
initializeDeck();
_deckSize = 51;
}
cardVal = deck[_deckSize].value;
return cardVal;
_deckSize--;
}
};
int main()
{
cout << "Welcome to BlackJack!!!" << endl;
CardDeck cd;
cd.initializeDeck();
cd.shuffleDeck();
int playerSum;
int computerSum;
int dealerSum;
int temp = 0;
char hitOrStand;
bool stand = false;
bool playerBlackjack = false;
bool computerBlackjack = false;
char playAgain;
for(int i = 0; i < 2; i++){
temp += cd.drawCard();
playerSum += temp;
cout << "Player was dealt a" << temp << endl;
temp += cd.drawCard();
computerSum += temp;
cout << "Computer was dealt a " << temp << endl;
temp += cd.drawCard();
dealerSum += temp;
cout << "Dealer was dealt a " << temp << endl;
}
if(dealerSum == 21){
cout << "Dealer has blackjack! Player and Computer lose!" << endl;
cout << "Would you like to play again? (Press y for yes, n for no): ";
cin >> playAgain;
if(playAgain = 'y')
main();
}
if(playAgain != 'y')
{
if(playerSum == 21){
cout << "Player has blackjack!" << endl;
playerBlackjack = true;
}
if(computerSum == 21){
cout << "Computer has blackjack!" << endl;
computerBlackjack = true;
}
cout << "Dealer has" << dealerSum << endl;
cout << "\n\n Player has " << playerSum << endl;
cout << " \n\n Would the player like to hit or stand? (Press h for hit, s for stand) ";
cin >> hitOrStand;
if(hitOrStand = 'h'){
while(stand = false){
temp = cd.drawCard();
playerSum += temp;
cout << "You were dealt a " << temp << endl;
cout << "Player now has " << playerSum <<endl;
cout << "Would player like to hit or stand (Press h for hit, s for stand ) ";
cin >> hitOrStand;
if(hitOrStand = 's'){
stand = true;
}
}
}
cout << "\n\n\n Computer has " << computerSum << endl;
if(computerSum <= 11)
{
do
{
temp = cd.drawCard();
computerSum += temp;
}
while(computerSum<12);
if(computerSum == 21)
{
cout << "Computer has 21, Computer will Stand" << endl;
}
else if((dealerSum + 10 > computerSum))
{
do
{
temp = cd.drawCard();
computerSum += temp;
cout << "Computer drew a " << temp << " and now has " << computerSum << endl;
}
while((dealerSum + 10) > computerSum && computerSum <= 21);
}
else if(12 < dealerSum && dealerSum < 16 && 12 < computerSum && computerSum < 16)
{
}
}
cout << "Computer has " << computerSum << endl;
do
{
temp = cd.drawCard();
dealerSum += temp;
cout << "Dealer drew a " << temp << " and now has " << dealerSum << endl;
}
while(dealerSum < 17);
if(dealerSum > 21)
{
cout << "Dealer busts!" << endl;
}
if(dealerSum > playerSum || dealerSum > computerSum)
{
cout << "Dealer wins!" << endl;
}
if(playerSum > dealerSum)
{
cout << "Player wins!" << endl;
}
if(computerSum > dealerSum)
{
cout << "Computer wins!" << endl;
}
cout << "Would you like to play again? (Hit y for yes, n for no)";
cin >> playAgain;
if(playAgain == 'y')
{
main();
}
return 0;
}
}