Class: PokerEval

Inherits:
Object
  • Object
show all
Defined in:
lib/poker_eval.rb,
ext/poker_eval_api/poker_eval.c

Overview

define version string to be used internally for the Gem by Hoe.

Defined Under Namespace

Modules: GemVersion

Constant Summary collapse

@@rankChars =
"23456789TJQKA"
@@suitChars =
"hdcs"
@@suitBase =
13

Class Method Summary collapse

Class Method Details

.best(args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/poker_eval.rb', line 30

def self.best args
  results = self.eval_hand(args)
  results["combination"].each_with_index do |i, index|
    if index > 0
      results["combination"][index] = card2string(i)
    end
  end

  results
end

.card2string(index) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/poker_eval.rb', line 16

def self.card2string index
  index = index.to_i
  card = ""
  if index == 255
    card = "__"
  elsif index < 52
    card = @@rankChars[index % @@suitBase] + @@suitChars[index / @@suitBase]
  else
    raise 'Unexisting card index given: ' + index.to_s
  end

  card
end

.eval(args) ⇒ Object



799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
# File 'ext/poker_eval_api/poker_eval.c', line 799

static VALUE
t_eval(VALUE self, VALUE args)
{
  int i;
  int pockets_size;
  int iterations = 0;
  VALUE rbpockets = 0;
  VALUE rbboard = 0;
  VALUE rbdead = 0;
  VALUE rbiterations = 0;
  char* game = 0;
  enum_gameparams_t* params = 0;

  game = RSTRING_PTR(rb_hash_aref(args, rb_str_new2("game")));
  rbpockets = rb_hash_aref(args, rb_str_new2("pockets"));
  rbboard = rb_hash_aref(args, rb_str_new2("board"));
  rbdead = rb_hash_aref(args, rb_str_new2("dead"));
  rbiterations = rb_hash_aref(args, rb_str_new2("iterations"));

  if( !NIL_P(rbiterations))
  {
    iterations = FIX2INT(rbiterations);
  }

  StdDeck_CardMask pockets[ENUM_MAXPLAYERS];
  int numToDeal[ENUM_MAXPLAYERS];
  CardMask dead_cards;
  CardMask board_cards;

  VALUE result = 0;

  if(!strcmp(game, "holdem")) {
    params = enumGameParams(game_holdem);
  } else if(!strcmp(game, "holdem8")) {
    params = enumGameParams(game_holdem8);
  } else if(!strcmp(game, "omaha")) {
    params = enumGameParams(game_omaha);
  } else if(!strcmp(game, "omaha8")) {
    params = enumGameParams(game_omaha8);
  } else if(!strcmp(game, "7stud")) {
    params = enumGameParams(game_7stud);
  } else if(!strcmp(game, "7stud8")) {
    params = enumGameParams(game_7stud8);
  } else if(!strcmp(game, "7studnsq")) {
    params = enumGameParams(game_7studnsq);
  } else if(!strcmp(game, "razz")) {
    params = enumGameParams(game_razz);
  } else if(!strcmp(game, "5draw")) {
    params = enumGameParams(game_5draw);
  } else if(!strcmp(game, "5draw8")) {
    params = enumGameParams(game_5draw8);
  } else if(!strcmp(game, "5drawnsq")) {
    params = enumGameParams(game_5drawnsq);
  } else if(!strcmp(game, "lowball")) {
    params = enumGameParams(game_lowball);
  } else if(!strcmp(game, "lowball27")) {
    params = enumGameParams(game_lowball27);
  }

  if(params == 0)
    rb_fatal("game %s is not a valid value (holdem, holdem8, omaha, omaha8, 7stud, 7stud8, 7studnsq, razz, 5draw, 5draw8, 5drawnsq, lowball, lowball27)", game);

  if (TYPE(rbpockets) != T_ARRAY)
    rb_fatal("pockets must be list");

  pockets_size = RARRAY_LENINT(rbpockets);

  {
    for(i = 0; i < pockets_size; i++) {
      int count;
      CardMask_RESET(pockets[i]);
      VALUE rbpocket = rb_ary_entry(rbpockets, i);

      count = rbList2CardMask(rbpocket, &pockets[i]);

      if(count < 0)
        goto err;
      if(count < RARRAY_LEN(rbpocket))
        numToDeal[i + 1] = RARRAY_LENINT(rbpocket) - count;
      else
        numToDeal[i + 1] = 0;
    }
  }


  {
    int count;
    count = rbList2CardMask(rbboard, &board_cards);
    if(count < 0)
      goto err;
    if(count < RARRAY_LENINT(rbboard))
      numToDeal[0] = RARRAY_LENINT(rbboard) - count;
    else
      numToDeal[0] = 0;
  }

  if(!NIL_P(rbdead) && RARRAY_LEN(rbdead) > 0) {
    if(rbList2CardMask(rbdead, &dead_cards) < 0){
      rb_fatal("dead cards error");
    }
  }
  else {
      CardMask_RESET(dead_cards);
  }

  {
    enum_result_t cresult;
    int err;
    memset(&cresult, '\0', sizeof(enum_result_t));

    if(iterations > 0) {
      err = rbenumSample(params->game, pockets, numToDeal, board_cards, dead_cards, pockets_size + 1, iterations, &cresult);
    } else {
      err = rbenumExhaustive(params->game, pockets, numToDeal, board_cards, dead_cards, pockets_size + 1, &cresult);
    }
    if(err != 0) {
      rb_fatal("poker-eval: rbenum returned error code %d", err);
    }

    result = rb_hash_new();

    VALUE info = rb_hash_new(); 
    rb_hash_aset(info, rb_str_new2("samples"), INT2NUM(cresult.nsamples));
    rb_hash_aset(info, rb_str_new2("haslopot"), INT2NUM(params->haslopot));
    rb_hash_aset(info, rb_str_new2("hashipot"), INT2NUM(params->hashipot));

    rb_hash_aset(result, rb_str_new2("info"), info);

    VALUE list = rb_ary_new();
    for(i = 0; i < pockets_size; i++) {
      VALUE tmp = rb_hash_new(); 
      rb_hash_aset(tmp, rb_str_new2("scoop"), INT2NUM(cresult.nscoop[i]));
      rb_hash_aset(tmp, rb_str_new2("winhi"), INT2NUM(cresult.nwinhi[i]));
      rb_hash_aset(tmp, rb_str_new2("losehi"), INT2NUM(cresult.nlosehi[i]));
      rb_hash_aset(tmp, rb_str_new2("tiehi"), INT2NUM(cresult.ntiehi[i]));
      rb_hash_aset(tmp, rb_str_new2("winlo"), INT2NUM(cresult.nwinlo[i]));
      rb_hash_aset(tmp, rb_str_new2("loselo"), INT2NUM(cresult.nloselo[i]));
      rb_hash_aset(tmp, rb_str_new2("tielo"), INT2NUM(cresult.ntielo[i]));
      rb_hash_aset(tmp, rb_str_new2("ev"), INT2NUM((cresult.ev[i] / cresult.nsamples) * 1000));
      rb_ary_push(list, tmp);
      tmp = 0;
    }
    rb_hash_aset(result, rb_str_new2("eval"), list);
  }

err:
  return result;
}

.eval_hand(args) ⇒ Object



705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
# File 'ext/poker_eval_api/poker_eval.c', line 705

static VALUE
t_eval_hand(VALUE self, VALUE args)
{
  VALUE result = 0;
  VALUE rbboard = 0;
  VALUE rbhand = 0;
  char* hilow_string = 0;

  hilow_string = RSTRING_PTR(rb_hash_aref(args, rb_str_new2("side")));
  rbboard = rb_hash_aref(args, rb_str_new2("board"));
  rbhand = rb_hash_aref(args, rb_str_new2("hand"));

  int low = 0;
  CardMask hand;
  CardMask board;
  int board_size = 0;
  CardMask best;
  HandVal best_handval;

  StdDeck_CardMask_RESET(best);

  if(!strcmp(hilow_string, "low")) {
    low = 1;
  }

  if(rbList2CardMask(rbhand, &hand) < 0)
    rb_fatal("empty hand given");

  if( !NIL_P(rbboard))
  {
    board_size = rbList2CardMask(rbboard, &board);
  }

  if(board_size > 0) {
    CardMask hicards;
    CardMask locards;
    HandVal  hival = 0;
    HandVal  loval = 0;
    StdDeck_CardMask_RESET(hicards);
    StdDeck_CardMask_RESET(locards);
    OmahaHiLow8_Best(hand, board, &hival, &loval, &hicards, &locards);
    if(low) {
      best_handval = loval;
      if(best_handval != LowHandVal_NOTHING)
	best = locards;
    } else {
      best = hicards;
      best_handval = hival;
    }
  } else {
    CardMask cards;
    CardMask dead;

    StdDeck_CardMask_RESET(best);

    StdDeck_CardMask_RESET(dead);
    StdDeck_CardMask_OR(dead, dead, hand);
    StdDeck_CardMask_NOT(dead, dead);

    if(low) {
      best_handval = LowHandVal_NOTHING;
    } else {
      best_handval = HandVal_NOTHING;
    }

    ENUMERATE_N_CARDS_D(cards, 5, dead, 
    {
      HandVal handval;

      if(low) {
	handval = Hand_EVAL_LOW8(cards, 5);
      } else {
	handval = Hand_EVAL_N(cards, 5);
      }

      if(low ? (handval < best_handval) : (handval > best_handval)) {
	best = cards;
	best_handval = handval;
      }
    }
			);
  }

  if(StdDeck_CardMask_IS_EMPTY(best)) {
    best_handval = low ? 0x0FFFFFFF : 0;
  }

  result = rb_hash_new();
  rb_hash_aset(result, rb_str_new2("value"), INT2NUM(best_handval));
  rb_hash_aset(result, rb_str_new2("combination"), CardMask2SortedRbList(best, low));

  return result;
}

.winner(args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/poker_eval.rb', line 41

def self.winner args
  index2index = {}
  normalized_pockets = []
  normalized_index = 0
  pockets = args["pockets"]

  pockets.each_with_index do |pocket, index|
    if !args["fill_pockets"]
      if pocket.find_index("__")
        pockets[index] = []
      end
    end

    if !pockets[index].empty?
      normalized_pockets << pockets[index]
      index2index[index] = normalized_index
      normalized_index += 1
    end
  end

  args["pockets"] = normalized_pockets
  results = self.eval(args)


  count = results["info"]["samples"]
  haslopot = results["info"]["haslopot"]
  hashipot = results["info"]["hashipot"]

  winners = { 'low' => [], 'hi' => [] }

  pockets.each_with_index do |pocket, index|
    if !index2index[index].nil?
      result = results["eval"][index2index[index]]
      if result["winhi"] == 1 or result["tiehi"] == 1
          winners["hi"] << index
      end
      if result["winlo"] == 1 || result["tielo"] == 1
        winners["low"] << index
      end
    end
  end

  if !haslopot || winners["low"].empty?
    winners.delete("low")
  end

  if !hashipot
    winners.delete("hi")
  end

  return winners

end