summaryrefslogtreecommitdiffhomepage
path: root/odds_testing/cards.rb
blob: 03c8d3f99dab29ff0cf07f718a0e84bae3a683e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
class Array
  def deep_copy
    Marshal.load(Marshal.dump(self))
  end
  def mean
    return 0 if self.length.zero?
    self.sum.to_f / self.length.to_f
  end
  def median
    return 0 if self.length.zero?
    sorted = self.sort
    mid = (sorted.length - 1).to_f / 2.0
    (sorted[mid.floor] + sorted[mid.ceil]).to_f / 2.0
  end
  def mode
    return 0 if self.length.zero?
    self.group_by(&:itself).values.max_by(&:size).first.to_f
  end
end

suits = [:s,:c,:h,:d]
ten = (1..10).to_a
courts = [:k, :q, :r, :j]
@cards = suits.map do |suit|
  ten.map do |num|
    [suit, num.to_s]
  end + courts.map do |court|
    [suit, court]
  end
end
@cards.flatten!

def test_multi(iterations, *block_params, &block)
  results = []
  iterations.times do
    results.push block.call(*block_params)
  end
  "Mean:   " + results.mean.to_s +
    "\nMedian: " + results.median.to_s +
    "\nMode:   " + results.mode.to_s +
    "\nDistribution:\n" +
    results.group_by(&:itself).map do |key, value|
      [key, value.size.to_f / results.size]
    end.to_h.sort_by do |key, value|
      [-value, key]
    end.to_h.to_s
end

basic_match = ->(player_hand_size = 3, dm_hand_size = 3) do
  dm_deck = @cards.shuffle
  player_deck = @cards.shuffle

  dm_hand = dm_deck[0...dm_hand_size]
  player_hand = player_deck[0...player_hand_size]
  dup_dm_hand = dm_hand.clone
  dup_player_hand = player_hand.clone
  matches = 0

  dm_hand.reverse_each do |dm_card|
    card_paired = false
    player_hand.reverse_each do |player_card|
      if player_card == dm_card
        dm_hand.delete dm_card
        player_hand.delete player_card
        card_paired = true
        matches += 1
        next
      end
      next if card_paired
    end
  end

  matches
end
#test_multi(20_000, 3, 3, &basic_match)
result = ""
3.times do |play|
  6.times do |dm|
    play_mod = (play + 2)
    dm_mod = (dm + 1)
    result += "player: #{play_mod}\n"
    result += "dm    : #{dm_mod}\n"
    result += test_multi(200_000, dm_mod, play_mod, &basic_match) + "\n\n"
  end
end

File.write('temp.txt', result)