Weird, cards not outputting correctly

This is a discussion on Weird, cards not outputting correctly within the C++ Programming forums, part of the General Programming Boards category; Hey I've been making a blackjack game, I'm doing pretty well but I've run into a problem that I think ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Weird, cards not outputting correctly

    Hey I've been making a blackjack game, I'm doing pretty well but I've run into a problem that I think may stem from how I'm passing function variables around, value not reference? or vice versa? I'll post alot of code: and a few sample outputs:

    IO Class: Handles input and output for the game
    Code:
    #ifndef IO_H
    #define IO_H
    #include <iostream>
    #include <string>
    #include <map>
    class IO
    {
    public:
    	typedef	std::map<std::string, std::string> output;
    
    	void update_output(std::string id, std::string & update)
    	{
    		output::iterator index = coutput.find(id);
    		if (index == coutput.end())
    		{
    			coutput.insert(std::make_pair(id, update));
    		}
    		else
    		{
    			(*index).second = update;
    		}
    	}
    
    	void display_output()
    	{
    		for( output::iterator index = coutput.begin(); index != coutput.end(); ++index)
    		{
    			cout << (*index).second << std::endl;
    		}
    	}
    	int get_input()
    	{
    		std::cin >> input;
    		return input;
    	}
    	void clear_screen()
    	{
    		system("cls");
    	}
    	output coutput;
    	int input;
    	
    };
    #endif
    Game class: this is where the main game loop runs and ties alot of the code together and makes it work.
    Code:
    #ifndef GAME_H
    #define GAME_H
    #include "Dealer.h"
    #include <vector>
    #include <iostream>
    #include "IO.H"
    #include "Player.h"
    class Game
    {
    public:
    
    	Game()
    	{
    		dealer = new Dealer;
    		deck = new Deck;
    		io = new IO;
    		init_players(1);
    	}
    
    	void play_game()
    	{
    		dealer->shuffle_deck((*deck));
    		dealer->draw((*deck), 2);
    		players[0]->draw((*deck), 1);
    		int gameloop = 1;
    		while(gameloop != 0)
    		{ 
    			io->update_output("dealer_cards", dealer->hand->read_cards());
    			io->update_output("player_cards", players[0]->hand->read_cards());
    			io->display_output();
    			gameloop = blackjack(io->get_input());
    			io->clear_screen();
    		}	  
    	}
    
    private:	
    
    	 int blackjack(int result)
    	 {
    		 if (result == 1)
    		 {
    			 players[0]->draw((*deck), 1);
    			 if (get_hand_value(players[0]->hand->hand) > 21)
    			 {
    				return 0;
    			 }
    			 else { return 1; }
    		 }
    		 else if (result == 2)
    		 {
     			 if(get_hand_value(players[0]->hand->hand ) > get_hand_value(dealer->hand->hand))  
    			 {
    				 return 0;
    			 }
    			 else { return 0; }
    		 }
    		 else if (result == 3)
    		 {	
    			 return 0;
    		 }
    		 return 0;
    	 }
    
    
    	 int get_hand_value(vector<Card> hand)
    	 {
    		 sum = 0;
    		 for (unsigned int loop = 0; loop < hand.size(); loop++)
    		 {
    			 sum += hand[loop].read_numeric_data();
    		 }
    		 return sum;
    	 }
    
    	 void init_players(unsigned int number)
    	 {
    		 for (unsigned int loop = 0; loop < number; loop++)
    		 {
    			 Player * player = new Player;
    			 //player id will increment with the number of players
    			 player->playerid = loop + 1;		
    			 players.push_back(player);
    		 }
    
    	 }
    	 int sum;
    	 Dealer * dealer;
    	 Deck * deck;
    	 IO * io;
    	 vector<Player*> players;
    
    };
    #endif
    This is how I output card strings, the hand class, the player class, and the deck class.
    Code:
    #ifndef PLAYER_H
    #define PLAYER_H
    #include "Data.h"
    #include "Deck.h"
    #include "IO.H"
    class Hand
    {
    public:
    	Hand()
    	{
    	}
    
    	string & read_cards()
    	{
    		show_hand.erase();
    		for(unsigned int loop = 0; loop < hand.size(); loop++)
    		{
    			show_hand.append(hand[loop].read_string_data());
    			show_hand.append("	");
    		}
    		show_hand.append("\n");
    		return show_hand;
    	}
    
    	vector<Card> hand;	
    private: // lookups
    	friend class Dealer;
    	friend class Game;
    
    	string show_hand;
    
    };
    
    class Player
    {
    public:
    	Player()
    	{
    		hand = new Hand;
    	}
    
    	void draw(Deck & deck, int number)
    	{
    		for(int loop = 0; loop < number; loop++)
    		{
    			hand->hand.push_back(deck.cards.back());
    			deck.cards.pop_back();
    		}
    	}
    
    	int playerid;
    	Hand * hand;	
    };
    
    
    #endif
    And the deck/cards
    Code:
    // This header file declares the deck class and all of its functions,
    // as well as the card structure.
    #ifndef DECK_H
    #define DECK_H
    #include <vector>
    #include <stdio.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <cassert>
    #include "Data.h"
    
    using namespace std;
    
    class Card : public Data
    {
    public:
    	Card(int suit, int value)
    	{
    		initialize();
    		text_data.erase();
    		text_data.append(values[value]);
    		text_data.append(" of ");
    		text_data.append(suits[suit]);
    		numeric_data = points[value];
    	}
    
    private:
    
    	void initialize()
    	{
    		add_to_string_lookup(suits, "Clubs");
    		add_to_string_lookup(suits, "Diamonds");
    		add_to_string_lookup(suits, "Hearts");
    		add_to_string_lookup(suits, "Spades");
    		add_to_string_lookup(values, "Two");
    		add_to_string_lookup(values, "Three");
    		add_to_string_lookup(values, "Four");
    		add_to_string_lookup(values, "Five");
    		add_to_string_lookup(values, "Six");
    		add_to_string_lookup(values, "Seven");
    		add_to_string_lookup(values, "Eight");
    		add_to_string_lookup(values, "Nine");
    		add_to_string_lookup(values, "Ten");
    		add_to_string_lookup(values, "Jack");
    		add_to_string_lookup(values, "Queen");
    		add_to_string_lookup(values, "King");
    		add_to_string_lookup(values, "Ace");
    		add_to_int_lookup(points, 2);
    		add_to_int_lookup(points, 3);
    		add_to_int_lookup(points, 4);
    		add_to_int_lookup(points, 5);
    		add_to_int_lookup(points, 6);
    		add_to_int_lookup(points, 7);
    		add_to_int_lookup(points, 8);
    		add_to_int_lookup(points, 9);
    		add_to_int_lookup(points, 10);
    		add_to_int_lookup(points, 10);
    		add_to_int_lookup(points, 10);
    		add_to_int_lookup(points, 10);
    		add_to_int_lookup(points, 11);
    
    	}	
    
    	string_lookup suits;
    	string_lookup values;
    	int_lookup points;
    	int Suit;
    	int Value;
    };
    
    class Deck
    {
    public:	
    
    	Deck()
    	{
    		unsigned int x = 0;
    		unsigned int y = 0;
    		unsigned int z = 0;
    			for (y = 0; y < 4;  y++)
    			{
    				
    				for (x = 0; x < 13; x++)
    				{
    					Card c(y,x);
    					cards.push_back(c);
    				}
    			}
    			assert(cards.size() == 52);
    	}
    	vector<Card> cards;
    };
    #endif
    Here is the dealer class, i might remove it since it's only unique member is shuffle_deck..
    Code:
    #ifndef DEALER_H
    #define DEALER_H
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include "Deck.h"
    #include "Player.h"
    
    class Dealer
    {
    public:
    
    	Dealer()
    	{
    		hand = new Hand;
     		playerid = 0;
    	}
    	void shuffle_deck(Deck & deck)
    	{
    		random_shuffle(deck.cards.begin(), deck.cards.end());
    	} 
    
    	void draw(Deck & deck, int number)
    	{
    		for(int loop = 0; loop < number; loop++)
    		{
    			hand->hand.push_back(deck.cards.back());
    			deck.cards.pop_back();
    		}
    	}
    
    	Hand * hand;
    	int playerid;
    	friend class Game;
    };
    #endif
    Here is some sample output: The dealer's card's are the first line of cards, and the players is the second, I can hit 1, 2, or 3, 1 draws a card and checks to see if I'm over 21, 2 compares my cards with the dealers and declares a winner, and 3 folds my cards.
    Code:
    Nine of Clubs   Two of Diamonds
    
    Eight of Clubs
    Notice how there are two clubs, I've noticed that there is almost always two of something... In this next bit of output I'll draw a few cards, look what happens:
    Code:
    Nine of Hearts  Ten of Diamonds
    
    Three of Spades Three of Clubs  Three of Diamonds
    OMG three three's?
    some more output..
    Code:
    Jack of Clubs   Six of Clubs
    
    Eight of Hearts Two of Spades   King of Clubs
    Maybe this is normal? I dunno..
    Anyways, can anyone help me clean up my code a little bit and get this random shuffle thing to work right?
    Last edited by Shamino; 11-30-2007 at 09:41 AM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't really see anything wrong here. You'll see clubs next to each other in a real shuffled deck, or threes next to each other, or whatever, so I don't think there's anything wrong there. If you want to check whether your deck is really shuffled, you could print out the entire deck from top to bottom.

    Other things that maybe you aren't worried about yet, but probably will be soon: while dealer->hand->hand is syntactically right, it looks oh so very wrong. ( You may want to distinguish the two, if it's not already too late. Since the second hand (inside Hand) is a vector of cards, maybe cardlist or something?) Also, your get_hand_value will need to count Aces as one or eleven in context.
    Last edited by tabstop; 11-30-2007 at 10:16 AM. Reason: put sentences in proper the order

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    maybe they are shuffling right, and im just being paranoid.. I guess if I deal 3 cards there is a significant chance that there will be two of the same suit? like 25% for each card I suppose.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Let's see . . . there are three cards, each of which is one of four suits, making 4^3 = 64 possible combinations. Four arrangements (XXO, XOX, OXX, XXX) are favourable times four suits makes 16/64 or a 25&#37; chance that you'll get two cards of the same suit with three cards dealt.

    Did I miss anything?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Your shuffle code looks fine to me, and your sample output looks normal, too.

    Why are dealer, deck and io pointers? I would make them objects. Right now you never clean up that memory you allocate with new.

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    i think i switched it to pointers because i was going to eventually remove io and deck and players and the dealer class from the game class and keep them all isolated except via pointer.

    I just changed it back. I think maybe I thought I needed pointers to pass stuff by reference so i changed it.
    Last edited by Shamino; 11-30-2007 at 02:32 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 02:22 PM
  2. Help!For poker game simulation
    By tx1988 in forum C++ Programming
    Replies: 24
    Last Post: 05-25-2007, 09:59 PM
  3. dealing cards
    By braddy in forum C Programming
    Replies: 15
    Last Post: 03-29-2006, 02:46 AM
  4. RANT: What the hell is the point of gift cards?
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 42
    Last Post: 12-03-2004, 06:43 AM
  5. Cribbage Game
    By PJYelton in forum Game Programming
    Replies: 14
    Last Post: 04-07-2003, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21