2015-11-20 39 views
-4
#HTML# 
<!DOCTYPE html> 
     <html> 
     <head> 
      <meta charset="utf-8"> 
      <script type="text/javascript" src='angular.js'></script> 
      <script type="text/javascript" src='a.js'></script> 
     </head> 
     <body> 
     <div ng-app="myApp" ng-controller="Ctrl"> 
      <button ng-click="shuffle(obj)">shuffle</button> 
     <div ng-repeat="o in obj"> 
      {{o}} 
     </div> 
     </div> 
     </body> 
     </html> 

如何使用2个阵列在角度js中洗牌一副牌?

var app = angular.module('myApp',[]); 
 

 
app.controller('Ctrl', ['$scope', function($scope){ 
 
    $scope.obj=[["Clubs", "Diamonds", "Hearts", "Spades" ],["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" ]] 
 
     
 
     //var suits=$scope.suit.length; 
 
     //var ranks=$scope.rank.length; 
 
     //var t=suits*ranks; 
 

 
     }] 
 
    //generic shuffling function 
 
    $scope.shuffle = function(o){ 
 

 
     for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); 
 
     return o; 
 
    }; 
 
}]);
<!DOCTYPE html> 
 
<html> 
 
<head> 
 

 
<meta charset="utf-8"> 
 
<script type="text/javascript" src='angular.js'></script> 
 
<script type="text/javascript" src='a.js'></script> 
 
</head> 
 
<body> 
 
<div ng-app="myApp" ng-controller="Ctrl"> 
 
    <button ng-click="shuffle(obj)">shuffle</button> 
 
    <div ng-repeat="o in obj"> 
 
     {{o}} 
 
    </div> 
 
</div> 
 

 
</body> 
 
</html>

var app = angular.module('myApp',[]); 
app.controller('Ctrl', ['$scope', function($scope){ 
     $scope.obj=[["Clubs", "Diamonds", "Hearts", "Spades" ],["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" ]] 

      var suits=$scope.suit.length; 
      var ranks=$scope.rank.length; 
      var t=suits*ranks; 

      }] 
     //generic shuffling function 
     $scope.shuffle = function(o){ 

      for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); 
      return o; 
     }; 
    }]); 
+2

如果您分享您遇到的特定问题,这可能会有所帮助。 – wwkudu

+0

其实我想要一个完整的纸牌游戏套牌处理,创建一副纸牌,推动和洗牌。所以我试图从头开始做。但我无法找到任何方式来做“angularJS”。我尝试使用“https://github.com/Aleksey-Danchin/angular-deck”上给出的代码,但它没有给出任何输出。 – shivani

回答

1

我会用lodash。它有一个简单的“洗牌”功能:

shuffled = _.shuffle(cards); 

我想具体介绍一下卡我,不只是有“T”,所以:

deck = function() { 
    _.each(suits, function(suit){ 
     _.each(ranks, function(rank){ 
      cards.push({ 
       suit: suit, 
       value: rank 
      }); 
     }); 
    }); 
}, 

然后,我会发牌:

deal = function() { 
    shuffled = _.shuffle(cards); 
    var card = 0; 
    _.times(numCards, function(c) { 
     _.times(players.length, function(p) { 
      hands[p].push(shuffled[card]); 
      showCard(p,c); 
      card++; 
     }); 
    }); 
}, 

这里有一个的jsfiddle: https://jsfiddle.net/mckinleymedia/y2rvnzLL/1/

我希望这有助于。