简体   繁体   English

我的阵列输出出现问题

[英]I am having problems with the output of my array

I have a class that I will later break up into multiple classes creating a BlackJack card game. 我有一个班级,以后将分成多个班级来创建BlackJack纸牌游戏。 I have the the decks created and accounted for correctly. 我已经创建好牌组并正确解释了。 I am having a problem with the output of my player array in my method "deal". 我的方法“交易”中播放器数组的输出出现问题。 The output only displays one player but my array of outputs should be returning two. 输出仅显示一个玩家,但我的输出数组应返回两个。 The amount of cards that are missing from my deck also indicate that I am missing 4 cards, which makes sense since I have 2 players being dealt 2 cards. 我的牌组中缺少的纸牌数量也表示我缺少4张纸牌,这很有意义,因为我有2位玩家被发了2张牌。 Can someone please help me return both players and display both of their respective cards. 有人可以帮我找回这两位选手并显示他们各自的牌吗?

<?php
/* creating a deck of cards class to be used in a blackJack game*/

class Deck{

  public $cards = array();
  public $player = [];

  //creates an instance of a deck of cards (works)
  public function __construct(){
    $values =array('2','3','4','5','6','7','8','9','10','J','Q','K','A');
    $suits =array('Diamond','Club','Heart','Spade');
      foreach ($suits as $suit) {
         foreach($values as $value){
           $this->cards[] = "$value of $suit's";
           //$deck = $this->cards;
         }
      }
  }



  /*add more decks to increase number of total cards
  in my array (works)*/

  public function numberOfDecks($number){

    $cards = $this->cards;

    $this->number = $number;
      for($i = 0 ; $i < $number-1; $i++){
          $this->cards = array_merge($this->cards, $cards);
    }
    return $cards;
  }

  /*adding elements to a player as expected need to return multiple players and their cards multiple players (does not currently work)*/

  public function deal($numberOfPlayers){

    $this->numberOfPlayers = $numberOfPlayers;
    $player = $this->player;
    $number = 2;

    for($i = 0; $i < $number; $i++){
      for($j = 0; $j < $numberOfPlayers; $j++){
        $this->player[$j] = $this->cards[0];
        array_shift($this->cards);
      }
    }
    for($k = 0; $k < $numberOfPlayers; $k++)
      return $player[$k];
  }
}



$deck = new Deck();//works as expected
$deck->numberOfDecks(3);//works as expec
$shuffled = shuffle($deck->cards);//works as expected
$deck->deal(2);
var_dump($deck);

here is the output I am currently getting back 这是我当前返回的输出 在此处输入图片说明

The problem is in this chunk here: 问题出在这里:

for($i = 0; $i < $number; $i++){
  for($j = 0; $j < $numberOfPlayers; $j++){
    $this->player[$j] = $this->cards[0]; // <--- fix me!
    array_shift($this->cards);
  }
}

When that outer loop goes around again, it just straight overwrites the cards issued. 当该外部循环再次发生时,它将直接覆盖已发行的卡。 Try something like this: 尝试这样的事情:

for($i = 0; $i < $number; $i++){
  for($j = 0; $j < $numberOfPlayers; $j++){
    $this->player[$j][] = $this->cards[0]; // <- push into an array
    array_shift($this->cards);
  }
}

This one's creating and pushing the cards into an array for each player, rather than there being just the one (repeatedly overwritten) card. 这是为每个玩家创建纸牌并将其推入阵列,而不是只有一张(反复覆盖)纸牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM