Blackjack Javascript:删除对象或数组中的项目
Blackjack Javascript: Remove item in an object or an array
我已经完成了一个 Blackjack 教程,其中有一些问题没有解决(可能出于教程目的)。
我正在尝试在卡片发完后从集合中删除卡片对象。
这是我在整个教程中一直使用的 data/object 代码:
let blackjackGame = {
'you': {"scoreSpan": '#your-blackjack-result', 'div': '#your-box', 'score': 0},
'dealer': {"scoreSpan": '#dealer-blackjack-result', 'div': '#dealer-box', 'score': 0},
'cards': ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'K', 'J', 'Q', 'A'],
'cardsMap': {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'K': 10, 'J': 10, 'Q': 10, 'A': [1, 11]}, };
我已经尝试对 blackjackGame 对象的 cards 使用 delete() 方法数组和我的 deal() 函数中的 cardsMap 对象在这里:
function blackjackDeal() {
if (blackjackGame['turnsOver'] === true) {
blackjackGame['isStand'] = false;
let yourImages = document.querySelector('#your-box').querySelectorAll('img');
let dealerImages = document.querySelector('#dealer-box').querySelectorAll('img');
for (i=0; i < yourImages.length; i++) {
yourImages[i].remove();
}
for (i=0; i < dealerImages.length; i++) {
dealerImages[i].remove();
}
YOU['score'] = 0;
DEALER['score'] = 0;
document.querySelector('#your-blackjack-result').textContent = 0;
document.querySelector('#dealer-blackjack-result').textContent = 0;
document.querySelector('#your-blackjack-result').style.color = 'white';
document.querySelector('#dealer-blackjack-result').style.color = 'white';
document.querySelector('#blackjack-result').textContent = "Let's play!";
document.querySelector('#blackjack-result').style.color = 'black';
blackjackGame['turnsOver'] = true;
}
}
以及 dealerLogic() 函数:
async function dealerLogic() {
blackjackGame['isStand'] = true;
while(DEALER['score'] < 16 && blackjackGame['isStand'] === true) {
let card = randomCard();
let copy = card;
showCard(copy, DEALER);
updateScore(copy, DEALER);
showScore(DEALER);
await sleep(1000);
}
blackjackGame['turnsOver'] = true;
let winner = computeWinner();
showResult(winner);
}
您会注意到我已经尝试制作 copys blackjackGame 对象和数组,但是卡片不会去任何地方。
这两个函数中的所有代码逻辑和 blackjackGame 变量工作正常,教程中没有涉及这部分(很好),我只是非常对如何做到这一点很感兴趣,就像我几年前在JavaScript(很久以前)用彩票预测机所做的那样。
如有任何帮助,我们将不胜感激。
您是否考虑过在卡片中添加一个 属性 来指定卡片是否已删除?例如
let blackjackGame = {
'you': {"scoreSpan": '#your-blackjack-result', 'div': '#your-box', 'score': 0},
'dealer': {"scoreSpan": '#dealer-blackjack-result', 'div': '#dealer-box', 'score': 0},
'cards': [
{"card": 2, "isDealt": false},
{"card": 3, "isDealt": false},
{"card": 4, "isDealt": false},
// the other cards ....
{"card": 'A', "isDealt": false}
],
'cardsMap': {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'K': 10, 'J': 10, 'Q': 10, 'A': [1, 11]}, };
并且每当发牌时,您将 属性 isDealt
更改为 true
。并且只发满足条件 blackjackGame.cards[i].isDealt === false
.
的牌
希望对您有所帮助。
答案帮助我找到了正确的方向。
let blackjackGame = {
'hearts': {"card2": 2, "isDealt": false, "card3": 3, "isDealt": false, "card4": 4, "isDealt": false,
"card5": 5, "isDealt": false, "card6": 6, "isDealt": false, "card7": 7, "isDealt": false,
"card8": 8, "isDealt": false, "card9": 9, "isDealt": false, "card10": 10, "isDealt": false,
"cardK": 10, "isDealt": false, "cardJ": 10, "isDealt": false, "cardQ": 10, "isDealt": false,
"cardA": [1, 11]},
};
console.log(delete(blackjackGame['hearts']['card2']));
它在控制台上输出为 true,从而删除 JavaScript 对象的 属性。
我已经完成了一个 Blackjack 教程,其中有一些问题没有解决(可能出于教程目的)。
我正在尝试在卡片发完后从集合中删除卡片对象。
这是我在整个教程中一直使用的 data/object 代码:
let blackjackGame = {
'you': {"scoreSpan": '#your-blackjack-result', 'div': '#your-box', 'score': 0},
'dealer': {"scoreSpan": '#dealer-blackjack-result', 'div': '#dealer-box', 'score': 0},
'cards': ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'K', 'J', 'Q', 'A'],
'cardsMap': {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'K': 10, 'J': 10, 'Q': 10, 'A': [1, 11]}, };
我已经尝试对 blackjackGame 对象的 cards 使用 delete() 方法数组和我的 deal() 函数中的 cardsMap 对象在这里:
function blackjackDeal() {
if (blackjackGame['turnsOver'] === true) {
blackjackGame['isStand'] = false;
let yourImages = document.querySelector('#your-box').querySelectorAll('img');
let dealerImages = document.querySelector('#dealer-box').querySelectorAll('img');
for (i=0; i < yourImages.length; i++) {
yourImages[i].remove();
}
for (i=0; i < dealerImages.length; i++) {
dealerImages[i].remove();
}
YOU['score'] = 0;
DEALER['score'] = 0;
document.querySelector('#your-blackjack-result').textContent = 0;
document.querySelector('#dealer-blackjack-result').textContent = 0;
document.querySelector('#your-blackjack-result').style.color = 'white';
document.querySelector('#dealer-blackjack-result').style.color = 'white';
document.querySelector('#blackjack-result').textContent = "Let's play!";
document.querySelector('#blackjack-result').style.color = 'black';
blackjackGame['turnsOver'] = true;
}
}
以及 dealerLogic() 函数:
async function dealerLogic() {
blackjackGame['isStand'] = true;
while(DEALER['score'] < 16 && blackjackGame['isStand'] === true) {
let card = randomCard();
let copy = card;
showCard(copy, DEALER);
updateScore(copy, DEALER);
showScore(DEALER);
await sleep(1000);
}
blackjackGame['turnsOver'] = true;
let winner = computeWinner();
showResult(winner);
}
您会注意到我已经尝试制作 copys blackjackGame 对象和数组,但是卡片不会去任何地方。
这两个函数中的所有代码逻辑和 blackjackGame 变量工作正常,教程中没有涉及这部分(很好),我只是非常对如何做到这一点很感兴趣,就像我几年前在JavaScript(很久以前)用彩票预测机所做的那样。
如有任何帮助,我们将不胜感激。
您是否考虑过在卡片中添加一个 属性 来指定卡片是否已删除?例如
let blackjackGame = {
'you': {"scoreSpan": '#your-blackjack-result', 'div': '#your-box', 'score': 0},
'dealer': {"scoreSpan": '#dealer-blackjack-result', 'div': '#dealer-box', 'score': 0},
'cards': [
{"card": 2, "isDealt": false},
{"card": 3, "isDealt": false},
{"card": 4, "isDealt": false},
// the other cards ....
{"card": 'A', "isDealt": false}
],
'cardsMap': {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'K': 10, 'J': 10, 'Q': 10, 'A': [1, 11]}, };
并且每当发牌时,您将 属性 isDealt
更改为 true
。并且只发满足条件 blackjackGame.cards[i].isDealt === false
.
希望对您有所帮助。
答案帮助我找到了正确的方向。
let blackjackGame = {
'hearts': {"card2": 2, "isDealt": false, "card3": 3, "isDealt": false, "card4": 4, "isDealt": false,
"card5": 5, "isDealt": false, "card6": 6, "isDealt": false, "card7": 7, "isDealt": false,
"card8": 8, "isDealt": false, "card9": 9, "isDealt": false, "card10": 10, "isDealt": false,
"cardK": 10, "isDealt": false, "cardJ": 10, "isDealt": false, "cardQ": 10, "isDealt": false,
"cardA": [1, 11]},
};
console.log(delete(blackjackGame['hearts']['card2']));
它在控制台上输出为 true,从而删除 JavaScript 对象的 属性。