我正在尝试构建一副牌并删除其中一张牌,将其个人名称,套装和值属性作为单独的元素返回。我创建了一个卡片对象,每个卡片都有一个" name"," suit"和"价值",并将其中的52个放在" Deck"阵列。我想画一张" Card"从牌组阵列中使用他们各自的属性进行纸牌游戏,但我不确定如何正确地做到这一点。下面是我试图实现这一目标的一个例子。
//CARD OBJECT
function card(name, suit, value) {
this.name = name;
this.suit = suit;
this.value = value;
}
//NEW DECK ARRAY
var cardsInDeck = new Array();
//INDIVIDUAL CARD OBJECTS IN THE DECK ARRAY
var aceSpades = new card("Ace", "Spades", 1);
cardsInDeck.push(aceSpades);
var twoSpades = new card("Two", "Spades", 2);
cardsInDeck.push(twoSpades);
var threeSpades = new card("Three", "Spades", 3);
cardsInDeck.push(threeSpades);
var fourSpades = new card("Four", "Spades", 4);
cardsInDeck.push(fourSpades);
var fiveSpades = new card("Five", "Spades", 5);
//REPEATED FOR ALL 52 CARDS
//SHUFFLE CARDS
var shuffle = function() {
var theLength = cardsInDeck.length - 1;
var toSwap;
var tempCard;
for (var i = theLength; i > 0; i--) {
toSwap = Math.floor(Math.random() * i);
tempCard = cardsInDeck[i];
cardsInDeck[i] = cardsInDeck[toSwap];
cardsInDeck[toSwap] = tempCard;
}
}
shuffle();
//DRAW ONE CARD
var drawOne = function() {
var card = cardsInDeck.slice(cardsInDeck.length - 1);
cardsInDeck.length--;
return card;
}
console.log(drawOne());
我可以得到一张"卡"要绘制,但它会返回一个数组(1),其中包含每张卡片的名称,套装和价值......但我不确定如何将这些属性分开并用它们来构建游戏。我确信这很简单,我忽略了一些小的东西,但我对编程很新,并且没有找到专门适用于这个问题的解决方案。
任何帮助都将不胜感激。
答案 0 :(得分:0)
如果我找对你,你可以在这里搜索类似的东西:
//CARD OBJECT
function card(name, suit, value) {
this.name = name
this.suit = suit
this.value = value
}
//NEW DECK ARRAY
var cardsInDeck = new Array()
//INDIVIDUAL CARD OBJECTS IN THE DECK ARRAY
var aceSpades = new card('Ace', 'Spades', 1)
cardsInDeck.push(aceSpades)
var twoSpades = new card('Two', 'Spades', 2)
cardsInDeck.push(twoSpades)
var threeSpades = new card('Three', 'Spades', 3)
cardsInDeck.push(threeSpades)
var fourSpades = new card('Four', 'Spades', 4)
cardsInDeck.push(fourSpades)
var fiveSpades = new card('Five', 'Spades', 5)
cardsInDeck.push(fiveSpades)
//REPEATED FOR ALL 52 CARDS
//DRAW ONE CARD
var drawOne = function() {
if(cardsInDeck.length > 0){
return cardsInDeck.splice(Math.random() * (cardsInDeck.length-1),1)[0]
}
}
// EXAMPLE EXECUTION
console.log(drawOne())
console.log(drawOne())
console.log(drawOne())
console.log(drawOne())
console.log(drawOne())
console.log(cardsInDeck.length)
执行示例如下:
答案 1 :(得分:0)
使用var card = cardsInDeck.pop();
绘制卡片。并且您不能使用cardsInDeck.length--;
,而pop会删除最后一张卡,因此长度将自动递减,对于更少和更好的代码,只需使用return而不创建变量,因此您的函数应该是:
var drawOne = function() {
return cardsInDeck.pop();
}
而且,不是手动创建所有卡片,而是使用它来获得更少更好的代码:
var cardsInDeck = [];
var suits = ["Spades","Hearts","Clubs","Diamonds"];
var names = ["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"];
var values = [1,2,3,4,5,6,7,8,9,10,11,12,13];
var card;
for (var i=0; i<suits.length; i++) {
for (var j=0; j<names.length; j++) {
cardsInDeck.push(new card(names[j], suits[i], values[j]));
}
}
要获得抽取卡的价值,名称或套装,请使用:
var card = drawOne();
var cardValue = card.value;
var cardName = card.name;
var cardSuit = card.suit;