Module: HandRecordUtility
- Defined in:
- lib/hand_record_utility.rb,
lib/hand_record_utility/version.rb
Constant Summary collapse
- D =
Constants. D is a 29 digit number
22222222221111111111000000000 98765432109876543210987654321 53644737765488792839237440000- DIR_N =
Different directions
0- DIR_E =
1- DIR_S =
2- DIR_W =
3- DIRECTIONS =
[ "North", "East", "South", "West", ]
- CHAR_TO_BITS =
64 characters so can map
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ{}"- BITS_TO_CHAR =
Given the ascii characters, map to a number. Subtract 48 from the char
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, # 0123456789 -1, -1, -1, -1, -1, -1, -1, 36, 37, 38, # :;<=>?@ABC 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, # DEFGHIJKLM 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, # NOPQRSTUVW 59, 60, 61, -1, -1, -1, -1, -1, -1, 10, # XYZ[/]^_'a 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, # bcdefghijk 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, # lmnopqrstu 31, 32, 33, 34, 35, 62, -1, 63 # vwxyz{|} ]
- PAVLICEK_RANKED =
Using the Ranked order, the order of cards is AS, AH, AD, AC Using the Suited order, the order of cards is AS, KS, QS, … Always used Ranked
1- PAVLICEK_SUITED =
2- S_MAX =
Used in Andrews numbers
Combinatorics::Choose.C(26,13)
- E_MAX =
Combinatorics::Choose.C(39,13)
- VERSION =
"1.0.2"
Class Method Summary collapse
-
.andrews_number_to_board(number) ⇒ Object
Convert from an Andrews number to a board.
-
.board_to_pavlicek_array(board, ranked_or_suited) ⇒ Object
Given a hand, converts to a Pavlicek array This is a new term I have created.
-
.debug_board(board) ⇒ Object
Print out in format normally associated with bridge journalism.
-
.debug_board_hand_suit(hand, suit) ⇒ Object
Returns the cards in a suit for a hand Assumes hand is well formed.
-
.debug_board_ns(hand) ⇒ Object
Prints the North or South hand.
-
.debug_board_short_form(board) ⇒ Object
Print a board in short form.
-
.debug_board_short_form_suit(hand_name, hand) ⇒ Object
Helper routine to print a hand in short form.
-
.debug_pavlicek_array(pav_array) ⇒ Object
Debug utility to print values of the pav_array.
- .decode_to_increasing_sequence(index) ⇒ Object
- .encode_increasing_sequence(sequence) ⇒ Object
-
.hex_to_number(s) ⇒ Object
From hex back to int This routine does no error checking on the input.
-
.number_to_hex(i) ⇒ Object
Given a number, convert to a 24 character hex string.
-
.number_to_string_64(i) ⇒ Object
From number to character string This routine does no error checking on the input.
-
.pavlicek_array_to_andrews_sequences(pav_array) ⇒ Object
Convert board to Andrews number.
-
.pavlicek_array_to_hand(pav_hand_array) ⇒ Object
pav_hand is an array of cards Returns a 17 char string with the hand record.
-
.pavlicek_array_to_number(pav_array) ⇒ Object
Given a Pavlicek array, convert to a number Using variable names from www.rpbridge.net/7z68.htm.
-
.pavlicek_arrays_to_board(hands) ⇒ Object
pavlicek_arrays_to_board takes four arrays and returns the entire board.
-
.pavlicek_hand_array_to_suit(pav_hand_array) ⇒ Object
Given an array holding cards in a suit, return a string with that suit.
-
.pavlicek_number_to_board(number) ⇒ Object
Default is suited Pavlicek web site uses the ranked order, Andrews reference is to the suited order.
-
.pavlicek_ranked_number_to_board(number) ⇒ Object
Uses the ranked Pavlicek method.
-
.pavlicek_ranked_or_suited_number_to_board(number, ranked_or_suited) ⇒ Object
Algorithm: 1.
-
.pavlicek_suited_number_to_board(number) ⇒ Object
Uses the suited Pavlicek method (default).
-
.put_hand_in_pavlicek_array(pav_array, hand, direction, ranked_or_suited) ⇒ Object
Given a hand in format “SKQH5432DAQJCT954” Fill out the pavlicek array.
-
.string_64_to_number(s) ⇒ Object
Converts the string to an int.
- .to_andrews_number(board) ⇒ Object
-
.to_pavlicek_number(board) ⇒ Object
Converts from a board to a Pavlicek number See www.rpbridge.net/7z68.htm Pavlicek ranks the cards in this order: AS, AH, AD, AC, KS.…
-
.to_pavlicek_ranked_number(board) ⇒ Object
Given a hand, converts to a Pavlicek array.
- .to_pavlicek_suited_number(board) ⇒ Object
Class Method Details
.andrews_number_to_board(number) ⇒ Object
Convert from an Andrews number to a board
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
# File 'lib/hand_record_utility.rb', line 509 def self.andrews_number_to_board(number) routine_name = "andrews_number_to_board" # Error check if (number <= 0) then puts "Error in #{routine_name}. Invalid number #{number}. Must be 1 or higher." return nil end if (number > HandRecordUtility::D) then puts "Error in #{routine_name}. Invalid number #{number} is too big." return nil end # Need to subtract 1 because the UI is 1..D, code is 0..D-1 number = number - 1 # Using variable names from http://www.rpbridge.net/7z68.htm s_index = number % S_MAX quotient = number / S_MAX e_index = quotient % E_MAX n_index = quotient / E_MAX north_sequence = decode_to_increasing_sequence(n_index) east_sequence = decode_to_increasing_sequence(e_index) south_sequence = decode_to_increasing_sequence(s_index) # initialize all cards to west since anything missing must be there! pav_array = Array.new(52,DIR_W) [[north_sequence,DIR_N], [east_sequence,DIR_E], [south_sequence,DIR_S]].each do |sequence,direction| hand_loc = 0 sequence_loc = 0 pav_array.each_with_index do |val,idx| if (val == DIR_W) then if (hand_loc == sequence[sequence_loc]) then pav_array[idx] = direction sequence_loc = sequence_loc + 1 end hand_loc = hand_loc + 1 end end end nsew = Array.new(4) { Array.new(4) { Array.new } } pav_array.each_with_index do |dir,idx| suit = idx / 13 rank = idx % 13 nsew[dir][suit].push(rank) end pavlicek_arrays_to_board(nsew) end |
.board_to_pavlicek_array(board, ranked_or_suited) ⇒ Object
Given a hand, converts to a Pavlicek array This is a new term I have created. Returns an array [0..51] where 0 is AS, 1 is AH, 2 is AD.… 51 is 2C The values in the array are [0123] (n, e, s, w) In other words, for each card we know which hand it is in
316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/hand_record_utility.rb', line 316 def self.board_to_pavlicek_array(board, ranked_or_suited) # Initialize an array of size 52. Set all values to -1 (as an error check) pav_array = Array.new(52, -1) put_hand_in_pavlicek_array(pav_array, board[:north], DIR_N, ranked_or_suited) put_hand_in_pavlicek_array(pav_array, board[:east], DIR_E, ranked_or_suited) put_hand_in_pavlicek_array(pav_array, board[:south], DIR_S, ranked_or_suited) put_hand_in_pavlicek_array(pav_array, board[:west], DIR_W, ranked_or_suited) # Return the array pav_array end |
.debug_board(board) ⇒ Object
Print out in format normally associated with bridge journalism
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 |
# File 'lib/hand_record_utility.rb', line 648 def self.debug_board(board) debug_board_ns(board[:north]) sw = debug_board_hand_suit(board[:west], "S") se = debug_board_hand_suit(board[:east], "S") printf "S: %-14s S: %s\n", sw, se sw = debug_board_hand_suit(board[:west], "H") se = debug_board_hand_suit(board[:east], "H") printf "H: %-14s H: %s\n", sw, se sw = debug_board_hand_suit(board[:west], "D") se = debug_board_hand_suit(board[:east], "D") printf "D: %-14s D: %s\n", sw, se sw = debug_board_hand_suit(board[:west], "C") se = debug_board_hand_suit(board[:east], "C") printf "C: %-14s C: %s\n", sw, se debug_board_ns(board[:south]) end |
.debug_board_hand_suit(hand, suit) ⇒ Object
Returns the cards in a suit for a hand Assumes hand is well formed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 |
# File 'lib/hand_record_utility.rb', line 675 def self.debug_board_hand_suit(hand, suit) s = "" case suit when "S" s = hand[/S.*H/] s = s[1, s.length - 2] when "H" s = hand[/H.*D/] s = s[1, s.length - 2] when "D" s = hand[/D.*C/] s = s[1, s.length - 2] when "C" s = hand[/C.*/] s = s[1, s.length - 1] end return s end |
.debug_board_ns(hand) ⇒ Object
Prints the North or South hand
666 667 668 669 670 671 |
# File 'lib/hand_record_utility.rb', line 666 def self.debug_board_ns(hand) printf " S: %s\n", debug_board_hand_suit(hand, "S") printf " H: %s\n", debug_board_hand_suit(hand, "H") printf " D: %s\n", debug_board_hand_suit(hand, "D") printf " C: %s\n", debug_board_hand_suit(hand, "C") end |
.debug_board_short_form(board) ⇒ Object
Print a board in short form
635 636 637 638 639 640 |
# File 'lib/hand_record_utility.rb', line 635 def self.debug_board_short_form(board) debug_board_short_form_suit("N", board[:north]) debug_board_short_form_suit("S", board[:south]) debug_board_short_form_suit("E", board[:east]) debug_board_short_form_suit("W", board[:west]) end |
.debug_board_short_form_suit(hand_name, hand) ⇒ Object
Helper routine to print a hand in short form
643 644 645 |
# File 'lib/hand_record_utility.rb', line 643 def self.debug_board_short_form_suit(hand_name, hand) puts " #{hand_name}: #{hand}" end |
.debug_pavlicek_array(pav_array) ⇒ Object
Debug utility to print values of the pav_array
622 623 624 625 626 627 628 629 630 631 632 |
# File 'lib/hand_record_utility.rb', line 622 def self.debug_pavlicek_array(pav_array) (0..4).each do |tens| printf "%2d: ", tens (0..9).each do |digit| num = (tens * 10) + digit printf "%d ", pav_array[num] end puts "\n" end printf "%2d: %d %d \n", 5, pav_array[50], pav_array[51] end |
.decode_to_increasing_sequence(index) ⇒ Object
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/hand_record_utility.rb', line 489 def self.decode_to_increasing_sequence(index) length = 13 result = Array.new( length, -1 ) 13.downto(1) do |i| last_value = 0 num = i next_value = Combinatorics::Choose.C(num,i) while index >= next_value do num += 1 last_value=next_value next_value = Combinatorics::Choose.C(num,i) end result[i-1] = num-1 index = index - last_value end result end |
.encode_increasing_sequence(sequence) ⇒ Object
466 467 468 469 470 471 472 473 474 |
# File 'lib/hand_record_utility.rb', line 466 def self.encode_increasing_sequence(sequence) sum = 0 sequence.each_with_index do |val,index| if val > index then sum = sum + Combinatorics::Choose.C(val,index+1) end end sum end |
.hex_to_number(s) ⇒ Object
From hex back to int This routine does no error checking on the input.
581 582 583 |
# File 'lib/hand_record_utility.rb', line 581 def self.hex_to_number(s) s.to_i(16) end |
.number_to_hex(i) ⇒ Object
Given a number, convert to a 24 character hex string. A number should be a maximum of 96 bits. A hex character has 4 bits, so should be a maximum of 24 characters. This routine does no error checking on the input.
575 576 577 |
# File 'lib/hand_record_utility.rb', line 575 def self.number_to_hex(i) i.to_s(16) end |
.number_to_string_64(i) ⇒ Object
From number to character string This routine does no error checking on the input.
590 591 592 593 594 595 596 597 598 599 |
# File 'lib/hand_record_utility.rb', line 590 def self.number_to_string_64(i) return "0" if (i == 0) s = "" while (i > 0) digit = i % 64 s = CHAR_TO_BITS[digit] + s i = i / 64 end s end |
.pavlicek_array_to_andrews_sequences(pav_array) ⇒ Object
Convert board to Andrews number. bridge.thomasoandrews.com/impossible/algorithm.html
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/hand_record_utility.rb', line 448 def self.pavlicek_array_to_andrews_sequences(pav_array) sequences = Array.new( 3 ) { Array.new( 13, -1 ) } (0..2).each do |j| count = 0 loc = 0 (0..51).each do |i| if (j == pav_array[i]) then sequences[j][loc] = count loc = loc + 1 end if (j <= pav_array[i]) then count = count + 1 end end end sequences end |
.pavlicek_array_to_hand(pav_hand_array) ⇒ Object
pav_hand is an array of cards Returns a 17 char string with the hand record.
227 228 229 230 231 232 233 |
# File 'lib/hand_record_utility.rb', line 227 def self.pavlicek_array_to_hand(pav_hand_array) s = "S" + pavlicek_hand_array_to_suit(pav_hand_array[0]) s = s + "H" + pavlicek_hand_array_to_suit(pav_hand_array[1]) s = s + "D" + pavlicek_hand_array_to_suit(pav_hand_array[2]) s = s + "C" + pavlicek_hand_array_to_suit(pav_hand_array[3]) return s end |
.pavlicek_array_to_number(pav_array) ⇒ Object
Given a Pavlicek array, convert to a number Using variable names from www.rpbridge.net/7z68.htm
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/hand_record_utility.rb', line 384 def self.pavlicek_array_to_number(pav_array) routine_name = "pavlicek_array_to_number" debug_routine = 0 n = 13 e = 13 s = 13 w = 13 c = 52 k = D i = 0 card_count = 1 # Go through the 52 cards (0..51).each do |card_count| hand_has_card = pav_array[card_count] if (debug_routine == 1) then # Use a shortened routine name puts "pa2n: count=#{card_count} hand_has_card=#{hand_has_card} c=#{c} k=#{k} i=#{i} n=#{n} e=#{e} s=#{s} w=#{w}" end x = (k * n) / c if (hand_has_card == DIR_N) then n = n - 1 else i = i + x x = (k * e) / c if (hand_has_card == DIR_E) then e = e - 1 else i = i + x x = (k * s) / c if (hand_has_card == DIR_S) then s = s - 1 else i = i + x x = (k * w) / c if (hand_has_card == DIR_W) then w = w - 1 else # Invalid card puts "Invalid direction in HandRecordUtility. direction=#{hand_has_card} count=#{count} value=#{hand_has_card}" end end end end if (debug_routine == 1) then puts "pa2n: end i=#{i} k=#{k} x=#{x} c=#{c} n=#{n} e=#{e} s=#{s} w=#{s}" end k = x c = c - 1 end # The algorithm uses 0..D-1, # The UI uses 1..D so need to increase by 1 return i + 1 end |
.pavlicek_arrays_to_board(hands) ⇒ Object
pavlicek_arrays_to_board takes four arrays and returns the entire board.
215 216 217 218 219 220 221 222 |
# File 'lib/hand_record_utility.rb', line 215 def self.pavlicek_arrays_to_board(hands) board = Hash.new board[:north] = pavlicek_array_to_hand(hands[DIR_N]) board[:east] = pavlicek_array_to_hand(hands[DIR_E]) board[:south] = pavlicek_array_to_hand(hands[DIR_S]) board[:west] = pavlicek_array_to_hand(hands[DIR_W]) return board end |
.pavlicek_hand_array_to_suit(pav_hand_array) ⇒ Object
Given an array holding cards in a suit, return a string with that suit
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/hand_record_utility.rb', line 236 def self.pavlicek_hand_array_to_suit(pav_hand_array) routine_name = "pavlicek_hand_array_to_suit" s = "" return s if (pav_hand_array.nil?) pav_hand_array.each do |card| case card when 0 s = s + "A" when 1 s = s + "K" when 2 s = s + "Q" when 3 s = s + "J" when 4 s = s + "T" when 5..12 c = 14 - card s = s + c.to_s else puts "Invalid value in #{routine_name}. card=#{card}" # Error end end return s end |
.pavlicek_number_to_board(number) ⇒ Object
Default is suited Pavlicek web site uses the ranked order, Andrews reference is to the suited order. Using Andrews web site to generate the numbers, the suited is preferred. The number is assumed to be 1..D, however the algorithm on Pavlicek’s page is 0..D-1. Therefore in a later routine we subtract 1.
89 90 91 |
# File 'lib/hand_record_utility.rb', line 89 def self.pavlicek_number_to_board(number) pavlicek_suited_number_to_board(number) end |
.pavlicek_ranked_number_to_board(number) ⇒ Object
Uses the ranked Pavlicek method
94 95 96 |
# File 'lib/hand_record_utility.rb', line 94 def self.pavlicek_ranked_number_to_board(number) pavlicek_ranked_or_suited_number_to_board(number, PAVLICEK_RANKED) end |
.pavlicek_ranked_or_suited_number_to_board(number, ranked_or_suited) ⇒ Object
Algorithm:
-
N=E=S=W=13; C=52; K=D
-
X=K*N/C; If I < X then N=N-1, go to 6
-
I=I-X; X=K*E/C; If I < X then E=E-1, go to 6
-
I=I-X; X=K*S/C; If I < X then S=S-1, go to 6
-
I=I-X; X=K*W/C; W=W-1
-
K=X; C=C-1, loop if not zero to 2
Returns a board
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/hand_record_utility.rb', line 111 def self.pavlicek_ranked_or_suited_number_to_board(number, ranked_or_suited) # Common theme to help with debugging. Define routine name for debug code. # Sometimes also define a short routine name. # Use a variable to turn debug on or off in the code # Similar code through most of this gem routine_name = "pavlicek_ranked_or_suited_number_to_board" short_routine_name = "prsntb" debug_routine = 0 # Error check if (number <= 0) then puts "Error in #{routine_name}. Invalid number #{number}. Must be 1 or higher." return nil end if (number > HandRecordUtility::D) then puts "Error in #{routine_name}. Invalid number #{number} is too big." return nil end # Using variable names from http://www.rpbridge.net/7z68.htm n = 13 e = 13 s = 13 w = 13 c = 52 k = D # hands[NESW][HCDS][0..12 where 0=A, 1=K, 2=Q, ...12=2] hands = Array.new(4) { Array.new(4) { Array.new } } # The algorithm assumes that the space is 0..D-1 # But the UI assumes 1..D, so decrement by 1. i = number - 1 (0..51).each do |card_count| # if (ranked_or_suited == PAVLICEK_RANKED) then # suit = card_count % 4 # # 0=A, 1=K, 2=Q, .... 12=2 # rank = (card_count / 4).to_i # else suit = (card_count / 13).to_i # 0=A, 1=K, 2=Q, .... 12=2 rank = card_count - (suit * 13) # end # Make sure do it this way and not # x = (k / c) * n x = (k * n) / c if (i < x) then n = n - 1 dir = DIR_N if (debug_routine == 1) then puts "#{short_routine_name}: dir=#{dir} suit=#{suit} rank=#{rank} i=#{i} x=#{x} n=#{n} e=#{e} s=#{s} w=#{w}" end hands[dir][suit] << rank else i = i - x x = (k * e) / c if (i < x) then e = e - 1 dir = DIR_E if (debug_routine == 1) then puts "#{short_routine_name}: dir=#{dir} suit=#{suit} rank=#{rank} i=#{i} x=#{x} n=#{n} e=#{e} s=#{s} w=#{w}" end hands[DIR_E][suit] << rank else i = i - x x = (k * s) / c if (i < x) then s = s - 1 dir = DIR_S if (debug_routine == 1) then puts "#{short_routine_name}: dir=#{dir} suit=#{suit} rank=#{rank} i=#{i} x=#{x} n=#{n} e=#{e} s=#{s} w=#{w}" end hands[dir][suit] << rank else # This is West i = i - x x = (k * w) / c if ((i < x) || ((i == 0) && (x == 0))) then w = w - 1 dir = DIR_W if (debug_routine == 1) then puts "#{short_routine_name}: dir=#{dir} suit=#{suit} rank=#{rank} i=#{i} x=#{x} n=#{n} e=#{e} s=#{s} w=#{w}" end hands[dir][suit] << rank else puts "Error. card_count=#{card_count} i=#{i} x=#{x} k=#{k} c=#{c} n=#{n} e=#{e} s=#{s} w=#{w}" end end end end k = x c = c - 1 end if (debug_routine == 1) then puts "#{routine_name}: end. n=#{n} e=#{e} s=#{s} w=#{w}" end return pavlicek_arrays_to_board(hands) end |
.pavlicek_suited_number_to_board(number) ⇒ Object
Uses the suited Pavlicek method (default)
99 100 101 |
# File 'lib/hand_record_utility.rb', line 99 def self.pavlicek_suited_number_to_board(number) pavlicek_ranked_or_suited_number_to_board(number, PAVLICEK_SUITED) end |
.put_hand_in_pavlicek_array(pav_array, hand, direction, ranked_or_suited) ⇒ Object
Given a hand in format “SKQH5432DAQJCT954” Fill out the pavlicek array
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/hand_record_utility.rb', line 330 def self.put_hand_in_pavlicek_array(pav_array, hand, direction, ranked_or_suited) suit = 0 hand.each_char do |value| if ((value == "S") || (value == "H") || (value == "D") || (value == "C")) then case value when "S" suit = 0 when "H" suit = 1 when "D" suit = 2 when "C" suit = 3 end else # We have a card in a suit card = 14 case value when "A" card_value = 0 when "K" card_value = 1 when "Q" card_value = 2 when "J" card_value = 3 when "T" card_value = 4 else # Map "9" -> 4, "8" -> 5, ... "2" -> 12 card_value = 62 - value.ord # 50=2, 57=9 # card_value = 14 - card_int if (card_value <= 0) then puts "Error in card. card_value=#{card_value} value=#{value}" end end #Error check? # AS=0, AH=1, AD=2, AC=3, ... 2C=51 if (ranked_or_suited == PAVLICEK_RANKED) then card = (card_value * 4) + suit else card = (suit * 13) + card_value end # card should be 0..51 # puts "hand=#{hand} value=#{value} card=#{card} direction=#{direction}" pav_array[card] = direction end end # No return value end |
.string_64_to_number(s) ⇒ Object
Converts the string to an int. This routine does no error checking on the input.
603 604 605 606 607 608 609 610 611 612 613 614 615 616 |
# File 'lib/hand_record_utility.rb', line 603 def self.string_64_to_number(s) i = 0 return i if (s.nil? || s.empty?) s.each_char do |c| chr = c.ord - 48 # Validity check return -1 if ((chr < 0) || (chr > 77)) n = BITS_TO_CHAR[chr] # Validity check return -1 if (n == -1) i = (i * 64) + n end i end |
.to_andrews_number(board) ⇒ Object
476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/hand_record_utility.rb', line 476 def self.to_andrews_number(board) pav_array = board_to_pavlicek_array(board, PAVLICEK_SUITED) sequences = pavlicek_array_to_andrews_sequences(pav_array) seqN = encode_increasing_sequence(sequences[0]) seqE = encode_increasing_sequence(sequences[1]) seqS = encode_increasing_sequence(sequences[2]) # Need to add 1. Internally we use 0..D-1, externally 1..D i = seqS + S_MAX * (seqE + (E_MAX * seqN)) + 1 return i end |
.to_pavlicek_number(board) ⇒ Object
Converts from a board to a Pavlicek number See www.rpbridge.net/7z68.htm Pavlicek ranks the cards in this order: AS, AH, AD, AC, KS.… 2C Algorithm from Richard’s web site
-
N=E=S=W=13; C=52; K=D; I=0
-
X=K*N/C; If N card then N=N-1, go to 6
-
I=I+X; X=K*E/C; If E card then E=E-1, go to 6
-
I=I+X; X=K*S/C; If S card then S=S-1, go to 6
-
I=I+X; X=K*W/C; W=W-1
-
K=X; C=C-1, loop if not zero to 2
275 276 277 |
# File 'lib/hand_record_utility.rb', line 275 def self.to_pavlicek_number(board) to_pavlicek_suited_number(board) end |
.to_pavlicek_ranked_number(board) ⇒ Object
Given a hand, converts to a Pavlicek array
296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/hand_record_utility.rb', line 296 def self.to_pavlicek_ranked_number(board) routine_name = "to_pavlicek_ranked_number" debug_routine = 1 if (debug_routine == 1) then puts "#{routine_name}: Board=#{board}" end pav_array = board_to_pavlicek_array(board, PAVLICEK_RANKED) pav_number = pavlicek_array_to_number(pav_array) # debug_pavlicek_array(pav_array) return pav_number end |
.to_pavlicek_suited_number(board) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/hand_record_utility.rb', line 279 def self.to_pavlicek_suited_number(board) routine_name = "to_pavlicek_suited_number" debug_routine = 0 if (debug_routine == 1) then puts "#{routine_name}: Board=#{board}" end pav_array = board_to_pavlicek_array(board, PAVLICEK_SUITED) # debug_pavlicek_array(pav_array) pav_number = pavlicek_array_to_number(pav_array) # debug_pavlicek_array(pav_array) return pav_number end |