prevent duplicate extraction from the array

Asked 2 years ago, Updated 2 years ago, 76 views

Sorry for asking you so many questions.I would appreciate it if you could let me know as soon as possible.
Could you tell me how to avoid the same card being displayed repeatedly when two players pull the card as follows?

public function createCards()
{
    // Initialize variable
    $newCard=array();
    $marks = array(
        "clubs",
        "hearts",
        "diams",   
        "spades",
    );
    // Create a mountain bill
    $card=array();
    for($mark=0;$mark<4;$mark++){
        for($num=1;$num<=13;$num++){    
            $newCard["mark"] = $marks[$mark];
            $newCard ["number"] = $num;
            array_push($card,$newCard);
        }
    }
    return$card;
}

/**
 * making a hand card
 * @param Array $tehuda_player1,$tehuda_player2
 * @return string
 */
public function shuffleCards ($card)
{
    // Take out the bill at random
    shuffle($card); 
    // Draw 5 cards from each player's card
    $tehuda_player1[]=array();
    $tehuda_player2[]=array();
    for($i=0;$i<5;$i++){
        $tehuda_player1[] = array_pop($card);
    }
    for($i=0;$i<5;$i++){
        $tehuda_player2[] = array_pop($card);
    }
    return$tehuda_player1;
    return$tehuda_player2;
}

/**
 * a sign on a hand card
 * 
 */ 
public function showCards($tehuda_player1,$tehuda_player2)
{
    // Prepare an image of a card
    foreach($tehuda_player1 as $tmp){
        $mark = $tmp ["mark"];
        $number = $tmp ["number" ];
        echo'<img src="image/gif/'.$mark."_".$number.'.gif" alt="">";
    }
}

Ideally, the card minus $tehuda_player1 will disappear from the array after processing $tehuda_player1, but if you write $card-$tehuda_player1, nothing will appear.

If you know the cause, please let me know.

From the comments, describe the assumptions about using the function.

$card=$trump->createCards();/52 cards 
$tehuda_player1 = $trump->shuffleCards($card); 
$tehuda_player2 = $trump->shuffleCards($card); 
$player1 = $trump->showCards($tehuda_player1); 
$player2 = $trump->showCards($tehuda_player2); 
echo"<div class='player1_cards'>";".$player1."</div>"; 
echo"<div class='player2_cards'>";".$player2."</div>"; 

php array

2022-09-30 16:00

1 Answers

I haven't touched PHP for a while, so there might be a typo, but I just used it.
I don't know the assumption that I'm using, so I'm not fiddling with the previously mentioned functions, but
I'm just fiddling with the return part that I mentioned in the previous comment.
I don't know what the class name is, so it's just fooClass.
I'm concerned that only Player1 is displayed in the last output function, but please play with it moderately.
add
There was another strange part.

when initializing arrays in shuffleCards $val[]=array() is incorrect.
$val=array() where $val[] is added to the end of variables already in the array.

public function createCards()
{
    // Initialize variable
    $newCard=array();
    $marks = array(
        "clubs",
        "hearts",
        "diams",   
        "spades",
    );
    // Create a mountain bill
    $card=array();
    for($mark=0;$mark<4;$mark++){
        for($num=1;$num<=13;$num++){    
            $newCard["mark"] = $marks[$mark];
            $newCard ["number"] = $num;
            array_push($card,$newCard);
        }
    }
    return$card;
}

/**
 * making a hand card
 * @param Array $tehuda_player1,$tehuda_player2
 * @return array
 */
public function shuffleCards ($card)
{
    // Take out the bill at random
    shuffle($card); 
    // Draw 5 cards from each player's card
    $tehuda_player1 = array();
    $tehuda_player2 = array();
    for($i=0;$i<5;$i++){
        $tehuda_player1[] = array_pop($card);
    }
    for($i=0;$i<5;$i++){
        $tehuda_player2[] = array_pop($card);
    }
    return array($tehuda_player1,$tehuda_player2);
}

/**
 * a sign on a hand card
 * 
 */ 
public function showCards($tehuda_player1,$tehuda_player2)
{
    // Prepare an image of a card
    foreach($tehuda_player1 as $tmp){
        $mark = $tmp ["mark"];
        $number = $tmp ["number" ];
        echo'<img src="image/gif/'.$mark."_".$number.'.gif" alt="">";
    }
}

/**
 * I don't know how to use it, so I'll try it.
 */
public function initialize() {
    $baseCards = $this->createCards();
    $playerCards=$this->shuffleCards($baseCards);
    $this->showCards($playerCards[0], $playerCards[1]);
}

$clsObj = new fooClass();
$clsObj->initialize();

Add more questions
echo in the function, but echo is output immediately, so
If it is as it is, the div image will be printed after the card image is printed.
上記 Modified to assume that the above modified function will be executed.

$card=$trump->createCards();/52 cards 
$tehuda_players=$trump->shuffleCards($card);
// player1 output
echo"<div class='player1_cards'>";
$trump->showCards($tehuda_players[0]); 
echo "</div>"; 
// player2 output
echo"<div class='player2_cards'>"; 
$trump->showCards($tehuda_players[1]); 
echo "</div>"; 

add
Rewrite the full code for the exam.
My answer above is almost the same, but
Do you copy and duplicate the code below?
(showCards argument is one and line breaks are included to make the output easier to understand.)

<?php
Class fooClass {
    public function createCards()
    {
        // Initialize variable
        $newCard=array();
        $marks = array(
            "clubs",
            "hearts",
            "diams",   
            "spades",
        );
        // Create a mountain bill
        $card=array();
        for($mark=0;$mark<4;$mark++){
            for($num=1;$num<=13;$num++){    
                $newCard["mark"] = $marks[$mark];
                $newCard ["number"] = $num;
                array_push($card,$newCard);
            }
        }
        return$card;
    }

    /**
     * making a hand card
     * @param Array $tehuda_player1,$tehuda_player2
     * @return array
     */
    public function shuffleCards ($card)
    {
        // Take out the bill at random
        shuffle($card); 
        // Draw 5 cards from each player's card
        $tehuda_player1 = array();
        $tehuda_player2 = array();
        for($i=0;$i<5;$i++){
            $tehuda_player1[] = array_pop($card);
        }
        for($i=0;$i<5;$i++){
            $tehuda_player2[] = array_pop($card);
        }
        return array($tehuda_player1,$tehuda_player2);
    }

    /**
     * a sign on a hand card
     * 
     */ 
    public function showCards ($tehuda_player)
    {
        // Prepare an image of a card
        foreach($tehuda_player as$tmp){
            $mark = $tmp ["mark"];
            $number = $tmp ["number" ];
            echo'<img src="image/gif/'.$mark."_".$number.'.gif" alt=">'."\n";
        }
    }
}

$trump = new fooClass();
$card = $trump->createCards(); // 52 cards 
$tehuda_players=$trump->shuffleCards($card);
// player1 output
echo"<div class='player1_cards'>\n";
$trump->showCards($tehuda_players[0]); 
echo "</div>\n\n"; 
// player2 output
echo"<div class='player2_cards'>\n"; 
$trump->showCards($tehuda_players[1]); 
echo "</div>"; 
?>


2022-09-30 16:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.