Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am a little new/rusty to this C++ stuff. Is this bad code? Ways to improve it?

    #include <windows.h>
    #include <string>
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    bool _initialized_time = false;
    void Wait() {
      std::cout << "Prease any key to continue ..." << std::endl;
      std::cin.get();
    }
    void print(char* _value) {
      std::cout << _value << std::endl;
    }
    char ConvertIntToChar(int _int) {
      return (char)(((int)'0')+_int);
    }
    int ReturnRandomNumber(int _start, int _end) {
      if(_initialized_time == false) {
        _initialized_time = true;
        srand(time(0));
      }
      int n = rand() % _end + _start;
      return n;
    }
    void ClearConsoleWindow() {
      for (int i = 0; i < 40; i++) {
        std::cout << "                                                                                          ";
      }
    }
    void Wait(int _mil){
        Sleep(_mil); // wait 1 second
    }
    enum eCardType { // THE TYPES OF CARDS
      cNothing = 0,
      cAce = 1,
      cTwo = 2,
      cThree = 3,
      cFour = 4,
      cFive = 5,
      cSix = 6,
      cSeven = 7,
      cEight = 8,
      cNine = 9,
      cTen = 10, 
      cJack = 11,
      cKing = 12,
      cQueen = 13,
      cJoker = 14,
    cAceSmall = 15
    };
    enum eCardSuite { // THE SUITE OF THE CARDS
      sNothing = 0,
      sSpades = 1, 
      sHearts = 2, 
      sDiamonds = 3,
    sClubs = 4
    };
    enum ePlayerType { // PLAYER TYPE COMPUTER OR PLAYER
      pPlayer = 1,
      pComputer = 2,
    };
    struct card { // CARD STRUCT CONTAINS THE SUITE AND TYPE
      enum eCardSuite cSuite;
      enum eCardType cType;
    };
    card _deck[52]; // THE DECK WHICH CONTAINS THE CARDS
    const int _deck_size = 52; // THE MAXIMUM SIZE OF A DECK OF CARDS
    card _player_cards[_deck_size]; // THE PLAYERS CARDS
    card _computer_cards[_deck_size]; // THE COMPUTERS CARDS
    bool _game_over = false; // IS THE GAME OVER OR NOT
    bool _hand_delt = false; // HAS THE HAND BEEN DELT
    char _next_move = ' '; // THE NEXT MOVE
    const int _computer_hit_threshold = 17; // IF THE VALUE IS OVER THE MAXIMUM, OR EQUAL TO IT, IT WILL STAY
    bool _player_stays = false; // DOES THE PLAYER STAY
    bool _computer_stays = false; // DOES THE COMPUTER STAY
    double _default_money = 2000.00; // DEFAULT MONEY AMOUNT
    double _default_bet = 100.00;
    double _player_money = 0.0; // AMOUNT PLAYER HAS
    double _computer_money = 0.0; // AMOUNT COMPUTER HAS
    bool _first_hit = true;
    double _pot = 0.0;
    double _temp_money = 0.0;
    bool _deck_empty = false;
    void ResetPlayerCards(ePlayerType _type) { // RESET PLAYER CARDS
      switch(_type) {
      case pPlayer:
        for(int i = 0; i <= _deck_size - 1; i++) {
          _player_cards[i].cSuite = sNothing;
          _player_cards[i].cType = cNothing;
        }
        break;
      case pComputer:
        for(int i = 0; i <= _deck_size - 1; i++) {
          _computer_cards[i].cSuite = sNothing;
          _computer_cards[i].cType = cNothing;
        }
        break;
      }
    }
    char* ReturnSuiteString(eCardSuite _suite) {
      char* _result;
      switch(_suite) {
        case sNothing:
          _result = "";
          break;
        case sSpades:
          _result = "Spades";
          break;
        case sHearts:
          _result = "Hearts";
          break;
        case sDiamonds:
          _result = "diamonds";
          break;
        case sClubs:
          _result = "clubs";
          break;
      }
      return _result;
    }
    int ReturnCardValue(card _card) {
      int _result;
      switch(_card.cType) {
        case cAceSmall:
          _result = 1;
          break;
        case cAce:
          _result = 10;
          break;
        case cTwo:
          _result = 2;
          break;
        case cThree:
          _result = 3;
          break;
        case cFour:
          _result = 4;
          break;
        case cFive:
          _result = 5;
          break;
        case cSix:
          _result = 6;
          break;
        case cSeven:
          _result = 7;
          break;
        case cEight:
          _result = 8;
          break;
        case cNine:
          _result = 9;
          break;
        case cTen:
          _result = 10;
          break;
        case cJack:
          _result = 10;
          break;
        case cQueen:
          _result = 10;
          break;
        case cKing:
          _result = 10;
          break;
        case cJoker:
          _result = 0;
          break;
      }
      return _result;
    }
    bool IsAce(card _card) {
      bool _result = false;
      if(_card.cType == cAce || _card.cType == cAceSmall) {
        _result = true;
      }
      return _result;
    }
    bool IsValidCard(card _card) {
      bool b = false;
      if(_card.cSuite != sNothing && _card.cType != cNothing && _card.cType != cJoker) {
        b = true;
      }
      return b;
    }
    int PlayerHandValue(ePlayerType _type) {
      int _value = 0;
      switch(_type) {
        case pComputer:
          for(int i = 0; i <= _deck_size - 1; i++) {
            if(_computer_cards[i].cSuite != sNothing && _computer_cards[i].cType != cNothing && _computer_cards[i].cType != cJoker) { 
              _value = _value + ReturnCardValue(_computer_cards[i]);
            }
          }
          break;
        case pPlayer:
          for(int i = 0; i <= _deck_size - 1; i++) {
            if(_player_cards[i].cSuite != sNothing && _player_cards[i].cType != cNothing && _player_cards[i].cType != cJoker) { 
              _value = _value + ReturnCardValue(_player_cards[i]);
            }
          }
          break;
      }
      return _value;
    }
    int PlayerCardCount(ePlayerType _type) {
      int _add = 1;
      int _count = 0;
      switch(_type) {
        case pComputer:
          for(int i = 0; i <= _deck_size - 1; i++) {
            if(_computer_cards[i].cSuite != sNothing && _computer_cards[i].cType != cNothing && _computer_cards[i].cType != cJoker) { 
              _count = _count + _add; 
            }
          }
          break;
        case pPlayer:
          for(int i = 0; i <= _deck_size - 1; i++) {
            if(_player_cards[i].cSuite != sNothing && _player_cards[i].cType != cNothing && _player_cards[i].cType != cJoker) { 
              _count = _count + _add;
            }
          }
          break;
      }
      return _count;
    }
    char* ReturnPlayerString(ePlayerType _type) {
      char* _result;
      switch(_type) {
        case pPlayer:
          _result = "Player";
          break;
        case pComputer:
          _result = "Computer";
          break;
        }
      return _result;
    }
    char* ReturnCardTypeString(eCardType _type) {
      char* _result;
      switch(_type) {
        case cNothing:
          _result = "";
          break;
        case cAceSmall:
          _result = "Ace";
          break;
        case cAce:
          _result = "Ace";
          break;
        case cTwo:
          _result = "Two";
          break;
        case cThree:
          _result = "Three";
          break;
        case cFour:
          _result = "Four";
          break;
        case cFive:
          _result = "Five";
          break;
        case cSix:
          _result = "Six";
          break;
        case cSeven:
          _result = "Seven";
          break;
        case cEight:
          _result = "Eight";
          break;
        case cNine:
          _result = "Nine";
          break;
        case cTen:
          _result = "Ten";
          break;
        case cJack:
          _result = "Jack";
          break;
        case cQueen:
          _result = "Queen";
          break;
        case cKing:
          _result = "King";
          break;
        case cJoker:
          _result = "Joker";
          break;
      }
      return _result;
    }
    bool DoesMoreCardsExistInDeck() {
      bool _more_exists = false;
      for(int i = 0; i <= _deck_size - 1; i++) {
        if(IsValidCard(_deck[i])) {
          _more_exists = true;
          break;
        }
      }
      return _more_exists;
    }
    card TakeNextCard() {
      card _new_card;
      for(int i = 0; i <= _deck_size - 1; i++) {
        if(IsValidCard(_deck[i])) {
          _new_card = _deck[i];
          _deck[i].cSuite = sNothing;
          _deck[i].cType = cNothing;
          break;
        }
      }
      return _new_card;
    }
    card GiveCard(ePlayerType _player_type, bool _show_computer = false) {
      bool _say = false;
      int _count = 0;
      card _new_card;
      _new_card.cSuite = sNothing;
      _new_card.cType = cNothing;
      if(DoesMoreCardsExistInDeck() == true) {
        _new_card =  TakeNextCard();
        switch(_player_type) {
          case pComputer:
            _count = PlayerCardCount(pComputer);
            _computer_cards[_count] = _new_card;
            if(_show_computer == true) {
              _say = true;
            }
            break;
          case pPlayer:
            _count = PlayerCardCount(pPlayer);
            _player_cards[_count] = _new_card;
            _say = true;
            break;
        }
        if(_say == true) {
          std::cout << ReturnPlayerString(_player_type) << " received " << ReturnCardTypeString(_new_card.cType) << " of " << ReturnSuiteString(_new_card.cSuite) << std::endl;
        } else {
          std::cout << ReturnPlayerString(_player_type) << " received an undisclosed card." << std::endl;
        }
      } else {
        print("Deck Empty");
        _deck_empty = true;
      }
      return _new_card;
    }
    bool IsCardAlreadyInDeck(card _card) {
      bool _result = false;
      for(int i = 0; i <= _deck_size - 1; i++) {
        if(IsValidCard(_deck[i])) {
          if(_card.cSuite == _deck[i].cSuite && _card.cType == _deck[i].cType) {
            _result = true;
          }
        }
      }
      return _result;
    }
    int CanUseCard(card _cards[], card _card) {
      bool _can_use = true;
      int _temp_card_count = 0;
      int _count;
      for(int i = 0; i <= _deck_size - 1; i++) {
        if(_card.cSuite != sNothing && _card.cType != cNothing && _card.cType != cJoker) {
          if((_cards[i].cSuite == _card.cSuite) && (_cards[i].cType == _card.cType)) {
            _temp_card_count++;
          }
        }
      }
      if(_card.cType == cAceSmall) { _can_use = false; }
      if(IsCardAlreadyInDeck(_card) == true) { _can_use = false; }
      if(_card.cSuite == sNothing || _card.cType == cNothing || _card.cType == cJoker) { _can_use = false; }
      if(_temp_card_count == 4) { _can_use = false; }
      return _can_use;
    }
    void ShuffleDeck() {
      int i = 0;
      _deck_empty = false;
      for(int i = 0; i <= _deck_size - 1; i++) {
        _deck[i].cSuite = sNothing;
        _deck[i].cType = cNothing;
      }
      int _count = 0;
      for(int i = 0; i <= _deck_size - 1; i++) {
        card _new_card = {sNothing, cNothing};
        int _continue = 1;
        while(_continue == 1) {
          _new_card.cSuite = (eCardSuite)ReturnRandomNumber(1, 4);
          _new_card.cType = (eCardType)ReturnRandomNumber(1, 13);
          if(CanUseCard(_deck, _new_card) == true) {
            _continue = 0;
            _deck[_count] = _new_card;
            _count = _count + 1;
          }
        }
      }
    }
    void Reset() {
      _first_hit = true;
      ClearConsoleWindow();
      ResetPlayerCards(pPlayer);
      ResetPlayerCards(pComputer);
      _computer_stays = false;
      _player_stays = false;
      _hand_delt = true;
      GiveCard(pComputer, true);
      GiveCard(pPlayer, true);
      GiveCard(pComputer, true);
      GiveCard(pPlayer, true);
    }
    void PromptNewGame() {
      std::cin >> _next_move;
      switch(_next_move) {
        case 'p':
          Reset();
          break;
        case 'q':
          _game_over = true;
          break;
      }
    }
    void UpdateLastCard(ePlayerType _type, card _card) {
      int _count = 0;
      _count = PlayerCardCount(_type) - 1;
      if(_type == pPlayer) {
        _player_cards[_count] = _card;
      } else if (_type == pComputer) {
        _computer_cards[_count] = _card;
      }
    }
    void RewardWinner() {
      if(PlayerHandValue(pPlayer) > PlayerHandValue(pComputer)) {
        if(PlayerHandValue(pPlayer) >= 22) {
          std::cout << "Computer Wins! Player: " << PlayerHandValue(pPlayer) << ", Computer: " << PlayerHandValue(pComputer) << std::endl;
          _computer_money = _computer_money - _default_bet + _pot;
          _player_money = _player_money - _default_bet;
          _pot = 0.0;
        } else {
          std::cout << "Player Wins! Player: " << PlayerHandValue(pPlayer) << ", Computer: " << PlayerHandValue(pComputer) << std::endl;
          _player_money = _player_money + _default_bet + _pot;
          _computer_money = _computer_money - _default_bet;
          _pot = 0.0;
        }
      } else if (PlayerHandValue(pPlayer) == PlayerHandValue(pComputer)) {
        std::cout << "Draw! Player: " << PlayerHandValue(pPlayer) << ", Computer: " << PlayerHandValue(pComputer) << std::endl;
        _player_money = _player_money - _default_bet;
        _computer_money = _computer_money - _default_bet;
        _pot = _pot + _default_bet;
      } else if(PlayerHandValue(pComputer) > PlayerHandValue(pPlayer)) {
        if(PlayerHandValue(pComputer) >= 22) {
          std::cout << "Player Wins! Player: " << PlayerHandValue(pPlayer) << ", Computer: " << PlayerHandValue(pComputer) << std::endl;
          _player_money = _player_money + _default_bet + _pot;
          _computer_money = _computer_money - _default_bet;
          _pot = 0.0;
        } else {
          std::cout << "Computer Wins! Player: " << PlayerHandValue(pPlayer) << ", Computer: " << PlayerHandValue(pComputer) << std::endl;
          _computer_money = _computer_money + _default_bet + _pot;
          _player_money = _player_money - _default_bet;
          _pot = 0.0;
        }
      }
    }
    void Initialize() {
      ShuffleDeck();
      _computer_money = _default_money;
      _player_money = _default_money;
      card _temp_card;
      do {
        if(_player_money == 0) {
          print("sorry, you are out of money [q]");
          std::cin >> _next_move;
          if(_next_move == 'q') {
            _game_over = true;
          }
        } else if(_default_bet > _player_money) {
          std::cout << "You only have " << _player_money << " but the bet is currently " << _default_bet << " [q]" << std::endl;
          std::cin >> _next_move;
          if(_next_move == 'q') {
            _game_over = true;
          }
        } else {
          if(_player_stays == true && _computer_stays == true) {
            RewardWinner();
            if(_player_money == 0) {
              std::cout << "You are out of money [q] Quit" << std::endl;
              std::cin >> _next_move;
              if(_next_move == 'q') {
                _game_over = true;
              }
            } else if(_default_bet > _player_money) {
              std::cout << "You only have " << _player_money << " but the bet is currently " << _default_bet << " [q]" << std::endl;
              std::cin >> _next_move;
              if(_next_move == 'q') {
                _game_over = true;
              }
            } else {
              std::cout << "[p] Play Again [q] Quit" << std::endl;
              PromptNewGame();
            }
          } else {
            if(PlayerHandValue(pPlayer) >= 22) {
              RewardWinner();
              if(_player_money == 0) {
                std::cout << "Player Busted with " << PlayerHandValue(pPlayer) << "! You are out of money [q] Quit" << std::endl;
                std::cin >> _next_move;
                if(_next_move == 'q') {
                  _game_over = true;
                }
              } else if(_default_bet > _player_money) {
                std::cout << "You only have " << _player_money << " but the bet is currently " << _default_bet << " [q]" << std::endl;
                std::cin >> _next_move;
                if(_next_move == 'q') {
                  _game_over = true;
                }
              } else {
                std::cout << "Player Busted with " << PlayerHandValue(pPlayer) << "! [p] Play Again [q] Quit" << std::endl;
                PromptNewGame();
              }
            } else {
              if(_hand_delt == false) { Reset(); }
              if(_first_hit == true) {
                std::cout << "Cards: " << PlayerCardCount(pPlayer) << ", Hand Value: " << PlayerHandValue(pPlayer) << ", Money: " << _player_money << "." << std::endl;
                if(_deck_empty == true) {
                  std::cout << "[d] Shuffle Deck [s] Stay [c] Change bet amount [q] Quit" << std::endl;
                } else {
                  std::cout << "[h] Hit Me [s] Stay [c] Change bet amount [q] Quit" << std::endl;
                }
                _first_hit = false;
              } else {
                if(_deck_empty == true) {
                  std::cout << "Cards: " << PlayerCardCount(pPlayer) << ", Hand Value: " << PlayerHandValue(pPlayer) << ", Money: " << _player_money << ". [d] Shuffle Deck [s] Stay [q] Quit" << std::endl;
                } else {
                  std::cout << "Cards: " << PlayerCardCount(pPlayer) << ", Hand Value: " << PlayerHandValue(pPlayer) << ", Money: " << _player_money << ". [h] Hit Me [s] Stay [d] Shuffle Deck [q] Quit" << std::endl;
                }
              }
              std::cin >> _next_move;
              switch(_next_move) {
                case 'd':
                  if(_deck_empty) {
                    ShuffleDeck();
                    break;
                  }
                case 'c':
                  if(_first_hit == true) {
                    if(_pot == 0) {
                      std::cout << "You currently have: " << _player_money << " the current bet is " << _default_bet << ". Enter the new default amount: " << std::endl;
                    } else {
                      std::cout << "You currently have: " << _player_money << " the current bet is " << _default_bet << ". There is " << _pot << " in the pot. Enter the new default amount: " << std::endl;
                    }
                    std::cin >> _temp_money;
                    if(_temp_money >= _default_bet) {
                      _default_bet = _temp_money;
                    } else {
                      std::cout << "You cannot bet that much, because you don't have enough money." << std::endl;
                    }
                  }
                  break;
                case 'q':
                  _game_over = true;
                  break;
                case 'h':
                  _temp_card = GiveCard(pPlayer);
                  if(_temp_card.cType == cAce) {
                    std::cout << "Select Ace [1] Ace (10 Points) [2] Ace (1 Points)";
                    std::cin >> _next_move;
                    switch(_next_move) {
                      case '1':
                        _temp_card.cType = cAce;
                        break;
                      case '2':
                        _temp_card.cType = cAceSmall;
                        break;
                    }
                    UpdateLastCard(pPlayer, _temp_card);
                  }
                  break;
                case 's':
                  _player_stays = true;
                  do {
                    _temp_card = GiveCard(pComputer, true);
                    if(_deck_empty == true) {
                      ShuffleDeck();
                    }
                    if(PlayerHandValue(pComputer) >= 22) {  
                      RewardWinner();
                      if(_player_money == 0) {
                        std::cout << "Computer Busted with " << PlayerHandValue(pComputer) << "! You are out of money [q] Quit" << std::endl;
                        std::cin >> _next_move;
                        if(_next_move == 'q') { _game_over = true; }
                      } else if(_default_bet > _player_money) {
                        std::cout << "Computer Busted with " << PlayerHandValue(pComputer) << " but the bet is currently " << _default_bet << " [q]" << std::endl;
                        std::cin >> _next_move;
                        if(_next_move == 'q') { _game_over = true; }
                      } else {
                        std::cout << "Computer Busted with " << PlayerHandValue(pComputer) << "! [p] Play Again [q] Quit" << std::endl;
                        PromptNewGame();
                      }
                      ResetPlayerCards(pComputer);
                      break;
                    }
                  } while (PlayerHandValue(pComputer) < PlayerHandValue(pPlayer));
                  _computer_stays = true;
                  break;
              }
            }
          }
        }
      } while (!_game_over);
    }
    int black_jack() {
      Initialize();
      Wait();
      return 0;
    }
share|improve this question
    
The best way: Post the code in the question and indent all the code by 4 spaces. There is a button that indents it for you "{}". –  Loki Astari Feb 25 '13 at 16:51
    
You are unlikely to get reviewed unless the code is posted inline here: 1) Because it makes your code open source. 2) We want our comments to be useful to more people than just you. To make the comments useful the code must be available with the question so the reader can understand the comments in context. By placing the code here we know that that the code will live as long as the comments and be formatted consistently. –  Loki Astari Feb 25 '13 at 16:54
    
Sorry I fixed it. I find this site to be fairly unintuetive. It would be nice if there's a paste box or something. –  guideX Feb 25 '13 at 17:46
    
Welcome, @guideX. This site is part of the Stack Exchange network. If you have any suggestions for improvement, please do go ahead and post them on the network's Meta site, where constructive criticism is welcome. Anything site-specific can be proposed on our own meta. –  codesparkle Feb 25 '13 at 19:30
    
Before using leading underscores in your identifiers. Read this: stackoverflow.com/questions/228783/… –  Loki Astari Feb 26 '13 at 4:52
add comment

2 Answers

Here are some suggestions for you:

bool _initialized_time = false;

int ReturnRandomNumber(int _start, int _end) {
  if(_initialized_time == false) {
    _initialized_time = true;
    srand(time(0));
  }
  int n = rand() % _end + _start;
  return n;
}

Avoid using global variables if at all possible. For this simple implementation of the game it probably does not matter, but unless you have a good reason to use them, it's just as well to get in the habit of avoiding them.

In this case you should just initialize the random number generator when the program starts:

int black_jack() {
  srand(time(0));  
  Initialize();
  ...

Also you should avoid using leading underscores in identifiers, see this answer for the reasoning. If needed you can use a trailing underscore instead, but don't add underscores for no reason.

About naming, drop the hungarian notation too.

Your Initializefunction does a lot more than initialize the game. It is also very big. You should consider breaking it up into several smaller functions, and give them names that describes what they do.

You do have a lot of good function with well chosen names already, like PromptNewGame, ShuffleDeck, GiveCard etc. Try to break the Initialize-function into similar chunks of well named functions.

The ReturnCardValue-function could probably be a lot smarter. Just handle the special cases, and return the card type for the rest. Something like this, perhaps:

int ReturnCardValue(const card & c) {
  if (c.type > Nine)
    return 10;
  else
    return reinterpret_cast<int>(c.type);
}

This may require the layout of your card type enum to be slightly different. Notica also that I chose to pass the card struct by reference instead of by value (copy). For a small struct like this it's not a big deal, but for bigger structs it's nice to avoid the copying if it's not needed.

Also, you don't have to initialize every member of the enum as long as the values are consecutive. Try this instead:

enum CardType {
  Joker, AceSmall, Two, Three, Four, Five,
  Six, Seven, Eight, Nine, Ten, Jack, King,
  Queen, Ace
};

Finally you may want to look into turning several parts of your program into classes. From the top of my head I can think of at least three immediately obvious ones, Game, Player, and Card. This would encapsulate the functionality in each element of the game into logical units and make the program easier to read and maintain at the same time.

Allright, that's just some suggestions for you. Gives you at least somewhere to start.

share|improve this answer
    
Thanks I learned a lot here, I have improved my code a lot as a result. –  guideX Feb 28 '13 at 20:10
add comment

Everything @harald said:

Is this correct?

int n = rand() % _end + _start;

// Given the names of the parameters start and end
// I would have thought it should be:
int n = rand() % (_end-start) + _start;

// This will give you a number in the range [start, end)

You can replace your big switch statements with a value look up:

 int ReturnCardValue(card _card) {
  int _result;
  switch(_card.cType) {
    case cAceSmall:
      _result = 1;
      break;
    case cAce:
      _result = 10;
      break;
    case cTwo:
      _result = 2;
      break;

 // easier to write:

 int ReturnCardValue(Card card)
 {
     static int cardValue[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 10, 1 /*ACE SMALL IS 15*/ };
     return cardValue[card.cType];
 }

Rather than assigning true false based on if statement. Use the conditional of the if statement directly:

bool IsValidCard(card _card) {
  bool b = false;
  if(_card.cSuite != sNothing && _card.cType != cNothing && _card.cType != cJoker) {
    b = true;
  }
  return b;
}

// easier to write and read as:
bool IsValidCard(card _card) {
  bool b = _card.cSuite != sNothing && _card.cType != cNothing && _card.cType != cJoker;

  return b;
}

Choice of algorithm based on a type are better done using encapsulation and polymorphism:

int PlayerHandValue(ePlayerType _type) {
  int _value = 0;
  switch(_type) {
    case pComputer:
      value = // Compute Value base on Computer
      break;
    case pPlayer:
      value = // Compute Value based on Player
      break;
  }
  return _value;
}

// If you used the concept of Players:
// The code looks like this:
int PlayerHandValue(Player& player)
{
  int _value = player.handValue();
  return _value;
}

// For this you need:
class Player
{
    public:
        virtual ~Player() {}
        virtual  int handValue() = 0;
};
class HumanPlyaer: public Player;
{
    public:
        virtual  int handValue()
        {
              // Code for Human here
        }
};
class ComputerPlyaer: public Player;
{
    public:
        virtual  int handValue()
        {
              // Code for Computer here
        }
};
// You can also add more styles of computer player without affecting
// the logic of your code.

Try and use the standard algorithms:

void ShuffleDeck()
{
     for(int i = 0; i <= _deck_size - 1; i++)
     {
         _deck[i].cSuite = static_cast<eCardSuite>(i / 13 + 1);
         _deck[i].cType  = static_cast<eCardType>(i % 13 + 1);
     }
     std::random_shuffle(&data[0], &data[_deck_size]);
}

Put repeated code into functions. That way if it is broken you only fix it in one place:

    } else {
      std::cout << "Player Wins! Player: " << PlayerHandValue(pPlayer) << ", Computer: " << PlayerHandValue(pComputer) << std::endl;
      _player_money = _player_money + _default_bet + _pot;
      _computer_money = _computer_money - _default_bet;
      _pot = 0.0;
    }

    // A couple of lines down you have:
   if(PlayerHandValue(pComputer) >= 22) {
      std::cout << "Player Wins! Player: " << PlayerHandValue(pPlayer) << ", Computer: " << PlayerHandValue(pComputer) << std::endl;
      _player_money = _player_money + _default_bet + _pot;
      _computer_money = _computer_money - _default_bet;
      _pot = 0.0;
    } else {

Looks about the same from here:

The Initialize function is getting a bit big.
You way want to split it into subroutines so you can read the overall plan of the function without having to scroll the screen. A rule of thumb is that a function should not be longer than a screen in length (you want to be able to read the whole function in one go with out loosing track of what happens at one end).

share|improve this answer
    
Thanks for the tips, I'm getting better all the time. Just looking at my code from a few days ago gives me the shivers. A lot of these things I already fixed, but the one I like the best is the "static int card_values_[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 10, 1 };" very elegant, I didn't think that it was possible. Also this doesn't work: //return rand() % (end_ - start_) + start_; i'm not sure why, the game doesn't load when I use it. –  guideX Feb 28 '13 at 20:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.