Using an array through a switch() statement in Javascript

前端 未结 6 2076
野的像风
野的像风 2021-01-01 23:28

I\'m trying to develop a simplified poker game through Javascript. I\'ve listed all possible card combinations a given player might have in its hand ordered by its value, li

6条回答
  •  死守一世寂寞
    2021-01-02 00:10

    a faster, potentially reusable, and more flexible way of doing it is to use an object instead of case:

    var ok= {
        '1 1 4 3 2':1,
        '1 1 5 3 2':2,
        '1 1 5 4 2':3,
        '1 1 5 4 3':4
    }[ sortedHand.join(' ') ] ;
    if(ok){ sortedHand.push( ok ,"Pair"); }
    

    objects work great when one output is hinged on one input. if you need to do five things in each case, then you have to use case, but if you just need X to turn into Y, (a 1:1), Look Up Tables in the shape of Objects are ideal.

    i imagine a RegExp can work here, i used them on a connect4 game to identify 4 in a row, but the above logic table should work as well or better than what you describe.

提交回复
热议问题