So, i was challenged by a coworker to build a random poker hand generator. At the time, all I had was my phone that I could run php with. So, after some tinkering and lots of searching, i came up with this:
<?php
$cards;
$card = array();
function GetCard($getSuit, $getNumber){
for($s=0; $s < 4; $s++){
for($n=0; $n < 13; $n++){
if($s == 0){
//Clubs
$suits[$s] = "-C";
} else if($s == 1){
//Hearts
$suits[$s] = "-H";
} else if($s == 2){
//Spades
$suits[$s] = "-S";
} else if($s == 3){
//Diamonds
$suits[$s] = "-D";
}
if($n == 0){
$num[$n] = "A";
} else if($n == 10){
$num[$n] = "J";
} else if($n == 11){
$num[$n] = "Q";
} else if($n == 12){
$num[$n] = "K";
} else {
$num[$n] = $n+1;
}
$cards[$s][$n] = $num[$n].$suits[$s];
}
}
return $cards[$getSuit][$getNumber];
}
function GetRandomPokerHand(){
$i = 0;
while($i < 5){
mt_srand();
$rs = mt_rand(0,3);
$rn = mt_rand(0,12);
$randomCard = GetCard($rs,$rn);
if(!in_array($randomCard,$card)){
$card[i] = GetCard($rs,$rn);
echo $card[i];
echo " ";
$i++;
}else{
echo " found ";
}
}
}
GetRandomPokerHand();
?>
I get the hand, however... every so often in_array fails and I get the same card twice. i added the "else" statement with "found" to see if it was finding the duplicate at all. It did and echos "found", and occasionally it STILL displays a duplicate.
So I decided to try the same code (well roughly the same) when i got home with c++: (Using "switch" instead of "if" was because of preference and switch statements are too much trouble on a phone)
#include <iostream>
#include <string>
#include <vector>
#include <time.h>
using namespace std;
string suits[4];
string num[13];
string cards[4][13];
string GetCard(int getSuit, int getNumber){
for (int s = 0; s < 4; s++){
for (int n = 0; n < 13; n++){
switch (s){
case 0:
suits[s] = "-C";
break;
case 1:
suits[s] = "-H";
break;
case 2:
suits[s] = "-S";
break;
case 3:
suits[s] = "-D";
break;
}
switch (n){
case 0:
num[n] = "A";
break;
case 10:
num[n] = "J";
break;
case 11:
num[n] = "Q";
break;
case 12:
num[n] = "K";
break;
default:
num[n] = to_string(n + 1);
break;
}
cards[s][n] = num[n] + suits[s];
}
}
string card = { cards[getSuit][getNumber] };
return card;
}
bool in_array(const string &value, const vector<string> &array){
return find(array.begin(), array.end(), value) != array.end();
}
void GetRanddomPokerHand(){
int hand = 0;
srand(time(NULL));
while (hand < 5){
int suit = rand() % 4;
int value = rand() % 13;
string randomCard = GetCard(suit, value);
vector<string> card = { "", "", "", "", "" };
if (!in_array(randomCard, card)){
card[hand] = randomCard;
cout << card[hand] << endl;
hand++;
}else{
cout << "found";
}
}
}
int main(){
GetRanddomPokerHand();
char stop;
cin >> stop; // yes, i know this isn't necessary in VE2013
return 0;
}
Same problem. I can't seem to figure out why the duplicates are being printed in either case. Any ideas?
In the c++ one you have this line inside your loop
vector<string> card = { "", "", "", "", "" };
That is creating a fresh blank hand each time, so it never will have duplicates.
$card[i] need to be $card[$i]
your $card array always has only 1 element which is last card cause of that mistake ;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.