Sunday, July 12, 2009

How would you define poker hands like 4 of a kind, flush, straight in C++ and how would you compare them?

Here is what I did so far:


// Royal Flush





int rf, RoyalFlush = 15;


for (int rf = 0; rf %26lt; 5; rf++)


{


if ((rf[0] == 1 %26amp;%26amp; rf[1] == 10 %26amp;%26amp; rf[2] == 11 %26amp;%26amp; rf[3] == 12 %26amp;%26amp; rf[4] == 13)||


(rf[0] == 14 %26amp;%26amp; rf[1] == 23 %26amp;%26amp; rf[2] == 24 %26amp;%26amp; rf[3] == 25 %26amp;%26amp; rf[4] == 26)||


(rf[0] == 27 %26amp;%26amp; rf[1] == 36 %26amp;%26amp; rf[2] == 37 %26amp;%26amp; rf[3] == 38 %26amp;%26amp; rf[4] == 39)||


(rf[0] == 40 %26amp;%26amp; rf[1] == 49 %26amp;%26amp; rf[2] == 50 %26amp;%26amp; rf[3] == 51 %26amp;%26amp; rf[4] == 52)||


}


player1card[rf] = RoyalFlush;





// Four of a Kind





int fk, FourKind = 14;


for (int fk = 0; fk %26lt; 5; fk++)


{


if





*/


// Straignt





int n = 0, straignt = 10;





player2Num[n]%13 = J;





if(player2Num[n+1]%13 = (Jay +1);


{


if(player2Num[n+2]%13 = (Jay +2);


{


if(player2Num[n+3]%13 = (Jay +3);


{


if(player2Num[n+4]%13 = (Jay +4);


}


}


}


player2card = Straigh

How would you define poker hands like 4 of a kind, flush, straight in C++ and how would you compare them?
Here's how I remembered doing it when I was in highschool. I randomized 52 numbers to represent the cards and put them into an array. my array would look something like this


[43,5,12,24,33,37,9.... and so on until all 52 number has been allocated]





there i divided them using the n / 13 to sort them into their suit (0=hearts,1=diamond,2=club,3=clover), and n % 13 to get the card #. like say i got the number 43, so 43/13=3.xxx and i get the remainder by using 43 % 4, so that would make the card the 4 of club..





so then with everything sorted, i'll take/pop 5 cards out of the array/stack and analyze their suit and numbers.





a flush for example


if ( int(card1/13) == int(card2/13) %26amp;%26amp; int(card2/13) == int(card3/13) %26amp;%26amp; int(card3/13) == int(card4/13) %26amp;%26amp; int(card4/13) == int(card5/13) ) {





flush=1;


}





a straight is a tad more difficult





assigning a card from my array


i=0; // index


card1=shuffled_cards[i]%13;


i++; //do so for all the other 4 cards or make a loop for it.





check the range of one card to see if all the others is within its range. say


rangex=card1+5;


rangey=card1-5;


if (card2 %26gt; rangex || card2 %26lt; rangey) {


cout %26lt;%26lt; "not a straight";


}


check to see that all the card is different so like


if (card1 != card2 %26amp;%26amp; card1 != card3 // and so on.


{


straight = 1;


}





if i had time, i would've put the 5 cards into an array and sort


them then check if the number is incremented by 1 after every iteration.





combine this with the flush and if both ring out true, then you've got a royal flush.





if (flush %26amp;%26amp; straight) {


royal_flush=1;


}





my program was actually a blackjack game, so sorry if my way of doing thing is wrong.


No comments:

Post a Comment