PHP中的纸牌游戏“WAR”

时间:2016-09-10 13:45:15

标签: php arrays

我在学校做了一个功课,这不是必需的,但我想做。

我必须通过数组在php中创建游戏,但我不知道如何使用。 所以我开始了,我有这个问题,你知道,在纸牌游戏“战争”中,是不是重复相同的卡,所以我不知道如何使用array_push或者array_pop? 然后,我如何将代码与svg-img卡配对?

$arr = array('one', 'two', 'three', 'four', 'five' , "six" , "seven");
shuffle($arr);
$number = count($arr);

for ($x=6; $x < $number; $x++) { 
echo $arr[$x];   
}

1 个答案:

答案 0 :(得分:0)

根据您的评论,

  

在物质真实中,它是游戏,当你和某人玩耍时,你从所有卡片中获得一半,而另一个人拥有第二包。然后卡被解释,并且具有更大价值卡的人获胜。因此,在PHP中,不能重复使用已经使用的相同卡。所以我需要使用的数字,不要重复。

假设(只是为了给你一个例子),假设我们有6张面额为1到6的牌。游戏解决方案将是这样的:

(通过以下评论)

// Hypothetically, lets assume we have 6 cards of denomination 1 to 6
$cards = array(1, 2, 3, 4, 5, 6);

// Shuffle the cards
shuffle($cards);

// Count total number of cards
$number = count($cards);

// Give half of the cards to player 1 and half of the cards to Player 2
list($player1_cards, $player2_cards) = array_chunk($cards, $number / 2);

// Both the players started playing the game:
// Let say this game will be played based on randomness
for($i = 0; $i < $number / 2; ++$i){  // Number of rounds
    // Both the players draw a random card
    $player1_card_key = array_rand($player1_cards);
    $player2_card_key = array_rand($player2_cards);

    // Compare their denominations and check who won this round
    $output = $player1_cards[$player1_card_key] > $player2_cards[$player2_card_key] ? 'Player 1' : 'Player 2';
    echo $output . ' won this round. <br />';

    // Make sure that players don't use the same card again
    unset($player1_cards[$player1_card_key], $player2_cards[$player2_card_key]);
}

以下是必要的参考资料: