dsx58940 2010-12-16 03:11
浏览 35
已采纳

PHP递归函数出错了,在一段时间内递归递归

I did all sorts of debugging and referred to a multitude of sources and can't figure this out on my own. I just started working with OOP in PHP and I'm trying to write a poker hand dealer. I attempt to use a recursive function and a loop and and an if else algorithm to avoid creating duplicates in the pokerHand array. My idea of how this code should work is that when a new pokerHand object is created the __construct function will be automatically called (that's the only reason i know of to use a constructor, but i think there's more to it). Then within a do while loop the code initializes a variable $rand which is a rand number between 1 & 52 and in_array checks to see if that "card" has already been entered in the array. If the card has been dealt the __construct function is supposed to call itself to produce another card until a novel card has been dealt before storing it in the array.

I am just starting out as a coder and any feedback on my code here will be of immeasurable value to me and I thank you in advance.

<?php
 class pokerHand {
    private $_pokerHand = array();
    private $_counter = 0;


    public function __construct (){

        do{

            if  (in_array ($rand = rand(1,52), $this->_pokerHand) ){
                return public function __construct();
            } else {
                $this->_pokerHand[] = $rand;
                $this->counter++;    
            }
        } while ($this->_counter < 5)


    public function showHand(){
        print_r ($this->_pokerHand);
    } 

 }




$obj = new pokerHand();
$obj => showHand();
?>
  • 写回答

8条回答 默认 最新

  • dspx15491 2010-12-16 03:22
    关注

    You you don't call __construct (well, hardly ever) -- it's called automatically when the new keyword is used to create a new object.

    While you could certainly use recursion in this case, it seems to me that it's overly complicating things.

    Try something along the lines of:

    public function __construct (){
    
        $this->cards = array();
        while(count($this->cards) < 5){
            $card = rand(1,52);
            while(in_array($card, $this->cards)){
                $card = rand(1,52);   
            }
            $this->cards[] = $card;
        }
    }
    

    If you really want to use recursion (*cough*homework*cough*) you could generate a hand like this:

    public function makeHand($hand=null){
       if (! is_array($hand)) $hand = array();
       if (count($hand) == 5) return $hand;
       $card = rand(1,52);
       while(in_array($card, $hand)) $card=rand(1,52);
       $hand[] = $card;
       return $this->makeHand($hand);
    }
    
    public function __construct(){
       $this->hand = $this->makeHand();
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable