Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Standard C, C++
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 08-15-2005, 06:30 PM   #16 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Do this:
Code:
theDeck.show(std::cout);
Basically you pass the type of stream you want the data being forwarded to. In our case the console/bash (std::cout) will do just fine.

The meaning of the excercise I gave you is not to fully understand the internal workings of the library (classes) I gave you, but more on training you how to use them. Then later we find out what is going on bit by bit. We'll disect it like we are medics examining a body.
__________________
Valmont is offline   Reply With Quote
Old 08-20-2005, 07:41 AM   #17 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
Yep, I understand that...just had no idea how to work it for this exercise. Thanks.

Anyway, I guess I'm stuck trying to show what the player has in his hand...shouldn't Player have a getMyCard_() method?
__________________
Aniviel833 is offline   Reply With Quote
Old 08-20-2005, 11:43 AM   #18 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Sure it should. But I would name it differently. Something like:
Code:
std::pair<std::string, int> my_hand() const {...}
__________________
Valmont is offline   Reply With Quote
Old 09-07-2005, 01:33 AM   #19 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
What have you got so far Anviel?
__________________
Valmont is offline   Reply With Quote
Old 09-18-2005, 12:02 PM   #20 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
Ha! See, what had happened was...school started, and now I'm not going to have much free time to devote to this *at all.* I will try and finish up this example here, but don't be surprised if my visits become few and far in between.

I'm still getting caught up on syntax. What is the purpose of the 'const' there?
Also, I don't understand why you're using this 'pair' thingy.

Thanks!
__________________
Aniviel833 is offline   Reply With Quote
Old 09-18-2005, 01:18 PM   #21 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Don't worry about the const thingy yet then. You can easely remove it. It is to prevent changing data that doesn't need to be changed.

This pair thingy is my view on what a single card is. To me a card is typically recognized by two properties: its suit and its value.
A suit is nothing else but a name: hearts, spades, clubs, diamonds. And the values are usually 1/11, 2, 3, .... 10. But anyways, typically a card is recognized by the pair suit/value. This is ideal for a container provided by the C++ STL. See the code below for a demonstration on how to create a single card:
Code:
#include <iostream> #include <utility> //pair #include <string> using namespace std; int main() { pair<string, unsigned> theCard("Hearts", 2); cout << theCard.first << endl; cout << theCard.second << endl; return 0; }
Let's see if I can make 52 cards and dump them all in a nice vector. The whole idea is to load the vector with 52 objects of type "pair". But each object has a different suit/value combination obviously.
Code:
#include <iostream> #include <utility> //pair #include <string> #include <vector> using namespace std; int main() { //STEP 1: A card is a pair or suit/value properties. pair<string, unsigned> theCard; //STEP 2: A deck is a container that holds suit/value properties. vector<pair<string, unsigned> > theDeck; //STEP 3: A nice way to load a deck with 52 cards: unsigned cardtype; string suit; for (unsigned i=0; i<52; i++) { // First 13 cards are Clubs, next 13 are Spades etc. cardtype = i/13; switch(cardtype) { case 0: suit = "Club"; break; case 1: suit = "Spade"; break; case 2: suit = "Diamond"; break; case 3: suit = "Heart"; break; } //STEP 4: So this is the card. Change the the pair object to its current card. theCard.first = suit; theCard.second = 2+i%13; //STEP 5: And then add this object to the vector. theDeck.push_back(theCard); } //We're done. We have a vector with 52 cards. Let's verify that. for (unsigned i=0; i<52; i++) { cout << theDeck[i].first << " " << theDeck[i].second << endl; } //In this example the Jack, Queen and the King have values from 12, 13 and 14. //But you can obviously customize this in STEP 3. For example, all pictures may //need to have the same value. In black jack that is 10. //Just think of a clever trick. //And if you need to have two jokers in the deck, then you'll need to find //another "clever" trick. Yada yada yada :) return 0; }
No worries if you don't have the time to finish your project. But I think you should play with the code I gave you. There's a lot of room to experiment with. That's what I always used to do when I was a student. I grabbed code from more experianced programmers and fiddled with it on a lazy sunday. I stared at it for an hour until I finally could "see" what was going on .
__________________
Valmont is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inheritance and Constructors Aniviel833 Standard C, C++ 5 08-10-2005 04:25 PM
I'm having some problems with inheritance... <-- newb mik0rs Standard C, C++ 5 04-08-2003 10:54 PM


All times are GMT -8. The time now is 06:30 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle