View Single Post
Old 2012-02-08, 10:44   #1
Bundu
 
Bundu's Avatar
 
Jul 2004
Mid Calder, Scotland

5×37 Posts
Default sequential number test

Hi folks,

It has been a while since I last posted... a beautiful baby daughter being born kind of sucks up your free time :-)

I have been attempting to build a little fun poker app to help me learn javascript for work and I am currently chugging through the difficult part (hand evaluation!). I am not a math boffin by any means so any help would be appreciated, thanked and if it goes anywhere practical I will come back and make a donation to you (or GIMPS) whichever you prefer.

I have been looking at checking various hands and am currently working on straights. I was wondering if there was any existing formulas that could test a sequence of numbers to determine if they were sequential?
  • assuming I have cards from 1 - 14 (ace if 14)
  • A player has 2 cards
  • There are 5 community cards that they pool with
  • of which they can use a maximum of 5 out of the 7 cards

I can create an array of the numerical values of the cards, sort the array so the numbers are listed sequentially and then iterate through the array testing if the next array value is 1 higher than the former and check for 5 or more "true" results.

I was just wondering if there was a simpler more elegant solution than anyone had to hand?

thank you in advance for any help you might give!

Edit: I realise I would have to test twice with the ace as 14 and as 1!

edit2:

I was thinking that if the numbers were sequential then the total of the numbers would be equal to the first and last number x half the array length but this only works for even length arrays?

[1,2,3,4,5,6] = 21
and (1+6)x3 = 21

[7,8,9,10,11,12] = 57
and 19x3 = 57

but since I am working with 5 and 7 cards this isn't ideal? and thinking about this even more there could be repeated numbers... hmm perhaps iterating through is the most efficient?

Last fiddled with by Bundu on 2012-02-08 at 11:14 Reason: I never get the first post right lol
Bundu is offline   Reply With Quote