Skip to content

Instantly share code, notes, and snippets.

@RoseAndres
Last active October 16, 2023 21:53
Show Gist options
  • Save RoseAndres/af18b2b446a5dac13366fa8b10f602d6 to your computer and use it in GitHub Desktop.
Save RoseAndres/af18b2b446a5dac13366fa8b10f602d6 to your computer and use it in GitHub Desktop.
Create a die-definition by creating an array containing a value for each side of the die. Can be used to generate statistical tables for personal use like https://docs.google.com/spreadsheets/d/1diclXxKa3jpCFKu91dWspjao72d_lEbHyT02spjRVQI/edit?usp=sharing
# d3 from the game 'Bord'
bd3 = [0, 1, 10]
# standard d3 & d6
d3 = [1,2,3]
d6 = [1,2,3,4,5,6]
# generates all possible totals for a given number of a given die-definition
def gen_totals(die, num_dice)
gen_totals_helper(die, num_dice, [])
end
def gen_totals_helper(die, num_dice, global_totals)
return global_totals[num_dice] unless global_totals[num_dice].nil?
local_totals = []
die.each do |die_side|
if num_dice == 1
local_totals << die_side
else
gen_totals(die, num_dice - 1).each do |total|
local_totals << total + die_side
end
end
end
global_totals[num_dice] = local_totals.uniq.sort
end
# counts up all of the victories, losses, and draws between two homogenous pools of dice
#
# draw_victory: when true, draws will count as a victory. when false, draws count as a loss. when nil, draws are counted separately.
def faceoff_odds(roll_1_num_dice, roll_1_die, roll_2_num_dice, roll_2_die, draw_victory: nil)
roll_1_totals = gen_totals(roll_1_die, roll_1_num_dice)
roll_2_totals = gen_totals(roll_2_die, roll_2_num_dice)
total_matchups = roll_1_totals.length * roll_2_totals.length
victories = 0
losses = 0
draws = 0
roll_1_totals.each do |roll_1_total|
roll_2_totals.each do |roll_2_total|
if roll_1_total > roll_2_total
victories += 1
elsif roll_1_total < roll_2_total
losses += 1
else
case draw_victory
when true
victories += 1
when false
losses += 1
else
draws += 1
end
end
end
end
{ victories: victories, losses: losses, draws: draws, total_matchups: total_matchups }
end
# generates a 6x6 2d array of win/loss/draw data for 2 die-definitions
def gen_percentage_table(die_1, die_2, draw_victory: false)
table = {}
(1..6).each do |num_dice_1|
table[num_dice_1] = {}
(1..6).each do |num_dice_2|
table[num_dice_1][num_dice_2] = faceoff_odds(num_dice_1, die_1, num_dice_2, die_2, draw_victory: draw_victory)
end
end
table
end
# prints a table returned from `gen_percentage_table()` in markdown format
def print_percentage_table(table)
print '|-----|'
(1..6).each do |i|
print " #{i} |"
end
puts
print '|-----|'
(1..6).each do |i|
print "-----|"
end
puts
table.each do |num_dice_1, row|
print "| #{num_dice_1} |"
row.each do |num_dice_2, odds|
print " #{ (odds[:victories] * 1.0 / odds[:total_matchups]).round(2) * 100 }% |"
end
puts
end
''
end
# Example Usage
puts 'bd3'
puts print_percentage_table(gen_percentage_table(bd3, bd3)
puts 'd3'
puts print_percentage_table(gen_percentage_table(d3, d3, draw_victory: true)
puts 'd6'
puts print_percentage_table(gen_percentage_table(d6, d6, draw_victory: false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment