in_array的随机错误

时间:2014-06-25 00:29:17

标签: php c++ arrays

所以,我被一位同事挑战,建立一个随机的扑克手发电机。当时,我所拥有的只是我的手机,我可以运行php。 所以,经过一些修补和大量的搜索后,我想出了这个:

<?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();
?>

我得到了手,然而......每隔一段时间in_array就失败了,我得到同一张牌两次。我添加了&#34; else&#34;声明&#34;发现&#34;看它是否找到了副本。它确实和echos&#34;发现&#34;,偶尔它仍然显示重复。

所以当我回到c ++时,我决定尝试使用相同的代码(大致相同):(使用&#34;切换&#34;代替&#34;如果&#34;是因为偏好和切换声明在手机上太麻烦了)

#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;
}

同样的问题。我似乎无法弄清楚为什么在任何一种情况下都会打印重复的内容。 有什么想法吗?

2 个答案:

答案 0 :(得分:2)

在c ++中,你在循环中有这一行

vector<string> card = { "", "", "", "", "" };

每次创造一个新的空白手,所以它永远不会有重复。

答案 1 :(得分:2)

$ card [i]需要是$ card [$ i]

你的$ card数组总是只有1个元素,这是该错误导致的最后一个卡;)