if (Deck[0] = 0)
Change it to
if (Deck[0] == 0)
if (Deck[0] = 0)
if (Deck[0] == 0)
class Card
{
public:
Card();
int cardValue;
int cardType;
void applyCard(int x,int y);
void rerand();
};
void Card::rerand()
{
cardType = (rand() % 13);
cardValue = 0;
}
Card::Card()
{
cardType = (rand() % 13);
cardValue = 0;
switch (cardType)
{
case 0:
if (Deck[0] == 0)
{
Deck[0] = 1;
cardValue = 2;
}
else
rerand();
break;
case 1:
if (Deck[1] == 0)
{
Deck[1] = 1;
cardValue = 3;
}
else
rerand();
break;
case 2:
if (Deck[2] == 0)
{
Deck[2] = 1;
cardValue = 4;
}
else
rerand();
break;
case 3:
if (Deck[3] == 0)
{
Deck[3] = 1;
cardValue = 5;
}
else
rerand();
break;
case 4:
if (Deck[4] == 0)
{
Deck[4] = 1;
cardValue = 6;
}
else
rerand();
break;
case 5:
if (Deck[5] == 0)
{
Deck[5] = 1;
cardValue = 7;
}
else
rerand();
break;
case 6:
if (Deck[6] == 0)
{
Deck[6] = 1;
cardValue = 8;
}
else
rerand();
break;
case 7:
if (Deck[7] == 0)
{
Deck[7] = 1;
cardValue = 9;
}
else
rerand();
break;
case 8:
if (Deck[8] == 0)
{
Deck[8] = 1;
cardValue = 10;
}
else
rerand();
break;
case 9:
if (Deck[9] == 0)
{
Deck[9] = 1;
cardValue = 10;
}
else
rerand();
break;
case 10:
if (Deck[10] == 0)
{
Deck[10] = 1;
cardValue = 10;
}
else
rerand();
break;
case 11:
if (Deck[11] == 0)
{
Deck[11] = 1;
cardValue = 10;
}
else
rerand();
break;
case 12:
if (Deck[12] == 0)
{
Deck[12] = 1;
cardValue = 10;
}
else
rerand();
break;
case 13:
if (Deck[13] == 0)
{
Deck[13] = 1;
cardValue = 11;
}
else
rerand();
break;
}
}
void Card::applyCard(int x, int y)
{
SDL_Surface *Card_image = NULL;
switch(cardType)
{
case 0:
Card_image = clarify_image("Art/2Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 1:
Card_image = clarify_image("Art/3Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 2:
Card_image = clarify_image("Art/4Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 3:
Card_image = clarify_image("Art/5Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 4:
Card_image = clarify_image("Art/6Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 5:
Card_image = clarify_image("Art/7Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 6:
Card_image = clarify_image("Art/8Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 7:
Card_image = clarify_image("Art/9Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 8:
Card_image = clarify_image("Art/10Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 9:
Card_image = clarify_image("Art/JClub.png");
apply_surface(x,y,Card_image,screen);
break;
case 10:
Card_image = clarify_image("Art/QClub.png");
apply_surface(x,y,Card_image,screen);
break;
case 11:
Card_image = clarify_image("Art/KClub.png");
apply_surface(x,y,Card_image,screen);
break;
case 12:
Card_image = clarify_image("Art/AClub.png");
apply_surface(x,y,Card_image,screen);
break;
}
}
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <cassert>
enum Suit
{
Heart, Club, Diamond, Spade
};
std::ostream &operator<<(std::ostream &out, Suit suit)
{
static const std::string lookup[] = { "Hearts", "Clubs", "Diamonds", "Spades" };
assert(suit >= 0 && suit < 4);
return out << lookup[suit];
}
enum CardType
{
One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King
};
std::ostream &operator<<(std::ostream &out, CardType cardType)
{
assert(cardType >= 0 && cardType <= King);
switch(cardType)
{
case Jack: out << "Jack"; break;
case Queen: out << "Queen"; break;
case King: out << "King"; break;
default:
out << static_cast<int>(cardType + 1);
break;
}
return out;
}
class Card
{
public:
Card(CardType type, Suit suit)
:
suit(suit),
type(type)
{
}
// ...
friend std::ostream &operator<<(std::ostream &out, const Card &card)
{
return out << card.type << " of " << card.suit;
}
private:
Suit suit;
CardType type;
};
class Deck
{
public:
Deck();
void shuffle();
Card takeCard();
bool isEmpty() const;
private:
std::vector<Card> cards;
};
Deck::Deck()
{
for(int suit = Heart ; suit <= Spade ; ++suit)
{
for(int type = One ; type <= King ; ++type)
{
Card card = Card(static_cast<CardType>(type), static_cast<Suit>(suit));
cards.push_back(card);
}
}
}
void Deck::shuffle()
{
std::random_shuffle(cards.begin(), cards.end());
}
bool Deck::isEmpty() const
{
return cards.empty();
}
Card Deck::takeCard()
{
if(isEmpty())
{
throw std::runtime_error("Cannot take a card from an empty deck!");
}
Card card = cards.back();
cards.pop_back();
return card;
}
int main()
{
Deck deck;
deck.shuffle();
std::string input;
while(!deck.isEmpty() && std::cout << "Press enter to be dealt a card!" && std::getline(std::cin, input))
{
std::cout << "Dealing card: " << deck.takeCard() << '\n';
}
if(deck.isEmpty())
{
std::cout << "Ran out of cards!\n";
}
else
{
std::cout << "Bye...";
}
}
int main ( int argc, char *args[] )
{
srand(time(0));
bool quit = false;
if( init() == false ) { return 1;}
if( load_files() == false ) { return 2;}
apply_surface( 0,0,Table,screen);
apply_surface(370,513,Border,screen);
Card1.applyCard(100,280);
Card2.applyCard(140,280);
Card3.applyCard(420,50);
while (quit == false)
{
option();
while ( SDL_PollEvent( &event ) )
{
if( event.type == SDL_QUIT )
{
quit = true;
}
}
SDL_Flip( screen );
}
cleanup();
return 0;
}
class Card
{
public:
Card();
int cardValue;
int cardType;
void applyCard(int x,int y);
void rerand();
};
void Card::rerand()
{
cardType = (rand() % 13);
cardValue = 0;
}
Card::Card()
{
cardType = (rand() % 13);
cardValue = 0;
switch (cardType)
{
case 0:
if (Deck[0] == 0)
{
Deck[0] = 1;
cardValue = 2;
}
else
rerand();
break;
case 1:
if (Deck[1] == 0)
{
Deck[1] = 1;
cardValue = 3;
}
else
rerand();
break;
case 2:
if (Deck[2] == 0)
{
Deck[2] = 1;
cardValue = 4;
}
else
rerand();
break;
case 3:
if (Deck[3] == 0)
{
Deck[3] = 1;
cardValue = 5;
}
else
rerand();
break;
case 4:
if (Deck[4] == 0)
{
Deck[4] = 1;
cardValue = 6;
}
else
rerand();
break;
case 5:
if (Deck[5] == 0)
{
Deck[5] = 1;
cardValue = 7;
}
else
rerand();
break;
case 6:
if (Deck[6] == 0)
{
Deck[6] = 1;
cardValue = 8;
}
else
rerand();
break;
case 7:
if (Deck[7] == 0)
{
Deck[7] = 1;
cardValue = 9;
}
else
rerand();
break;
case 8:
if (Deck[8] == 0)
{
Deck[8] = 1;
cardValue = 10;
}
else
rerand();
break;
case 9:
if (Deck[9] == 0)
{
Deck[9] = 1;
cardValue = 10;
}
else
rerand();
break;
case 10:
if (Deck[10] == 0)
{
Deck[10] = 1;
cardValue = 10;
}
else
rerand();
break;
case 11:
if (Deck[11] == 0)
{
Deck[11] = 1;
cardValue = 10;
}
else
rerand();
break;
case 12:
if (Deck[12] == 0)
{
Deck[12] = 1;
cardValue = 10;
}
else
rerand();
break;
case 13:
if (Deck[13] == 0)
{
Deck[13] = 1;
cardValue = 11;
}
else
rerand();
break;
}
}
void Card::applyCard(int x, int y)
{
SDL_Surface *Card_image = NULL;
switch(cardType)
{
case 0:
Card_image = clarify_image("Art/2Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 1:
Card_image = clarify_image("Art/3Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 2:
Card_image = clarify_image("Art/4Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 3:
Card_image = clarify_image("Art/5Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 4:
Card_image = clarify_image("Art/6Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 5:
Card_image = clarify_image("Art/7Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 6:
Card_image = clarify_image("Art/8Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 7:
Card_image = clarify_image("Art/9Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 8:
Card_image = clarify_image("Art/10Club.png");
apply_surface(x,y,Card_image,screen);
break;
case 9:
Card_image = clarify_image("Art/JClub.png");
apply_surface(x,y,Card_image,screen);
break;
case 10:
Card_image = clarify_image("Art/QClub.png");
apply_surface(x,y,Card_image,screen);
break;
case 11:
Card_image = clarify_image("Art/KClub.png");
apply_surface(x,y,Card_image,screen);
break;
case 12:
Card_image = clarify_image("Art/AClub.png");
apply_surface(x,y,Card_image,screen);
break;
}
}
Card Card1;
Card Card2;
Card Card3;