我是 Javascript 的初学者,正在尝试制作二十一点游戏。
数组和对象:
card = {}, //every card is an object having 3 properties: suit, number and points
playerCards = [], //an array containing player cards (objects)
dealerCards = [], //an array containing dealer cards (objects)
我还手动创建了一些用于测试目的的卡片,如下所示:
card = {
suit : "spade",
number : "queen",
points : 10
};
playerCards.push(card);
然后我有随机创建新卡并检查它是否已被使用的功能:
function makeCard() {
"use strict";
var uK;
for (;;) {
createCard(card, suit, number, points); // creates a new card, code below
checkCard(card, playerCards, dealerCards, uC); // checkes if card is already in use and returns uC = 1 if "yes" and uC = 0 if "not" (working)
if (uK === 0) {
break;
}
}
return card;
}
这是 createCard() 的代码:
function createCard(card, suit, number, points) {
"use strict";
var i = Math.floor(Math.random() * 4);
card.suit = suit[i];
i = Math.floor(Math.random() * 13);
card.number = number[i];
card.points = points[i];
return card;
}
它可以工作,但是当它创建一张新卡时,它会替换最后一张手动添加到 playerCards/dealerCards 数组中的卡。我可以通过创建一个毫无意义的示例卡而不是将其添加到数组中来防止它(然后这个将被替换),但这似乎不正确。所以我该怎么做?
提前致谢!