View Single Post
Old 2012-02-08, 21:32   #6
Bundu
 
Bundu's Avatar
 
Jul 2004
Mid Calder, Scotland

5×37 Posts
Default

axn that function is amazing, thankyou for posting a great off the cuff working example. It is so good it doesn't even need the array to be sorted, is that intentional or a by product of the bitwise operation?

Sorry for the questions but as a complete novice in javascript the bitwise functionality is beyond me at the moment! But I have begun studying it! So far I have only learned to count in binary but it is a start!

This was my approach so far (just so you know I am actually trying to do some work myself and not just sponging!) which feels positively clumsy compared to yours. My thinking was if I was analysing the hand array I would loop through and at the same time perform counts of the numbers which would allow me to decide if I should check for pairs, trips, quads of full boats. I am only at the beginning but further thinking is laid out below.

Code:
<!doctype html>
<head>
 
<style>
 
</style>
 
<script type="text/javascript">
// --------------------------------------------------------------------- GLOBAL VARIABLES - SET HERE
outputStr = "+++++++++DEBUG OUTPUT+++++++<br />";
playerhandArray = new Array(2);
communityCardarray = new Array(5);
combinedHandArray = new Array(7);
// --------------------------------------------------------------------- GLOBAL VAR END!
 
playerhandArray = [2,2];
communityCardarray = [3,5,6,13,14];
combinedHandArray = [2,3,3,4,5,5,14];
 
 
 
//reverse sort the array
function decendSort(arrayNM)  {
  arrayNM.sort(function(a,b){return b - a});
  debugOutput(arrayNM);
};
 
function checkNumSequnce(arrayNM2) {
  seqCounter=1;
  acePresent=0;
  currentStraightHigh=arrayNM2[0];
 
  repeatArray= new Array(13);
  repeatArray= [0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  if (arrayNM2[0]==14) {
    acePresent=1;
  }  
       
    // Run through the array and get some info number of sequential increments
    for (i=0;i<arrayNM2.length-1;i++) {
        j=i+1;              
          if (arrayNM2[i]- arrayNM2[j] == 1) {        // check if the two numbers have 1 difference (straight hands)           
               seqCounter++;                
                           
          } else if (arrayNM2[i]- arrayNM2[j] != 0) { // if it was equal to zero we have a repeated number and should do nothing
                                   
                seqCounter=1;     // then we break straight sequences by resetting the seqCounter               
                currentStraightHigh=arrayNM2[j];  // the currentStraightHigh is therefore the next array element
       
 
          };
         
          repeatArray[(arrayNM2[i]-1)]++ // This counts the number of instances of each value for pair - quad and fb
 
        debugOutput("Seq: " + seqCounter + " Str+: " + currentStraightHigh + " Rpt Array?: " + repeatArray); 
    }; // end of for loop
   
    repeatArray[(arrayNM2[i]-1)]++ // --------------------------------------> At this point we should analyse the repeat array
 
      
    debugOutput("<br />Seq: " + seqCounter + " Str+: " + currentStraightHigh + " Rpt Array?: " + repeatArray);
   
};  // end of function
 
 
 
//window.onload=alert('loaded'); (Remember you have an onload function...)
</script>
</head>
 
<body>
 
<div id="outerwrap" class="">
    <br />
    <br />
   
    <div id="debug"></div>
    <br />
</div>
 
</body>
<script type="text/javascript">
 
 
function debugOutput(text) {
    outputStr = outputStr + text + "<br />";
    document.getElementById('debug').innerHTML = outputStr;
};
 
 
 
// Useful Schtuff  ---- http://www.webreference.com/programming/javascript/onloads/index.html
function addLoadEvent(func) {
  var oldonload = window.onload;
    if (typeof window.onload != 'function') {
    window.onload = func;
    } else {
    window.onload = function() {
    if (oldonload) {
      oldonload();
    }
      func();
    }
  }
};
 
addLoadEvent(function() {
  debugOutput(playerhandArray);
  debugOutput(communityCardarray);
  debugOutput(combinedHandArray);
  decendSort(combinedHandArray);
});    
 
addLoadEvent(function() {
  checkNumSequnce(combinedHandArray);
});
 
 
</script>
</html>

I saw that http://www.suffecool.net/poker/evaluator.html this chap seemed to have taken the approach that you guys did (working at the bitwise level and hash tables ). Again knowing that this is currently just outside my grasp (and a suspicion that just copying his work with no thought was just plain cheating!) I was thinking that I would try and eliminate possibilities through quick determinations.

As the link specifies if all possible hands are included (5 cards) then there are 2,598,960 hand combinations of which there are 7,462 distinct hands. Becuase he went with an elimination approach I thought I *could* do the same but using simpler calculations. My theory was get some quick hits ( I am trying to replace a pass and play poker app I used to have access to on my iphone but is not on android that a buddy of mine and i played every lunchtime!):

If the players have the same hands(numerical values) you don't have to compute the numeric values of either hand as we have an instant tie and split pot!*(save calcs). [*However If they have the same hand they could avoid a tie with a flush.]
Flush is an *easy* test as there are only 4 suits, flush true breaks tie anything else keeps it the nice thing about this is that although flush mechanics are more complex if the players have the same numerically valued hands the finer flush mechanics can be ignored as they rely on numeric values of cards!

If the players don't have the same hand then we need to do a little more work as we need to evalute both hands so in the code above I thought the "repeatArray" would quickly tell me if quads or full boats were possible (don't bother testing for anything below which elminates flushes and below).
Check each hand for straights and flushes (eliminate below if found)
Then check for trips (elminate below)... and so on

I realise now that my approach involves a lot more logic and will possibly be more computationally intensive which is a bad thing!

So I will persevere for now with what I can and as I learn the bitwise element I should be able to potentially improve my code later!
Bundu is offline   Reply With Quote