News    Guild    Options    More
Forums:   Guild,    Games,    Hardware,    Misc
Home 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 Sigil Developer Tracker



The dev tracker is no longer actively scanning, however you may continue to browse the archives collected over the past several years here. Please remember that these developer posts are taken out of context, so beware of any silky venom being spewed forth.

Color Key:

Green - Sigil Games Online Employee
Pink - Sony Online Entertainment
Gray - Microsoft Game Studios Employee
Orange - Community Member

Thread List Dev List Site List Search


0 C++ Question about the string1.compare(x,n,string2); function O F F I C I A L    V A N G U A R D    F O R U M S
Dec 13, 2005 - 11:34 AM - by Crestone
Quote:
Originally Posted by Baadan

In my head, it code looks like this:
Code:
if (c[0].ard.compare(0, 3, c[1].ard) == 0) cout << "You have a pair!" << endl;

String comparisons have never been my favorite thing for a few reasons:
1. the compare functions are very cryptic (including the descriptions in help docs), as you've undoubtedly found.
2. they're slow.
3. their behavior rarely seems to be what you want

I'd actually suggest a different approach if you're getting to the point where the string comparisons are becoming too unwieldy to deal with.

Create a struct that just contains 2 integers:
Code:
struct card { int suit; int value; };
suit goes from 0-3 (or 1-4 if you don't like 0 indexing), and value 0-12 (or 1-13).

Then just create an array for each player of that struct and fill the 2 values for each card.

To check for a pair, you just need to see if they have two cards where the value member of the struct is the same. For a flush, just check to see if the suit member is the same.

When you go to display what is in a player's hand, you just loop through a player's hand doing switch statements on the suit and value of each card:
Code:
switch player1[i].suit { case 0: cardsuit = "hearts"; break; etc etc etc; } switch player1[i].value { case 0: cardvalue = "Ace"; break; etc etc etc; } cout << "card 1: " << cardvalue << " of " << cardsuit;
The syntax probably isn't correct for some of that...it's been ages since I've dealt with the c++ libraries like iostream and stock strings, but that should give a good idea.

You could also create a 2 dimension array for the players and support more than 2 people. You'd need to keep track of whether or not a card is already dealt somehow, so that could lead to even another approach:

Create a 2d int array of the deck with 4 columns and 13 rows where [0][2] could be the 3 of hearts. When a player is dealt a card, put their player number at the corresponding location. To look for pairs, you have to search for the same player number in a row, and for a flush, 5 of a single player in a single column.

Finding that google is a great programming resource is also a good move, and you did that, so you're not far from being done now.

Good luck getting it done!
reply to this post Find all posts by Crestone.
Find all posts in "C++ Question about the string1.compare(x,n,string2); function".




All times are GMT -4. The time now is 08:29 PM.



©2005-2011 Silky Venom
Hosted by...
Uberguilds Network