Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Algorithm Help
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bfh




PostPosted: Sun Aug 01, 2010 11:46 pm   Post subject: Algorithm Help

Okay Guys Im trying to think of an algorithm that could help solve this problem..

1-13 = 1 (hearts
14-26 = 2 (diamonds
27-39 = 3 (spades
40-52 = 4 (clubs

0-12 (hearts
13-25 (diamonds
26-39 (spades
40-52 (clubs

Now Each Time I cycle to the next set of card types, we start at value 0

then cross check if set 1 = set 2 or set 1 = 3 so on so forth.


any ideas, im starting to play around but would like some input on this question
code:

if (player_card2 > 12 && player_card2 <=25){
        pc_2 = player_card2 % 13;
}else if (player_card2 >= 26 && <=39){
        pc_2 = player_card2 %26;
}else{
        pc_2 = player_card2 % 39
}


Then i would do the same for player_card1 but call the new varible pc_1;

then i would check if pc_1 == pc_2 then keep going with my programming.


Im willing to bet their is a better way of doing this, their has to be, doing this for my own benifit, so
new ways of thinking are a good thing for me.


-Thanks Guys hoping to get a new way of doing this soon.
Sponsor
Sponsor
Sponsor
sponsor
TheGuardian001




PostPosted: Mon Aug 02, 2010 3:17 am   Post subject: Re: Algorithm Help

I don't really understand what you're asking...

What exactly are you trying to do here (What is the program, or this section of it, actually supposed to accomplish)? How many "Sets" of cards are there (And for that matter, what are you referring to by "set" of cards? A deck? A hand?)? Why do you have 2 different indexes on the suits?
Insectoid




PostPosted: Mon Aug 02, 2010 10:11 am   Post subject: RE:Algorithm Help

What I did for a similar project was generate an array of card objects containing a value (1-13) and a suitID (0-3) and then a second array of suits containing strings "Hearts", "Clubs", etc. Whenever a card is called up, its value is its, well, value (ace is 1, jack is 11, etc.) and its suit would be the index suitID at suits. It's not the most RAM-efficient solution but it works quite well and for something this small, RAM isn't really an issue.
DemonWasp




PostPosted: Tue Aug 03, 2010 8:57 am   Post subject: RE:Algorithm Help

This question is a derivative of the other one posted here. I think I provided a reasonably decent explanation there.

To be even more blunt:
code:

int suitID = cardValue / 4; // integer division!
int valueID = cardValue % 4; // modulus!


Insectoid: I challenge you to suggest a more ram-efficient solution that accomplishes the same effect.
md




PostPosted: Tue Aug 03, 2010 7:56 pm   Post subject: RE:Algorithm Help

c++:

unsigned char cards[52];

...

unsigned char suitID = cardValue / 4; // integer division!
unsigned char valueID = cardValue % 4; // modulus!


Wink
OneOffDriveByPoster




PostPosted: Thu Aug 05, 2010 9:58 am   Post subject: Re: RE:Algorithm Help

Noting that 4 is a power of two, this could also be done with bitwise operations.
c++:

unsigned char suitID = cardValue & 0x03;  // lower 2 bits
unsigned char valueID = cardValue >> 2// the rest of the bits


You can also think of this as a packed value with a 2-bit field and another field with the remaining bits.

Now what if you have some other number of possible values for your fields?
The division/modulus approach may be advantageous because every value within a contiguous range is valid (even when you don't happen to have some power of 2).
If you are doing many operations, it may make sense to use the bitwise approach even when you do not have a power of 2 around.
QuantumPhysics




PostPosted: Wed May 30, 2012 9:08 am   Post subject: RE:Algorithm Help

haha, you can just read up on these links:

http://en.wikipedia.org/wiki/Minimax

http://en.wikipedia.org/wiki/Bubble_sort
bbi5291




PostPosted: Wed May 30, 2012 1:41 pm   Post subject: Re: RE:Algorithm Help

OneOffDriveByPoster @ Thu Aug 05, 2010 9:58 am wrote:
Noting that 4 is a power of two, this could also be done with bitwise operations.
c++:

unsigned char suitID = cardValue & 0x03;  // lower 2 bits
unsigned char valueID = cardValue >> 2// the rest of the bits

Don't do this. It makes the code harder to read, and the optimizer will catch it anyway.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: