扑克游戏获得下一张卡ajax

时间:2013-12-30 16:21:48

标签: javascript php jquery ajax

我正在尝试创建一个基本的扑克游戏。我想在服务器上进行卡片绘制功能。我从来没有在AJax中做任何编程,但这听起来像我需要做的工作。我有两个主要问题。如果我在我的DECK.php CLASS中对NextCard()FUNCTION进行2次ajax调用,我将从同一个DECK实例获得结果,或者我将从两个不同的DECK对象获得结果。 B如何使用ajax调用CurrentCard()?谢谢

<?php
include 'Card.php';

class Deck {
    private $deck = Array();
    private $currentindex = 0;

   function __construct() {
        $x=0;   
        $suit = 's';            
        for($x=0;$x<52;$x++){
            if($x <39) $suit = 'h';
            if($x <26) $suit = 'd';
            if($x <13) $suit = 'c';         
            $deck[$x] = new Card($suit,$x % 13);    


        }
   }        

    function CurrentCard(){
        return $deck[$currentindex].toString();     
    }

    function NextCard(){
            $currentindex++;
        return $deck[$currentindex].toString();

    }
    function Shuffle(){
        $x=0;
        $temp;      
        for($x=0;$x<52;$x++){
            $Rnumb = rand (  0 , 52);               
            $temp = $deck[$x];
            $deck[$x] = $deck[$Rnumb];
            $deck[$Rnumb] = $temp;
        }

    }  

}
?>

1 个答案:

答案 0 :(得分:0)

第1和:不同的结果 第二个:

<script>
$(document).ready(function () {
    $.post( "Deck controller link like base_url(). Deck/CurrentCard", function( data ) {
        // This will be $deck[$currentindex].toString() received from controller funct.    
        // Use it on any view element(need view codes to complete:()
    });
});
</script>

您还可以从该控制器func发布json数据。在这种情况下,您需要在ajax调用中指定它。