CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Threaded View

  1. #1
    Join Date
    Dec 2007
    Posts
    12

    Newb needs help with structs

    Okay first off I'm sure this probably is some horrible code, but I'm trying to get my feet wet in coding by just creating stuff, no matter how horrible it is.

    But anyway, here's the issue I'm having, I can't get player.cards[] to take on values from the cardIndex[]. I'm sure it's just something that I don't understand between the way classes and/or structs work, but I just can't figure it out. So if anyone could tell me what I'm doing wrong I would greatly appreciate it.

    EDIT: Oh, and I'm not getting compiling errors, my player.cards[] is just holding nothing but gibberish.

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <time.h>
    using namespace std;
    
    struct card{
    	char face;
    	int value;
    	string suit[10];
    };
    card cardIndex[53] = {
    	{'J', 0, ""},
    	{'A', 1, "Spades"},
    	{'2', 2, "Spades"},
    	{'3', 3, "Spades"},
    	{'4', 4, "Spades"},
    	{'5', 5, "Spades"},
    	{'6', 6, "Spades"},
    	{'7', 7, "Spades"},
    	{'8', 8, "Spades"},
    	{'9', 9, "Spades"},
    	{'J', 10, "Spades"},
    	{'Q', 10, "Spades"},
    	{'K', 10, "Spades"},
    	{'A', 1, "Hearts"},
    	{'2', 2, "Hearts"},
    	{'3', 3, "Hearts"},
    	{'4', 4, "Hearts"},
    	{'5', 5, "Hearts"},
    	{'6', 6, "Hearts"},
    	{'7', 7, "Hearts"},
    	{'8', 8, "Hearts"},
    	{'9', 9, "Hearts"},
    	{'J', 10, "Hearts"},
    	{'Q', 10, "Hearts"},
    	{'K', 10, "Hearts"},
    	{'A', 1, "Clubs"},
    	{'2', 2, "Clubs"},
    	{'3', 3, "Clubs"},
    	{'4', 4, "Clubs"},
    	{'5', 5, "Clubs"},
    	{'6', 6, "Clubs"},
    	{'7', 7, "Clubs"},
    	{'8', 8, "Clubs"},
    	{'9', 9, "Clubs"},
    	{'J', 10, "Clubs"},
    	{'Q', 10, "Clubs"},
    	{'K', 10, "Clubs"},
    	{'A', 1, "Diamonds"},
    	{'2', 2, "Diamonds"},
    	{'3', 3, "Diamonds"},
    	{'4', 4, "Diamonds"},
    	{'5', 5, "Diamonds"},
    	{'6', 6, "Diamonds"},
    	{'7', 7, "Diamonds"},
    	{'8', 8, "Diamonds"},
    	{'9', 9, "Diamonds"},
    	{'J', 10, "Diamonds"},
    	{'Q', 10, "Diamonds"},
    	{'K', 10, "Diamonds"}
    };
    
    
    class Hand{
    public:
    	Hand(int);
    	void givecard(int);
    	int getscore();
    	int getmoney();
    	void getcards();
    	void addmoney();
    private:
    	card cards[10];
    	int i;
    	int money;
    };
    
    Hand::Hand(int start)
    {
    	money = start;
    	i = 0;
    	return;
    }
    void Hand::givecard(int x)
    {
    	cards[i] = cardIndex[x];
    	i++;
    	return;
    }
    void Hand::getcards()
    {
    	int x;
    	for(x=0; x <= i; x++)
    	{
    		cout << "Player has a " << cards[x].face << "of " << cards[x].suit << endl;
    	}
    	return;
    }
    
    void deal(Hand, Hand);
    int pickcard();
    
    int main ()
    {
    	Hand player (500);
    	Hand dealer (0);
    	int bet;
    		deal(player, dealer);
    		player.getcards();
    		getchar();
    
      return 0;
    }
    
    void deal(Hand player, Hand dealer)
    {
    	int i;
    	int cardnumber;
    	for(i=0; i<2; i++)
    	{
    		cardnumber = pickcard();
    		player.givecard(cardnumber);
    		cardnumber = pickcard();
    		dealer.givecard(cardnumber);
    	}
    	return;
    }
    
    int pickcard()
    {	
    	int x;
    	srand((unsigned)time(NULL));
    	x = rand()&#37;52 +1;
    	return x;
    }
    Last edited by Daimones; August 6th, 2009 at 02:11 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured