player1和player2都应该从阵列中接收3张随机纸牌。抽出卡后,应将其丢弃。我无法将3张随机纸牌放入玩家纸牌中,也将其丢弃。拼接是实现此目的的最佳方法吗?
let pokemonCards =
[
{ name: "Bulbasaur", damage: 60 },
{ name: "Caterpie", damage: 40 },
{ name: "Charmander", damage: 60 },
{ name: "Clefairy", damage: 50 },
{ name: "Jigglypuff", damage: 60 },
{ name: "Mankey", damage: 30 },
{ name: "Meowth", damage: 60 },
{ name: "Nidoran - female", damage: 60 },
{ name: "Nidoran - male", damage: 50 },
{ name: "Oddish", damage: 40 },
{ name: "Pidgey", damage: 50 },
{ name: "Pikachu", damage: 50 },
{ name: "Poliwag", damage: 50 },
{ name: "Psyduck", damage: 60 },
{ name: "Rattata", damage: 30 },
{ name: "Squirtle", damage: 60 },
{ name: "Vulpix", damage: 50 },
{ name: "Weedle", damage: 40 }
]
// assign AI to player1 and player 2
// They should each draw 3 cards
let player1 = {
cards: [],
}
let player2 = {
cards: [],
}
//functions
let randomCard = function(){
return randomCardValue = pokemonCards[Math.floor(Math.random()*pokemonCards.length)]
}
let drawCards = function(){
for(i = 0; i < 3; i++){
player1.cards.push(randomCard())
pokemonCards.splice(randomCard(),1)
}
}
drawCards()
答案 0 :(得分:0)
尝试一下。我存储随机选择的卡,并使用splice()从卡组中取出卡,然后将卡推入玩家的手中。应该可以。
let pokemonCards =
[
{ name: "Bulbasaur", damage: 60 },
{ name: "Caterpie", damage: 40 },
{ name: "Charmander", damage: 60 },
{ name: "Clefairy", damage: 50 },
{ name: "Jigglypuff", damage: 60 },
{ name: "Mankey", damage: 30 },
{ name: "Meowth", damage: 60 },
{ name: "Nidoran - female", damage: 60 },
{ name: "Nidoran - male", damage: 50 },
{ name: "Oddish", damage: 40 },
{ name: "Pidgey", damage: 50 },
{ name: "Pikachu", damage: 50 },
{ name: "Poliwag", damage: 50 },
{ name: "Psyduck", damage: 60 },
{ name: "Rattata", damage: 30 },
{ name: "Squirtle", damage: 60 },
{ name: "Vulpix", damage: 50 },
{ name: "Weedle", damage: 40 }
]
// assign AI to player1 and player 2
// They should each draw 3 cards
let player1 = {
cards: [],
}
let player2 = {
cards: [],
}
//functions
let randomCard = function(){
return randomCardValue = pokemonCards[Math.floor(Math.random()*pokemonCards.length)]
}
let drawCards = function(){
for(i = 0; i < 3; i++){
let selectedRandomCard = pokemonCards.splice(randomCard(),1)[0];
console.log(selectedRandomCard);
player1.cards.push(selectedRandomCard);
}
}
drawCards()
答案 1 :(得分:0)
我建议改组甲板。然后,您不必担心跟踪任何内容。您可以像在真实套牌中一样弹出卡片。
这是关于shuffling的标准Stack溢出答案,我在精神上一直在使用它。如果需要,您可以pop()
循环刷卡。但是下面使用Array.from()
,这是一种无需显式循环即可创建数组的快速方法:
// shuffles the deck in place
function shuffle(array) {
var currentIndex = array.length;
while (0 !== --currentIndex) {
let randomIndex = Math.floor(Math.random() * currentIndex);
[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
}
return array;
}
let pokemonCards =
[
{ name: "Bulbasaur", damage: 60 },
{ name: "Caterpie", damage: 40 },
{ name: "Charmander", damage: 60 },
{ name: "Clefairy", damage: 50 },
{ name: "Jigglypuff", damage: 60 },
{ name: "Mankey", damage: 30 },
{ name: "Meowth", damage: 60 },
{ name: "Nidoran - female", damage: 60 },
{ name: "Nidoran - male", damage: 50 },
{ name: "Oddish", damage: 40 },
{ name: "Pidgey", damage: 50 },
{ name: "Pikachu", damage: 50 },
{ name: "Poliwag", damage: 50 },
{ name: "Psyduck", damage: 60 },
{ name: "Rattata", damage: 30 },
{ name: "Squirtle", damage: 60 },
{ name: "Vulpix", damage: 50 },
{ name: "Weedle", damage: 40 }
]
function deal(num_of_cards){
// return an array of n cards
return Array.from({length:num_of_cards}, () => pokemonCards.pop())
}
// randomize the deck order:
shuffle(pokemonCards)
console.log("deck length: ", pokemonCards.length)
// players should be in a datastructure of some sort
// not individual variables
let players = []
players.push({name: "player 1", cards: deal(3)})
players.push({name: "player 2", cards: deal(3)})
console.log(players[0])
console.log(players[1])
// double check deck length
console.log("deck length: ", pokemonCards.length)
答案 2 :(得分:0)
使用这种解决方案,并不是所有的事情都是全球性的,像游戏这样的东西很重要……但是,除了那些东西,我想说使用拼接可能是最好的方法,它很简单,易于阅读等。 ..
我也花了一些时间为您创建一个config
对象,我的意思是您可能喜欢它,不喜欢它,这就是您的喊话,我只是这么做了,因为我喜欢这样看。
const PokemonApp = function(myNameSpace) {
const publicProps = {};
const config = {
cards: [
{ name: "Bulbasaur", damage: 60 },
{ name: "Caterpie", damage: 40 },
{ name: "Charmander", damage: 60 },
{ name: "Clefairy", damage: 50 },
{ name: "Jigglypuff", damage: 60 },
{ name: "Mankey", damage: 30 },
{ name: "Meowth", damage: 60 },
{ name: "Nidoran - female", damage: 60 },
{ name: "Nidoran - male", damage: 50 },
{ name: "Oddish", damage: 40 },
{ name: "Pidgey", damage: 50 },
{ name: "Pikachu", damage: 50 },
{ name: "Poliwag", damage: 50 },
{ name: "Psyduck", damage: 60 },
{ name: "Rattata", damage: 30 },
{ name: "Squirtle", damage: 60 },
{ name: "Vulpix", damage: 50 },
{ name: "Weedle", damage: 40 }
],
cardLimit: 3,
players: [{ cards: [] }, { cards: [] }]
};
publicProps.getPlayer1 = () => { return {...config.players[0]} };
publicProps.getPlayer2 = () => { return {...config.players[1]} };
publicProps.getCards = () => { return [...config.cards] };
const randomCard = () => {
const index = Math.floor(Math.random() * config.cards.length);
return {
index: index,
card: config.cards[index]
}
};
publicProps.drawCards = () => config.players.map(p =>
Array.from({length: config.cardLimit}, () => {
const cardObject = randomCard();
p.cards.push(cardObject.card);
config.cards.splice(cardObject.index, 1);
}));
myNameSpace.launch = () => {
console.log('Launching App...');
return publicProps;
};
return myNameSpace;
}({});
const app = PokemonApp.launch();
console.log(app.getCards()); // before.
app.drawCards();
console.log(app.getCards()); // after.
console.log(app.getPlayer1());
console.log(app.getPlayer2());
答案 3 :(得分:0)
我可以看到两个问题:
您在呼叫splice()
错误(第一个参数必须是您将要开始拼接的索引,第二个参数应是删除计数:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice)因此,您需要知道您随机选择的卡的索引。您可以修改randomCards()
函数来实现此目的。
您正在randomCards
函数中两次调用drawCards
,两者均返回不同的结果。为了保持一致性,请在每个循环中调用一次,将结果分配给一个变量,并在推送和拼接时使用该变量。
我相信以下代码片段可以完成您希望代码执行的操作:
//functions
let randomCard = function() {
let randomCardIndex = Math.floor(Math.random() * pokemonCards.length)
let randomCardValue = pokemonCards[randomCardIndex]
return [randomCardIndex, randomCardValue]
}
let drawCards = function() {
for(i = 0; i < 3; i++){
let card = randomCard();
player1.cards.push(card[1])
pokemonCards.splice(card[0], 1)
}
}