views:

31

answers:

1

I'm writing a Bridge (card game) scoring application as practice in javascript, and am looking for suggestions on how to set up my objects. I'm pretty new to OO in general, and would love a survey of how and why people would structure a program to do this (the "survey" begets the CW mark. Additionally, I'll happily close this if it's out of range of typical SO discussion).

The platform is going to be a web-app for webkit on the iPhone, so local storage is an option. My basic structure is like this:

var Team = {
    player1: ,
    player2: ,
    vulnerable: ,  //this is a flag for whether or 
                   //not you've lost a game yet, affects scoring
    scoreAboveLine: ,
    scoreBelowLine: 
    gamesWon: 
};

var Game = {
    init: ,//function to get old scores and teams from DB
    currentBid: ,
    score: , //function to accept whether bid was made and apply to Teams{}
    save: //auto-run that is called after each game to commit to DB
};

So basically I'll instantiate two teams, and then run loops of game.currentBid() and game.score. Functionally the scoring is working fine, but I'm wondering if this is how someone else would choose to break down the scoring of Bridge, and if there are any oversights I've made with regards to OO-ness and how I've abstracted the game.

Thanks!

+1  A: 

First of all, relax a bit, if your code is working then you clearly haven't done it wrong, but your code might not be OO in the sense you wanted it to be. I presume that what you want to achieve is something like Java style OO.

JavaScript allow you to use almost any coding style, in particular JavaScript objects can be used in a lot of ways, I presume you are familiar with the most basic way of adding and changing fields dynamically. I don't know your level, but you shouldn't try to write OO code before you have got the basics down.

var foo={} //Create a new object and assign it to the identifier foo
foo.bar=5 //Add a property by giving it a value

Another thing that is important to understanding JavaScript OO wise is that any object is also a variable scope, and any variable scope is also an object. Indeed it is possible to change the current scope to an object's scope.

with(foo){
    alert(bar) //If bar is a property of foo, it's value will be shown
}

Note that it is usually not recommended to use the with statement like that, this is merely to demonstrate the concept.

OO-ness is usually achieved by using a function as a constructor. Within a typical constructor function two objects/scopes are erected, one for private variables and methods, and one for public variables and methods. We can simply use the functions own scope for the private scope (as opposed to any other language that I know of, the function scope will persist after the function has terminated). For the public scope we'll simply erect a new object.

function Team(player1,player2){
    var pub={} //The public object
    var vulnerable=false //A private field declaration
    pub.gamesWon=0 //A public field declaration
    pub.makeVulnerable=function(){ //A public method
        vulnerable=true
    }
    var methodname=function(){
        //A private method
    }
    pub.teamName=player1+" and "+palyer2 //Code that is run when the object is created
    return pub //Return the public object so that it may be used from outside the function
}

Now we can erect a new member of the Team class simply by calling the function.

teams=[]
teams[0]=Team("Joe","Johanna")
teams[1]=Team("Mike","George")
//etc.

You would probably also make a game class taking two teams and the game score as arguments, it could update the team classes immediately and save all the details for later use.

eBusiness