21. juli 2008 - 12:14Der er
20 kommentarer og 1 løsning
fejlfinding: 13+ fejl i 498 linjer kode
Hej..
Jeg har skrevet en meget stor kode ind på Visual Studio 2008 Professional (ifølge arne_v i http://www.eksperten.dk/spm/838900 skulle det virke på en eller anden måde).. jeg er sikker på at den virkede i VS98, dog kan det være der er skrevet fejl et par steder.. er der nogen der vil/kan hjælpe mig over email/msn? jeg har koden liggende i et tekstdokument, og fejlene har jeg taget screenshot af, så jeg kan sende filerne over email/msn, hvis nogen er villig til at hjælpe? :-)
Hvad er der for noget kode? Hvis du prøver at bruge MFC f.eks, så vil det ikke virke. De skiftede det helt helt ud for nyligt, blandt andet på grund af MFC's mange brud på C++ standarden.
Det ville være nemmere at starte forfra med et rent MFC projekt, og så inkorporere dine ændringer deri.
-- // Blackjack.cpp : Defines the entry point for the console application. // Description: Plays a simple version of the casino game of blackjack; for 1 - 7 players.
int Card::GetValue() const { // if a card is face down, its value is 0 int value = 0; if (m_IsFaceUp) { // value is number showing on card value = m_Rank; // value is 10 for face cards if (value > 10) value = 10; } return value; }
void Card::Flip() { m_IsFaceUp = !(m_IsFaceUp); }
class Hand { public: Hand();
virtual ~Hand();
// adds a card to the hand void Add(Card* pCard);
// clears hand of all cards void Clear();
// gets hand total value, intelligently treats aces as 1 or 11 int GetTotal() const;
protected: vector<Card*> m_Cards; };
Hand::Hand() { m_Cards.reserve(7); }
Hand::~Hand() // don't use the keyword virtual outside of class definition { Clear(); }
void Hand::Clear() { // iterate through vector, freeing all memory on the heap vector<Card*>::iterator iter = m_Cards.begin(); for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter) { delete *iter; *iter = 0; } // clear vector of pointers m_Cards.clear(); }
int Hand::GetTotal() const { // if no cards in hand, return 0 if (m_Cards.empty()) return 0;
// if a first card has value of 0, then card is face down; return 0 if (m_Cards[0]->GetValue() == 0) return 0;
// add up card values, treat each ace as 1 int total = 0; vector<Card*>::const_iterator iter; for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter) total += (*iter)->GetValue();
// determine if hand contains an ace bool containsAce = false; for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter) if ((*iter)->GetValue() == Card::ACE) containsAce = true;
// if hand contains ace and total is low enough, treat ace as 11 if (containsAce && total <= 11) // add only 10 since we've already added 1 for the ace total += 10;
return total; }
class GenericPlayer : public Hand { friend ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer);
public: GenericPlayer(const string& name = "");
virtual ~GenericPlayer();
// indicates whether or not generic player wants to keep hitting virtual bool IsHitting() const = 0;
// returns whether generic player has busted - has a total greater than 21 bool IsBusted() const;
// announced that the generic player busts void Bust() const;
void House::FlipFirstCard() { if (!(m_Cards.empty())) m_Cards[0]->Flip(); else cout << "No card to flip!\n"; }
class Deck : public Hand { public: Deck();
virtual ~Deck();
// create a standard deck of 52 cards void Populate();
// shuffle cards void Shuffle();
// deal one card to a hand void Deal(Hand& aHand);
// give additional cards to a generic player void AdditionalCards(GenericPlayer& aGenericPlayer); };
Deck::Deck() { m_Cards.reserve(52); Populate(); }
Deck::~Deck() {}
void Deck::Populate() { Clear(); // create standard deck for (int s = Card::CLUBS; s <= Card::SPADES; ++s) for (int r = Card::ACE; r <= Card::KING; ++r) Add(new Card(static_cast<Card::rank>(r), static_cast<Card::suit>(s))); }
void Deck::Deal(Hand& aHand) { if (!m_Cards.empty()) { aHand.Add(m_Cards.back()); m_Cards.pop_back(); } else { cout << "Out of cards. Unable to deal."; } }
void Deck::AdditionalCards(GenericPlayer& aGenericPlayer) { cout << endl; // continue to deal a card as long as generic player isn't busted and // wants another hit while (!(aGenericPlayer.IsBusted()) && aGenericPlayer.IsHitting()) { Deal(aGenericPlayer); cout << aGenericPlayer << endl;
if (aGenericPlayer.IsBusted()) aGenericPlayer.Bust(); } }
class Game { public: Game(const vector<string>& names);
~Game();
// plays the game of blackjack void Play();
private: Deck m_Deck; House m_House; vector<Player> m_Players; };
Game::Game(const vector<string>& names) { // create a vector of players from a vector of names vector<string>::const_iterator pName; for (pName = names.begin(); pName != names.end(); ++pName) m_Players.push_back(Player(*pName));
srand(time(0)); // seed the random number generator m_Deck.Populate(); m_Deck.Shuffle(); }
Game::~Game() {}
void Game::Play() { // deal initial 2 cards to everyone vector<Player>::iterator pPlayer; for (int i = 0; i < 2; ++i) { for (pPlayer = m_Player.begin(); pPlayer != m_Player.end(); ++pPlayer) m_Deck.Deal(*pPlayer); m_Deck.Deal(m_House); }
// hide fouse's first card m_House.FlipFirstCard();
// deal additional cards to house m_Deck.AdditionalCards(m_House);
if (m_House.IsBusted()) { // everyone still playing wins for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) if (!(pPlayer->IsBusted())) pPlayer->Win(); } else { // compare each player still paying to house for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) if (!(pPlayer->IsBusted())) { if (pPlayer->GetTotal() > m_House.GetTotal()) pPlayer.Win(); else if (pPlayer->GetTotal() < m_House.GetTotal()) pPlayer.Lose(); else pPlayer.Push(); } }
int main() { cout << "\t\tWelcome to Blackjack!\n\n";
int numPlayer = 0; while (numPlayers < 1 && numPlayers > 7) { cout << "How many players? (1 - 7): "; cin >> numPlayers; }
vector<string> names; string name; for (int i = 0; i < numPlayers; ++i) { cout << "Enter player name: "; cin >> name; names.push_back(name); } cout << endl;
// the game loop Game aGame(names); char again = 'y'; while (again != 'n' && again != 'N') { aGame.Play(); cout << "\nDo you want to play again? (Y/N): "; cin >> again; }
system("pause"); return (0); }
// overloads << operator so Card object can be sent to cout ostream& operator<<(ostream& os, const Card& aCard) { const string RANKS[] = {"0", "a", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; const string SUITS[] = {"c", "d", "h", "s"};
if (aCard.m_IsFaceUp) os << RANKS[aCard.m_Rank] << SUITS[aCard.m_Suit]; else os << "XX";
return os; }
// overloads << operator so GenericPlayer object can be sent to cout ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer) { os << aGenericPlayer.m_Name << ":\t";
vector<Card*>::const_iterator pCard; if (!aGenericPlayer.m_Cards.empty()) { for (pCard = aGenericPlayer.m_Cards.begin(); pCard != aGenericPlayer.end(); ++pCard) os << *(*pCard) << "\t";
fejlene har jeg taget screenshot af og sendt til min email så jeg kan poste dem mens jeg er på arbejde, så jeg kan ikke helt skrive dem her.. - imageshack virker ikke på denne computer
// Blackjack.cpp : Defines the entry point for the console application. // Description: Plays a simple version of the casino game of blackjack; for 1 - 7 players.
int Card::GetValue() const { // if a card is face down, its value is 0 int value = 0; if (m_IsFaceUp) { // value is number showing on card value = m_Rank; // value is 10 for face cards if (value > 10) value = 10; } return value; }
void Card::Flip() { m_IsFaceUp = !(m_IsFaceUp); }
class Hand { public: Hand();
virtual ~Hand();
// adds a card to the hand void Add(Card* pCard);
// clears hand of all cards void Clear();
// gets hand total value, intelligently treats aces as 1 or 11 int GetTotal() const;
protected: vector<Card*> m_Cards; };
Hand::Hand() { m_Cards.reserve(7); }
Hand::~Hand() // don't use the keyword virtual outside of class definition { Clear(); }
void Hand::Clear() { // iterate through vector, freeing all memory on the heap vector<Card*>::iterator iter = m_Cards.begin(); for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter) { delete *iter; *iter = 0; } // clear vector of pointers m_Cards.clear(); }
int Hand::GetTotal() const { // if no cards in hand, return 0 if (m_Cards.empty()) return 0;
// if a first card has value of 0, then card is face down; return 0 if (m_Cards[0]->GetValue() == 0) return 0;
// add up card values, treat each ace as 1 int total = 0; vector<Card*>::const_iterator iter; for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter) total += (*iter)->GetValue();
// determine if hand contains an ace bool containsAce = false; for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter) if ((*iter)->GetValue() == Card::ACE) containsAce = true;
// if hand contains ace and total is low enough, treat ace as 11 if (containsAce && total <= 11) // add only 10 since we've already added 1 for the ace total += 10;
return total; }
class GenericPlayer : public Hand { friend ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer);
public: GenericPlayer(const string& name = "");
virtual ~GenericPlayer();
// indicates whether or not generic player wants to keep hitting virtual bool IsHitting() const = 0;
// returns whether generic player has busted - has a total greater than 21 bool IsBusted() const;
// announced that the generic player busts void Bust() const;
void House::FlipFirstCard() { if (!(m_Cards.empty())) m_Cards[0]->Flip(); else cout << "No card to flip!\n"; }
class Deck : public Hand { public: Deck();
virtual ~Deck();
// create a standard deck of 52 cards void Populate();
// shuffle cards void Shuffle();
// deal one card to a hand void Deal(Hand& aHand);
// give additional cards to a generic player void AdditionalCards(GenericPlayer& aGenericPlayer); };
Deck::Deck() { m_Cards.reserve(52); Populate(); }
Deck::~Deck() {}
void Deck::Populate() { Clear(); // create standard deck for (int s = Card::CLUBS; s <= Card::SPADES; ++s) for (int r = Card::ACE; r <= Card::KING; ++r) Add(new Card(static_cast<Card::rank>(r), static_cast<Card::suit>(s))); }
void Deck::Deal(Hand& aHand) { if (!m_Cards.empty()) { aHand.Add(m_Cards.back()); m_Cards.pop_back(); } else { cout << "Out of cards. Unable to deal."; } }
void Deck::AdditionalCards(GenericPlayer& aGenericPlayer) { cout << endl; // continue to deal a card as long as generic player isn't busted and // wants another hit while (!(aGenericPlayer.IsBusted()) && aGenericPlayer.IsHitting()) { Deal(aGenericPlayer); cout << aGenericPlayer << endl;
if (aGenericPlayer.IsBusted()) aGenericPlayer.Bust(); } }
class Game { public: Game(const vector<string>& names);
~Game();
// plays the game of blackjack void Play();
private: Deck m_Deck; House m_House; vector<Player> m_Players; };
Game::Game(const vector<string>& names) { // create a vector of players from a vector of names vector<string>::const_iterator pName; for (pName = names.begin(); pName != names.end(); ++pName) m_Players.push_back(Player(*pName));
srand(time(0)); // seed the random number generator m_Deck.Populate(); m_Deck.Shuffle(); }
Game::~Game() {}
void Game::Play() { // deal initial 2 cards to everyone vector<Player>::iterator pPlayer; for (int i = 0; i < 2; ++i) { for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) m_Deck.Deal(*pPlayer); m_Deck.Deal(m_House); }
// hide fouse's first card m_House.FlipFirstCard();
// deal additional cards to house m_Deck.AdditionalCards(m_House);
if (m_House.IsBusted()) { // everyone still playing wins for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) if (!(pPlayer->IsBusted())) pPlayer->Win(); } else { // compare each player still paying to house for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) if (!(pPlayer->IsBusted())) { if (pPlayer->GetTotal() > m_House.GetTotal()) pPlayer->Win(); else if (pPlayer->GetTotal() < m_House.GetTotal()) pPlayer->Lose(); else pPlayer->Push(); } }
int main() { cout << "\t\tWelcome to Blackjack!\n\n";
int numPlayers = 0; while (numPlayers < 1 && numPlayers > 7) { cout << "How many players? (1 - 7): "; cin >> numPlayers; }
vector<string> names; string name; for (int i = 0; i < numPlayers; ++i) { cout << "Enter player name: "; cin >> name; names.push_back(name); } cout << endl;
// the game loop Game aGame(names); char again = 'y'; while (again != 'n' && again != 'N') { aGame.Play(); cout << "\nDo you want to play again? (Y/N): "; cin >> again; }
system("pause"); return (0); }
// overloads << operator so Card object can be sent to cout ostream& operator<<(ostream& os, const Card& aCard) { const string RANKS[] = {"0", "a", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; const string SUITS[] = {"c", "d", "h", "s"};
if (aCard.m_IsFaceUp) os << RANKS[aCard.m_Rank] << SUITS[aCard.m_Suit]; else os << "XX";
return os; }
// overloads << operator so GenericPlayer object can be sent to cout ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer) { os << aGenericPlayer.m_Name << ":\t";
vector<Card*>::const_iterator pCard; if (!aGenericPlayer.m_Cards.empty()) { for (pCard = aGenericPlayer.m_Cards.begin(); pCard != aGenericPlayer.m_Cards.end(); ++pCard) os << *(*pCard) << "\t";
jeg har afinstalleret visual studio 2008 og installeret vs6 - fandt informationer på nettet om hvordan jeg kunne installere vs6, så koden på vs6 giver disse fejl:
--------------------Configuration: Blackjack - Win32 Debug-------------------- Compiling... Blackjack.cpp c:\windows\system32\blackjack.cpp(353) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,s td::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information c:\windows\system32\blackjack.cpp(353) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::ch ar_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information c:\windows\system32\blackjack.cpp(461) : error C2248: 'm_IsFaceUp' : cannot access private member declared in class 'Card' c:\windows\system32\blackjack.cpp(33) : see declaration of 'm_IsFaceUp' c:\windows\system32\blackjack.cpp(462) : error C2248: 'm_Rank' : cannot access private member declared in class 'Card' c:\windows\system32\blackjack.cpp(31) : see declaration of 'm_Rank' c:\windows\system32\blackjack.cpp(462) : error C2248: 'm_Suit' : cannot access private member declared in class 'Card' c:\windows\system32\blackjack.cpp(32) : see declaration of 'm_Suit' c:\windows\system32\blackjack.cpp(472) : error C2248: 'm_Name' : cannot access protected member declared in class 'GenericPlayer' c:\windows\system32\blackjack.cpp(156) : see declaration of 'm_Name' c:\windows\system32\blackjack.cpp(475) : error C2248: 'm_Cards' : cannot access protected member declared in class 'Hand' c:\windows\system32\blackjack.cpp(76) : see declaration of 'm_Cards' c:\windows\system32\blackjack.cpp(477) : error C2248: 'm_Cards' : cannot access protected member declared in class 'Hand' c:\windows\system32\blackjack.cpp(76) : see declaration of 'm_Cards' c:\windows\system32\blackjack.cpp(477) : error C2248: 'm_Cards' : cannot access protected member declared in class 'Hand' c:\windows\system32\blackjack.cpp(76) : see declaration of 'm_Cards' c:\windows\system32\blackjack.cpp(478) : error C2593: 'operator <<' is ambiguous Error executing cl.exe.
det skal nok lige nævnes at jeg havde lidt travlt da jeg installerede vs6, så jeg fik ikke installeret SP6.. det får jeg så forhåbentligt tid til at gøre senere i dag, når jeg har fri.. ved ikke om det er relevant eller ej, tænkte bare hvis der var vigtige ændringer i syntax o.lign..
det var så fordi jeg ikke brugte "Kør som administrator".. men nu får jeg et problem når jeg spiller .. jeg kan ikke gøre noget.. uanset hvad jeg gør, så kommer computeren til at spille alene hele tiden..
ah lol.. jeg har byttet om på operatørerne.. det skal så selvfølgelig være
while (numPlayers >= 1 && numPlayers <= 7)
eller... mener jeg da?
eller måske (numPlayers > 0 && numPlayers < 8) ?
ville det ikke være korrekt for den øverste kommentar?
// Blackjack.cpp : Defines the entry point for the console application. // Description: Plays a simple version of the casino game of blackjack; for 1 - 7 players.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.