mercredi 27 janvier 2016

Alternate structure for a sequence of If statements?

I'm programming a poker program in JavaScript. I have a Hand class that has the properties "cards", "value" and "valueCards". The value property is an integer that corresponds to a hand type, and the valueCards is the array of five cards that also corresponds to the hand type. For example, if my original seven cards(contained in the cards property) contains a flush, this.value will flip to 6, and this.valueCards will equal only the five cards that equal the highest flush.

I have one method for each hand type, and ALL of them change the value and valueCards if that hand type is detected. I have an accessor method for value called getValue, so when I went to make a method to run all the tests on a hand and keep the highest one, it came out looking like this:

POKER.Hand.prototype.getTrueValue = function () {
    this.testStraightFlush();
    if(this.value == POKER.HAND_TYPE.STRAIGHT_FLUSH){ return; }

    this.testQuads();
    if(this.value == POKER.HAND_TYPE.QUADS){ return; }

    this.testFullHouse();
    if(this.value == POKER.HAND_TYPE.FULL_HOUSE){ return; }

    this.testFlush();
    if(this.value == POKER.HAND_TYPE.FLUSH){ return; }

    this.testStraight();
    if(this.value == POKER.HAND_TYPE.STRAIGHT){ return; }

    this.testTrips();
    if(this.value == POKER.HAND_TYPE.TRIPS){ return; }

    this.testTwoPair();
    if(this.value == POKER.HAND_TYPE.TWO_PAIR){ return; }

    this.testPair();
    if(this.value == POKER.HAND_TYPE.PAIR){ return; }
    
    this.getHighCards();
};

I mean, the method works fine. It just bothers me, like maybe I should be doing it a different way. Does this go against convention?

Aucun commentaire:

Enregistrer un commentaire