i trying make poker algorithm determines chance of winning hand. needs extremely fast because have loop through hundreds of thousands of different hands each time.
what i'm struggling able unique hands possible come in board. board contains 5 cards.
each time card comes on board, card cannot come again.
so i've been doing possible board combinations loop through possible results using loops.
for example, i'm going first 3 cards of board.
the code this:
// $card_set_count amount of cards left in deck after taking away // user's hand cards. for($i=0;$i<$card_set_count;$i++) { // first known card $known_card1 = $card_set[$i]; for($j=0;$j<$card_set_count;$j++) { // second known card $known_card2 = $card_set[$j]; // skip card if have out if($known_card1 == $known_card2) continue; for($k=0;$k<$card_set_count;$k++) { // third known card $known_card3 = $card_set[$k]; // skip card if card out if($known_card3 == $known_card2 || $known_card1 == $known_card3) continue // create board $board = array(); $board[] = $known_card1; $board[] = $known_card2; $board[] = $known_card3; } } }
this me possible board combinations. problem gets me duplicate values too, example boards:
ad 6d 4c
is same as
4c ad 6d
i run array_unique() on list of boards, problem comes here forloop having loop through 91020 hands. super slow algorithm.
i wondering if had better idea of looping through possible boards.
storing board values in array , testing see if card value in list still slow. there way of looping through unique board combinations?
what you're doing ok, instead of iterating 0 $card_set_count
each time, second card should picked cards come after first card, , third card should picked cards come after second card, this:
for($i=0;$i<$card_set_count - 2;$i++) { for($j=$i + 1;$j<$card_set_count - 1;$j++) { for($k=$j + 1;$k<$card_set_count;$k++) { $board = array(); $board[] = $card_set[$i]; $board[] = $card_set[$j]; $board[] = $card_set[$k]; // evaluate hand ... } } }
with 50 cards left in pack, gives 19600 combinations. prune these combinations further, because hands suit isn't important, may complicated.
at first misread question, , gave answer below, doesn't address specific problem. haven't deleted because had upvote, apparently found useful.
create array cards. create variable number of cards still left: cardsleft = 52
. then, when need pick card, choose @ random card 1 52, swap chosen card card 52, , set cardsleft
51. next card, choose card 1 51, swap card 51, set cardsleft
50, , on...
whenever need start new game new deck, reset cardsleft 52. no need re-initialize or shuffle array.
i haven't used php in years, here's example in javascript; it's quite self-explanatory. run code snippet draw poker hand 3 players (see output in console).
function deck() { this.cards = []; this.left = 52; (var suit = 0; suit < 4; suit++) { (var number = 0; number < 13; number++) { this.cards.push("23456789tjqka".charat(number) + "cdhs".charat(suit)); } } } deck.prototype.draw = function() { if (this.left == 0) this.shuffle(); var pick = math.floor(math.random() * this.left); var swap = this.cards[pick]; this.cards[pick] = this.cards[--this.left]; this.cards[this.left] = swap; return swap; } deck.prototype.shuffle = function() { this.left = 52; } var d = new deck(); document.write("player 1: " + d.draw() + "," + d.draw() + "<br>"); document.write("player 2: " + d.draw() + "," + d.draw() + "<br>"); document.write("player 3: " + d.draw() + "," + d.draw() + "<br>"); document.write("flop: " + d.draw() + "," + d.draw()+ "," + d.draw() + "<br>"); document.write("turn: " + d.draw() + "<br>"); document.write("river: " + d.draw());
Comments
Post a Comment