Using an array through a switch() statement in Javascript -
i'm trying develop simplified poker game through javascript. i've listed possible card combinations given player might have in hand ordered value, this:
switch(sortedhand) { //pair case [1,1,4,3,2]: sortedhand.push(1,"pair"); break; case [1,1,5,3,2]: sortedhand.push(2,"pair"); break; case [1,1,5,4,2]: sortedhand.push(3,"pair"); break; case [1,1,5,4,3]: sortedhand.push(4,"pair"); break; case [1,1,6,3,2]: sortedhand.push(5,"pair"); break; case [1,1,6,4,2]: sortedhand.push(6,"pair"); break; case [1,1,6,4,3]: sortedhand.push(7,"pair"); break; case [1,1,6,5,2]: sortedhand.push(8,"pair"); break; case [1,1,6,5,3]: sortedhand.push(9,"pair"); break; case [1,1,6,5,4]: sortedhand.push(10,"pair"); break;
even though "sortedhand" array stores values succesfully (as i've seen through console.log), switch() statement returns default case, , gets straight flush. fear matter of literal approach i've used declare possible array values compared whole of "sortedhand", don't know better. possible use switch() in such manner?
you can try switch
ing on textual representation of array.
switch(sortedhand.join(' ')) { //pair case '1 1 4 3 2': sortedhand.push(1,"pair"); break; case '1 1 5 3 2': sortedhand.push(2,"pair"); break; case '1 1 5 4 2': sortedhand.push(3,"pair"); break; case '1 1 5 4 3': sortedhand.push(4,"pair"); break; // etc. }
as alternative specifying every case directly, perhaps build function dispatch table using object , rid of switch entirely.
var dispatch = {}; // build table you'd like, application (var = 0; < 10; i++) { (function(i) { var hand = ...; // add hand logic here dispatch[hand] = function() { sortedhand.push(i, "pair"); }; })(i); } // execute routine dispatch[sortedhand.join(' ')]();
Comments
Post a Comment