views:

47

answers:

1

I'm working on a scoring script for contract bridge, just for giggles. I'm storing the game as an object:

var game = {
    team1 : { player1 : prompt("Team 1, first player: "), player2 : prompt("Team 1, second player:") }, 
    team2 : { player1 : prompt("Team 2, first player: "), player2 : prompt("Team 2, second player:") },
}

function deal(bid){
    console.log("The bid was " + bid);
    game.hand = {"bid" : bid , "made" : undefined};
    score();
}

So what I'd like to do though, better than this, is to keep a history of the games played this session. I'd like to, in pseudocode, do something like this:

game.(hand + (hand.length+1))

or something kind of like that; basically auto-increment a certain object within an object. I'm not so sure an array would would here, but perhaps? I'm open to suggestions/bettering of my code.

PS - I'd prefer to do this in javascript, not jQuery, Prototype, Dojo, MooTools... or any other library. Thanks!

EDIT

Sorry, let me clarify: The result after playing 3 hands or so would be an object like this:

var game = {
    team1 : { player1 : prompt("Team 1, first player: "), player2 : prompt("Team 1, second player:") }, 
    team2 : { player1 : prompt("Team 2, first player: "), player2 : prompt("Team 2, second player:") },
    hand1 : { bid : 2 , made : 2 } ,
    hand2 : { bid : 1 , made : 4 } ,
    hand3 : { bid : 3 , made : 1 } ,
    hand4 : { bid : 2 , //and made hasn't been set yet because we're mid-hand

}
+2  A: 

Given your pseudocode, you can do the following:

 game[hand + (hand.length+1)]

i.e. game["prop"] == game.prop - both provide access to the same property.

Alexander Gyoshev