在Javascript中动态命名对象
我正在尝试创建一个21点游戏,当涉及到分手时,我遇到了一个问题。最后,我想创建一个在Javascript中动态命名对象,javascript,arrays,Javascript,Arrays,我正在尝试创建一个21点游戏,当涉及到分手时,我遇到了一个问题。最后,我想创建一个关联数组,该数组具有每个分割的总计和状态,将每个分割视为自己的手对象。我遇到的一个问题是命名功能,我正在尝试动态执行命名功能,因此每次调用分割函数时,它都会根据手被分割的次数创建名称 到目前为止,我已经创造了一些背景知识;卡片对象包含名称、花色和价值(即皇后、俱乐部、10)。我有一个名为cardsInDeck的数组,它在一个牌组中容纳所有的牌对象,该牌组被洗牌,因此每张牌都可以随机抽取。当从卡片组中取出卡片时,值被
关联数组
,该数组具有每个分割的总计和状态,将每个分割视为自己的手对象
。我遇到的一个问题是命名功能,我正在尝试动态执行命名功能,因此每次调用分割函数时,它都会根据手被分割的次数创建名称
到目前为止,我已经创造了一些背景知识;卡片对象包含名称、花色和价值(即皇后、俱乐部、10)。我有一个名为cardsInDeck的数组,它在一个牌组中容纳所有的牌对象,该牌组被洗牌,因此每张牌都可以随机抽取。当从卡片组中取出卡片时,值被推入一个数组以计算值,名称和西装被连接并添加到字符串中,以填充HTML以在页面上显示卡片
function drawOne() {
let card = cardsInDeck.pop();
return card;
}
var handNumber = 0;
var playerHand01 = [];
var p1, p2, d1;
function initialDealOut() {
++handNumber;
p1 = drawOne();
playerHand01.push(p1.value);
++playerCount;
let p1n = p1.name + p1.suit;
let p1c = "images/" + p1n + ".png";
let p1Image = document.getElementById('player01');
p1Image.src = p1c;
}
这对交易商的第一张牌(d1)和玩家的第二张牌(p2)重复,效果良好。我想做的是让playerHand01
成为手部对象的一部分,该手部对象位于容纳所有手部的Player数组中,并将状态(“动作”、“站立”、“胸围”)关联到每个手部对象
let playerHands = [];
function Split() {
++handNumber;
let newHand = new Hand ({
handName: 'playerHand' + handNumber
handTotal: 0,
status: 'action'
});
playerHands.push(newHand);
}
我正试图通过以下代码来实现这一点:
var player = new Array();
function Hand(total, status) {
this.total = total;
this.status = status;
}
function Split() {
++handNumber;
let handName = "playerHand0" + handNumber;
let handTotal = "playerHandTotal0" + handNumber;
handName = new Hand {
handTotal: 0,
status: "action"
}
}
我是编程新手,我知道这是可以做到的,只是知道我现在的做法并不完全正确
如果您有任何建议,我将不胜感激。我可能会使用阵列来存储双手。它们可以通过索引访问。如果你想给他们一个名字,那可能是手对象的一部分
let playerHands = [];
function Split() {
++handNumber;
let newHand = new Hand ({
handName: 'playerHand' + handNumber
handTotal: 0,
status: 'action'
});
playerHands.push(newHand);
}
或者,如果您想使用对象(可能是因为我误解了您的需求),可以动态地为对象指定属性。有两种不同的语法可用于对象属性。以下两项具有相同的结果:
let myObject = {};
myObject.test = 'hi';
及
您通常会使用前者,但如果您在运行时之前不知道名称,则后者会增加灵活性,因为您可以使用任何字符串进行访问。例如,以下内容完全有效:
let player = {};
let handName = 'playerHand0' + handnumber;
player[handName] = new Hand();
这本书读起来有点让人困惑,而且我认为您还没有充分考虑到需要存储哪些信息
由于这个问题的焦点实际上是如何分手,我将向你们展示这一点,希望它能为你们指明正确的方向
首先是手的物体。我将使用es6,但是您可以使用那里的构造函数方法来做类似的事情
class Hand {
constructor(total, status){
this.total = total;
this.status = action;
this.cards = [];
}
drawCard(){
this.addCard(drawOne());
}
addCard(card){
this.cards.push(card);
this.updateScore();
}
updateScore(){
// loop through and update the score and status
}
// this is the one you've been waiting for
split(player){
// can only split in certain conditions
if(this.cards.length !== 2 || this.cards[0] !== this.cards[1]){
console.log('cannot split this (?)');
return null;
}
// create the new hand
let additionalHand = new Hand(0, 'active');
additionalHand.addCard(this.cards.pop());
player.hands.push(additionalHand);
// need to draw a new card for each hand
this.drawCard();
additionalHand.drawCard()
//return the new hand in case you want to do something with that.
return additionalHand;
}
}
这可能不是您想要的,但它应该是一个很好的帮助初始指南。我建议为每个概念(玩家、牌、牌组、手、游戏)设置类,并且在游戏级别,您将保留一组活动手,这些手(在分割后)的数量可能与玩家数量不同
下面是它的外观:
class Card {
constructor(name, suit, value) {
this.name = name+'';
this.suit = suit;
this.value = typeof name === 'number' ? name
: 10 + (name === 'Ace');
}
}
class Player {
constructor(name) {
this.name = name;
}
}
class Hand {
constructor(player, total, status) {
this.player = player; // a back reference to the owning player
this.total = total;
this.status = status;
}
addCard(card) {
this.total += card.value;
}
copy() {
return new Hand(this.player, this.total, this.status);
}
}
class Deck {
constructor() {
this.cards = [];
for (let suit of ['Clubs', 'Hearts', 'Spades', 'Diamonds']) {
for (let name of ['Ace',2,3,4,5,6,7,8,9,10,'Jack','Queen','King']) {
this.cards.push(new Card(name, suit));
}
}
}
shuffle() {
for (let i = this.cards.length; i; i--) {
let j = Math.floor(Math.random() * i);
[this.cards[i - 1], this.cards[j]] = [this.cards[j], this.cards[i - 1]];
}
return this;
}
drawOne() {
return this.cards.pop();
}
}
class Game {
constructor() {
this.players = [];
this.activeHands = [];
this.currentHandId = 0;
this.deck = new Deck().shuffle();
}
addPlayer(player) {
this.players.push(player);
// Create a hand for this player:
this.activeHands.push(new Hand(player, 0, "active"));
}
deal() {
// Draw card and assign to current hand
this.activeHands[this.currentHandId].addCard(this.deck.drawOne());
}
nextHand() {
this.currentHandId = (this.currentHandId + 1) % this.activeHands.length;
}
split() {
// Insert the new, copied hand just after the current one:
this.activeHands.splice(this.currentHandId+1, 0,
this.activeHands[this.currentHandId].copy());
}
bust() {
// remove hand from active hands:
this.activeHands[this.currentHandId].status = "busted";
this.activeHands.splice(this.currentHandId, 1);
}
}
// Start a game
var game = new Game();
game.addPlayer(new Player('Helen'));
game.addPlayer(new Player('John'));
// Deal a card to the first active hand
game.deal();
game.nextHand(); // move to second player
game.deal();
// ...etc.
// When you need to split the current hand
game.split();
// Then deal a card to both hands:
game.deal();
game.nextHand(); // move to second hand of same player
game.deal();
// ..etc.
当然,您需要添加更多的游戏逻辑和规则。但这可能是一个模板,可以扩展到您的需要。这是一个令人惊讶的响应……尽管这与我已经完成的工作相去甚远。这是一种全新的解决问题的方法,我将在另一个时间再次讨论。谢谢你的意见!存储手的数组就是这个想法……player=new array();但我的想法是,我需要在数组中有许多Hand对象才能存储多个值…即handName、handTotal、handStatus。我认为你的第一个建议对我正在努力做的事情最有效。谢谢你的回复!因此,当我创建新手并分配handName:handTotal:和status时,当我注销它时,它返回:name:{name:“playerHand01”,total:Array(0),status:“action”}status:undefined total:undefined proto:Object