Results 1 to 10 of 10

Thread: C++ halp     submit to reddit submit to twitter

  1. #1
    Yoshi P
    Join Date
    Aug 2006
    Posts
    5,139
    BG Level
    8
    FFXIV Character
    Dead Gye
    FFXIV Server
    Lamia
    FFXI Server
    Ragnarok

    C++ halp

    I have a quick question regarding arrays and classes for a programming assignment I'm attempting to complete.

    I have a class called Card, and then a class called Hand. The Hand class is an array of 5 Cards. With my main program I'm trying to access the member functions of the cards inside the Hand array.. but c++ doesn't seem to let me do that, or if it does it's definitely not the way I've been trying.

    I've declared my Hand using "Hand hand;" Using my superior logicz I assumed that "hand[i].getValue()" would perform the "getValue()" function from the card class for the card inside the ith index of the hand array.. but apparently it doesn't. Would any of you out there happen to know the correct way to do what I'm trying to do?

  2. #2
    Naver
    Guest

    Did you make the "getValue()" public or private?

  3. #3
    Hello. My name is Inigo Montoya. You killed my father. Prepare to die.
    Join Date
    Sep 2007
    Posts
    1,978
    BG Level
    6
    FFXIV Character
    Claire Farron
    FFXIV Server
    Hyperion
    FFXI Server
    Fenrir

    Quote Originally Posted by Deadgye View Post
    I have a quick question regarding arrays and classes for a programming assignment I'm attempting to complete.

    I have a class called Card, and then a class called Hand. The Hand class is an array of 5 Cards. With my main program I'm trying to access the member functions of the cards inside the Hand array.. but c++ doesn't seem to let me do that, or if it does it's definitely not the way I've been trying.

    I've declared my Hand using "Hand hand;" Using my superior logicz I assumed that "hand[i].getValue()" would perform the "getValue()" function from the card class for the card inside the ith index of the hand array.. but apparently it doesn't. Would any of you out there happen to know the correct way to do what I'm trying to do?
    It's kind of hard to visualize what you are doing, you should post the code here and let us have a looksee at what you're doing.

  4. #4
    Yoshi P
    Join Date
    Aug 2006
    Posts
    5,139
    BG Level
    8
    FFXIV Character
    Dead Gye
    FFXIV Server
    Lamia
    FFXI Server
    Ragnarok

    Quote Originally Posted by Naver View Post
    Did you make the "getValue()" public or private?
    public. When I used hand[x].getValue() inside of my Hand class it works fine.. but inside my main program it apparently doesn't want to work.

    It's kind of hard to visualize what you are doing, you should post the code here and let us have a looksee at what you're doing.
    Meh, most of it will be irrelevant to the problem but will do.

    Hand.h
    Spoiler: show
    Code:
    #pragma once
    #include "Card.h"
    
    class Hand
    {
    private:
    	Card cards[5];
    
    public:
    	Hand(void);										// Constructor
    
    	void setCard(int index, Card charizard);		// Mutator
    
    	Card getCard(int index);						// Accessor
    
    	void printHand();								// toString
    };


    Hand.cpp
    Spoiler: show
    Code:
    #include <iostream>
    #include "Hand.h"
    using namespace std;
    
    /*
    	Hand class: This class defines a hand of 5 poker cards.
    	Types: (1 = Diamonds), (2 = Hearts), (3 = Spades), (4 = Clubs)
    	Value: (1 = Ace), 2, 3, 4, 5, 6, 7, 8, 9, 10, (11 = Jack), (12 = Queen), (13 = King)
    */
    
    
    // Constructor
    Hand::Hand(void)
    {
    	int x;
    	for (x = 0; x < 5; x++) {
    		cards[x] = Card();
    	}
    }
    
    // Set the card at a given index.
    void Hand::setCard(int index, Card charizard) {
    	cards[index] = charizard;
    }
    
    // Return the card at a given index.
    Card Hand::getCard(int index) {
    	return cards[index];
    }
    
    // Print the hand graphically.
    void Hand::printHand() {
    	int x;
    
    	cout << "***** ***** ***** ***** *****\n"; 
    
    	//////////////////
    	
    	for (x = 0; x < 5; x++) {
    		if ((cards[x].getValue() <= 9) && (cards[x].getValue() >= 2)) {
    			cout << "*" << cards[x].getValue() << "  * ";
    		}
    		else if (cards[x].getValue() == 10) {
    			cout << "*10 * ";
    		}
    		else if (cards[x].getValue() == 11) {
    			cout << "*J  * ";
    		}
    		else if (cards[x].getValue() == 12) {
    			cout << "*Q  * ";
    		}
    		else if (cards[x].getValue() == 13) {
    			cout << "*K  * ";
    		}
    		else {
    			cout << "*A  * ";
    		}
    	}
    	cout << "\n";
    
    	//////////////////
    
    	for (x = 0; x < 5; x++) {
    		if (cards[x].getType() == 1) { cout << "* /\\* "; }
    		else if (cards[x].getType() == 2) { cout << "* ^^* "; }
    		else if (cards[x].getType() == 3) { cout << "* /\\* "; }
    		else { cout << "* o * "; }
    	}
    	cout << "\n";
    
    	//////////////////
    	
    	for (x = 0; x < 5; x++) {
    		if (cards[x].getType() == 3) { cout << "* ][* "; }
    		else if (cards[x].getType() == 4) { cout << "*o:o* "; }
    		else { cout << "* \\/* "; }
    	}
    	cout << "\n";
    
    	//////////////////
    
    	cout <<	"***** ***** ***** ***** *****\n\n";
    }


    Deck.h
    Spoiler: show
    Code:
    #pragma once
    #include "Card.h"
    
    class Deck
    {
    private:
    	Card cards[52];
    	int status[52];
    
    public:
    	Deck(void);					// Constructor
    
    	void Reset();				// Mutator
    
    	Card getNextCard();			// Accessor
    
    };


    Deck.cpp
    Spoiler: show
    Code:
    #include "Deck.h"
    #include <stdlib.h>
    #include <time.h>
    
    /*
    	Deck class: This class defines a deck of 52 poker cards.
    	Types: (1 = Diamonds), (2 = Hearts), (3 = Spades), (4 = Clubs)
    	Value: (1 = Ace), 2, 3, 4, 5, 6, 7, 8, 9, 10, (11 = Jack), (12 = Queen), (13 = King)
    */
    
    
    // Constructor
    Deck::Deck(void) {
    	int x;
    	int value = 1;
    	int type = 1;
    
    	// Create the deck with 52 unique poker cards.
    	for (x = 0; x < 52; x++) {
    		cards[x] = Card(value, type);
    		value++;
    
    		if (value == 14) { 
    			value = 1;
    			type++;
    		}
    	}
    
    	// Set the status of each card to "unused".
    	for (x = 0; x < 52; x++) {
    		status[x] = 1;
    	}
    }
    
    // Reset the deck.
    void Deck::Reset() {
    	int x;
    	for (x = 0; x < 52; x++) {
    		status[x] = 1;
    	}
    }
    
    // Take a random card out from the deck.
    Card Deck::getNextCard() {
    	srand(time(NULL));
    	int lolwut = 0;
    	int choice;
    
    	while (lolwut == 0) {
    		choice = rand() % 53;
    		if (status[choice] == 1) { lolwut = 1; } 
    	}
    
    	status[choice] = 0;
    	return cards[choice];
    
    }


    Card.h
    Spoiler: show
    Code:
    #pragma once
    
    class Card
    {
    private:
    	int value;
    	int type;
    
    public:
    	Card();
    	Card(int initValue, int initType);			// Constructor
    
    	void setValue(int newValue);				// Mutator - Value
    	void setType(int newType);					// Mutator - Type
    
    	int getValue();								// Accessor - Value
    	int getType();								// Accessor - Type
    
    };


    Card.cpp
    Spoiler: show
    Code:
    #include "Card.h"
    
    /*
    	Card class: This class defines a single poker card.
    	Types: (1 = Diamonds), (2 = Hearts), (3 = Spades), (4 = Clubs)
    	Value: (1 = Ace), 2, 3, 4, 5, 6, 7, 8, 9, 10, (11 = Jack), (12 = Queen), (13 = King)
    */
    
    // Useless default constructor
    Card::Card() {
    	value = 0;
    	type = 0;
    }
    
    // Constructor
    Card::Card(int initValue, int initType) {
    	value = initValue;
    	type = initType;
    }
    
    // Mutators
    void Card::setValue(int newValue) {
    	value = newValue;
    }
    
    void Card::setType(int newType) {
    	type = newType;
    }
    
    // Accessors
    int Card::getValue() {
    	return value;
    }
    
    int Card::getType() {
    	return type;
    }


    Main Program.cpp
    Spoiler: show
    Code:
    #include <iostream>
    #include "Deck.h"
    #include "Hand.h"
    #include "Card.h"
    using namespace std;
    
    /*
    	Name: 
    	Assignment: CSE230 HW5
    	Compiler: Visual Studios 2008
    	Description: This program is essentially a single player poker game. 
    */
    
    int main() {
    	// Introduction
    	cout << "Welcome to C++ Draw Poker, Spring 2010 Edition!\n";
    
    	// Variables
    	int monies = 100;
    	int choice = 1;
    	int discard;
    	int temp;
    	int x, i, j;
    	Deck deck;
    	Hand hand;
    
    	// Main Loop
    	while (choice != 0) {
    		cout << "You currently have " << monies << " monies.\n";
    
    		// Make sure player has monies.
    		if (monies == 0) {
    			cout << "You can't play with no monies! Fission Mailed. :(\n";
    			choice = 0;
    			break;
    		}
    
    		// See if player wants to play or quit; and take wager.
    		cout << "How many monies do you want to wager? (1-5)\n";
    		cout << "(Enter 0 to quit.) Wager: ";
    		cin >> choice;
    
    		// Input error handling
    		if ((choice > 0) && (choice < 6)) {;}
    		else if (choice == 0) { break; }
    		else { 
    			cout << "You have input an invalid choice. Please try again.\n";
    			choice = 1;
    			continue;
    		}
    
    		// Minus the monies.
    		monies -= choice;
    
    		// Reset the deck and draw the hand.
    		deck.Reset();
    		for (x = 0; x < 5; x++) {
    			hand.setCard(x, deck.getNextCard());
    		}
    
    		// Display the hand.
    		cout << "Your hand is currently: \n";
    		hand.printHand();
    
    		// Ask player how many cards he wishes to discard.
    		discard = 6;	// 'reset' discard
    		while ((discard < 0) || (discard > 5)) {
    			cout << "How many cards do you wish to discard? (0-5): ";
    			cin >> discard;
    
    			if ((discard >= 0) && (discard < 6)) { break; }
    
    			cout << "You have entered an invalid number. Please try again.\n";
    		}
    
    		// Ask the player which cards he wants to discard
    		while (discard != 0) {
    			cout << "Discard card: ";
    			cin >> temp;
    			hand.setCard(temp, deck.getNextCard());
    			discard--;
    		}
    
    		// Check to see if the player won.
    		// First sort the hand to make things easier for us...
    		for (j = 3; j > 0; j--) {
    			for (i = 0; i <= j; i++) {
    				if (hand[i].getValue() > hand[i+1].getValue()) {
    					Card temp = hand[i];
    					hand[i] = hand[i+1];
    					hand[i+1] = temp;
    				}
    			}
    		}
    
    
    	}
    
    	return 0;
    }


    The sorting thingy at the bottom of the main program is where I'm encountering my problem.

  5. #5
    Naver
    Guest

    Should probably post your code so we can see what's going on. I can't really visualize what's going wrong.

  6. #6
    Puppetmaster
    Join Date
    Apr 2010
    Posts
    52
    BG Level
    2
    FFXI Server
    Kujata

    So you have a class called Hand, and inside that class is an array of 5 'Cards'?

    If so, then you need to access the array of Cards itself first then use the .getValue().
    Either that or overload the [] operator to do all that for you.

    Edit: Looking at your code: hand.getCard(i).getValue() is what you probably need to do.

  7. #7
    Yoshi P
    Join Date
    Aug 2006
    Posts
    5,139
    BG Level
    8
    FFXIV Character
    Dead Gye
    FFXIV Server
    Lamia
    FFXI Server
    Ragnarok

    If so, then you need to access the array of Cards itself first then use the .getValue().
    Either that or overload the [] operator to do all that for you.
    Isn't that what hand[x] should be doing?

    Edit: Looking at your code: hand.getCard(i).getValue() is what you probably need to do.
    Thanks, that seems to have done the trick. Is there any reason why simply using hand[x] didn't reference to the same thing? (I tried moving the hand array in the hand.h into public to see if it made any difference and it didn't.)

  8. #8
    Puppetmaster
    Join Date
    Apr 2010
    Posts
    52
    BG Level
    2
    FFXI Server
    Kujata

    If you have an array of Hand classes, (i.e. Hand hand[5]) that would work. You would still then be just accessing each individual hand though.
    You want to access the array within each individual Hand class.
    To do what you want to do without going through accessors you would have to overload the [] operator.

  9. #9
    Yoshi P
    Join Date
    Aug 2006
    Posts
    5,139
    BG Level
    8
    FFXIV Character
    Dead Gye
    FFXIV Server
    Lamia
    FFXI Server
    Ragnarok

    Ah alright.. what screwed me up was that I was able to just use the brackets inside the hand class, but I guess that only works because I'm inside the hand class. Thanks for the help~

  10. #10
    Puppetmaster
    Join Date
    Apr 2010
    Posts
    52
    BG Level
    2
    FFXI Server
    Kujata

    It's that while you were working inside your Hand class you were using the cards array directly.