I'm sorry it took me so long, but I didn't have access to my notebook where the source code is located.
First, it isn't C but Java. I hope this doesn't matter too much. Second, it's basically cheating. What the program does is importing
which contains all single preflop matchups and weighting all possible combinations of two ranges and thus giving a correct equity. Nothing spectacular.
For calculating Ranges like Top10%: It's quit easy to write a function that does that, but you'd have to know how to sort the hands. I.e. by CS-Rankings or their equity against a random hand or ... .
Code:
import java.util.*;
import java.io.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class range {
// CHANGE THE LOCATION AND NAME OF THE FILE, TO WHEREEVER IT'S LOCATED ON YOUR COMPUTER
public static String matchups = new String("/premat.txt");
public static String[] num_to_hand =
{
"AA ", "AKs", "AQs", "AJs", "ATs", "A9s", "A8s", "A7s", "A6s", "A5s", "A4s", "A3s", "A2s", "AKo", "KK ", "KQs", "KJs", "KTs", "K9s", "K8s", "K7s", "K6s", "K5s", "K4s", "K3s", "K2s", "AQo", "KQo", "QQ ", "QJs", "QTs", "Q9s", "Q8s", "Q7s", "Q6s", "Q5s", "Q4s", "Q3s", "Q2s", "AJo", "KJo", "QJo", "JJ ", "JTs", "J9s", "J8s", "J7s", "J6s", "J5s", "J4s", "J3s", "J2s", "ATo", "KTo", "QTo", "JTo", "TT ", "T9s", "T8s", "T7s", "T6s", "T5s", "T4s", "T3s", "T2s", "A9o", "K9o", "Q9o", "J9o", "T9o", "99 ", "98s", "97s", "96s", "95s", "94s", "93s", "92s", "A8o", "K8o", "Q8o", "J8o", "T8o", "98o", "88 ", "87s", "86s", "85s", "84s", "83s", "82s", "A7o", "K7o", "Q7o", "J7o", "T7o", "97o", "87o", "77 ", "76s", "75s", "74s", "73s", "72s", "A6o", "K6o", "Q6o", "J6o", "T6o", "96o", "86o", "76o", "66 ", "65s", "64s", "63s", "62s", "A5o", "K5o", "Q5o", "J5o", "T5o", "95o", "85o", "75o", "65o", "55 ", "54s", "53s", "52s", "A4o", "K4o", "Q4o", "J4o", "T4o", "94o", "84o", "74o", "64o", "54o", "44 ", "43s", "42s", "A3o", "K3o", "Q3o", "J3o", "T3o", "93o", "83o", "73o", "63o", "53o", "43o", "33 ", "32s", "A2o", "K2o", "Q2o", "J2o", "T2o", "92o", "82o", "72o", "62o", "52o", "42o", "32o", "22 "
};
public static int hand_to_num(String hand)
{
if (hand.equals("AA ")) return 0; else if (hand.equals("AKs")) return 1; else if (hand.equals("AQs")) return 2; else if (hand.equals("AJs")) return 3; else if (hand.equals("ATs")) return 4; else if (hand.equals("A9s")) return 5; else if (hand.equals("A8s")) return 6; else if (hand.equals("A7s")) return 7; else if (hand.equals("A6s")) return 8; else if (hand.equals("A5s")) return 9; else if (hand.equals("A4s")) return 10; else if (hand.equals("A3s")) return 11; else if (hand.equals("A2s")) return 12; else if (hand.equals("AKo")) return 13; else if (hand.equals("KK ")) return 14; else if (hand.equals("KQs")) return 15; else if (hand.equals("KJs")) return 16; else if (hand.equals("KTs")) return 17; else if (hand.equals("K9s")) return 18; else if (hand.equals("K8s")) return 19; else if (hand.equals("K7s")) return 20; else if (hand.equals("K6s")) return 21; else if (hand.equals("K5s")) return 22; else if (hand.equals("K4s")) return 23; else if (hand.equals("K3s")) return 24; else if (hand.equals("K2s")) return 25; else if (hand.equals("AQo")) return 26; else if (hand.equals("KQo")) return 27; else if (hand.equals("QQ ")) return 28; else if (hand.equals("QJs")) return 29; else if (hand.equals("QTs")) return 30; else if (hand.equals("Q9s")) return 31; else if (hand.equals("Q8s")) return 32; else if (hand.equals("Q7s")) return 33; else if (hand.equals("Q6s")) return 34; else if (hand.equals("Q5s")) return 35; else if (hand.equals("Q4s")) return 36; else if (hand.equals("Q3s")) return 37; else if (hand.equals("Q2s")) return 38; else if (hand.equals("AJo")) return 39; else if (hand.equals("KJo")) return 40; else if (hand.equals("QJo")) return 41; else if (hand.equals("JJ ")) return 42; else if (hand.equals("JTs")) return 43; else if (hand.equals("J9s")) return 44; else if (hand.equals("J8s")) return 45; else if (hand.equals("J7s")) return 46; else if (hand.equals("J6s")) return 47; else if (hand.equals("J5s")) return 48; else if (hand.equals("J4s")) return 49; else if (hand.equals("J3s")) return 50; else if (hand.equals("J2s")) return 51; else if (hand.equals("ATo")) return 52; else if (hand.equals("KTo")) return 53; else if (hand.equals("QTo")) return 54; else if (hand.equals("JTo")) return 55; else if (hand.equals("TT ")) return 56; else if (hand.equals("T9s")) return 57; else if (hand.equals("T8s")) return 58; else if (hand.equals("T7s")) return 59; else if (hand.equals("T6s")) return 60; else if (hand.equals("T5s")) return 61; else if (hand.equals("T4s")) return 62; else if (hand.equals("T3s")) return 63; else if (hand.equals("T2s")) return 64; else if (hand.equals("A9o")) return 65; else if (hand.equals("K9o")) return 66; else if (hand.equals("Q9o")) return 67; else if (hand.equals("J9o")) return 68; else if (hand.equals("T9o")) return 69; else if (hand.equals("99 ")) return 70; else if (hand.equals("98s")) return 71; else if (hand.equals("97s")) return 72; else if (hand.equals("96s")) return 73; else if (hand.equals("95s")) return 74; else if (hand.equals("94s")) return 75; else if (hand.equals("93s")) return 76; else if (hand.equals("92s")) return 77; else if (hand.equals("A8o")) return 78; else if (hand.equals("K8o")) return 79; else if (hand.equals("Q8o")) return 80; else if (hand.equals("J8o")) return 81; else if (hand.equals("T8o")) return 82; else if (hand.equals("98o")) return 83; else if (hand.equals("88 ")) return 84; else if (hand.equals("87s")) return 85; else if (hand.equals("86s")) return 86; else if (hand.equals("85s")) return 87; else if (hand.equals("84s")) return 88; else if (hand.equals("83s")) return 89; else if (hand.equals("82s")) return 90; else if (hand.equals("A7o")) return 91; else if (hand.equals("K7o")) return 92; else if (hand.equals("Q7o")) return 93; else if (hand.equals("J7o")) return 94; else if (hand.equals("T7o")) return 95; else if (hand.equals("97o")) return 96; else if (hand.equals("87o")) return 97; else if (hand.equals("77 ")) return 98; else if (hand.equals("76s")) return 99; else if (hand.equals("75s")) return 100; else if (hand.equals("74s")) return 101; else if (hand.equals("73s")) return 102; else if (hand.equals("72s")) return 103; else if (hand.equals("A6o")) return 104; else if (hand.equals("K6o")) return 105; else if (hand.equals("Q6o")) return 106; else if (hand.equals("J6o")) return 107; else if (hand.equals("T6o")) return 108; else if (hand.equals("96o")) return 109; else if (hand.equals("86o")) return 110; else if (hand.equals("76o")) return 111; else if (hand.equals("66 ")) return 112; else if (hand.equals("65s")) return 113; else if (hand.equals("64s")) return 114; else if (hand.equals("63s")) return 115; else if (hand.equals("62s")) return 116; else if (hand.equals("A5o")) return 117; else if (hand.equals("K5o")) return 118; else if (hand.equals("Q5o")) return 119; else if (hand.equals("J5o")) return 120; else if (hand.equals("T5o")) return 121; else if (hand.equals("95o")) return 122; else if (hand.equals("85o")) return 123; else if (hand.equals("75o")) return 124; else if (hand.equals("65o")) return 125; else if (hand.equals("55 ")) return 126; else if (hand.equals("54s")) return 127; else if (hand.equals("53s")) return 128; else if (hand.equals("52s")) return 129; else if (hand.equals("A4o")) return 130; else if (hand.equals("K4o")) return 131; else if (hand.equals("Q4o")) return 132; else if (hand.equals("J4o")) return 133; else if (hand.equals("T4o")) return 134; else if (hand.equals("94o")) return 135; else if (hand.equals("84o")) return 136; else if (hand.equals("74o")) return 137; else if (hand.equals("64o")) return 138; else if (hand.equals("54o")) return 139; else if (hand.equals("44 ")) return 140; else if (hand.equals("43s")) return 141; else if (hand.equals("42s")) return 142; else if (hand.equals("A3o")) return 143; else if (hand.equals("K3o")) return 144; else if (hand.equals("Q3o")) return 145; else if (hand.equals("J3o")) return 146; else if (hand.equals("T3o")) return 147; else if (hand.equals("93o")) return 148; else if (hand.equals("83o")) return 149; else if (hand.equals("73o")) return 150; else if (hand.equals("63o")) return 151; else if (hand.equals("53o")) return 152; else if (hand.equals("43o")) return 153; else if (hand.equals("33 ")) return 154; else if (hand.equals("32s")) return 155; else if (hand.equals("A2o")) return 156; else if (hand.equals("K2o")) return 157; else if (hand.equals("Q2o")) return 158; else if (hand.equals("J2o")) return 159; else if (hand.equals("T2o")) return 160; else if (hand.equals("92o")) return 161; else if (hand.equals("82o")) return 162; else if (hand.equals("72o")) return 163; else if (hand.equals("62o")) return 164; else if (hand.equals("52o")) return 165; else if (hand.equals("42o")) return 166; else if (hand.equals("32o")) return 167; else if (hand.equals("22 ")) return 168; else
return -1;
}
public static String[] filetostring(String datei){
String[] tmp;
int numlines = 0;
File file = null;
file = new File (datei);
try {
FileReader file_reader = new FileReader (file);
BufferedReader buf_reader = new BufferedReader (file_reader);
do {
String line = buf_reader.readLine ();
if (line == null) break;
numlines++;
} while (true);
buf_reader.close ();
}
catch (IOException e) {
System.out.println("E");
}
tmp = new String[numlines];
numlines=0;
file = new File (datei);
try {
FileReader file_reader = new FileReader (file);
BufferedReader buf_reader = new BufferedReader (file_reader);
do {
String line = buf_reader.readLine ();
if (line == null) break;
tmp[numlines]=line;
numlines++;
} while (true);
buf_reader.close ();
}
catch (IOException e) {
System.out.println("E");
}
return tmp;
}
public static double[][] eq = new double[169][169];
public static int[][] possi = new int[169][169];
public static void make_eq_array()
{
String[] hands = new String[169];
for (int lz=0; lz<169; lz++) hands[lz]="";
int index = 0;
String[] data = filetostring(matchups);
for (int lz=0 ; lz<data.length; lz++)
{
int h1 = hand_to_num(data[lz].substring(0,3));
int h2 = hand_to_num(data[lz].substring(8,11));
double eq1 = Double.parseDouble(data[lz].substring(15,23));
double eq2 = Double.parseDouble(data[lz].substring(28,36));
boolean switched = false;
if (h1<h2)
{
switched = true;
int tmp = h1;
h1 = h2;
h2 = tmp;
}
eq[h1][h2] = eq2;
eq[h2][h1] = eq1;
}
for (int lz=0; lz<169; lz++) eq[lz][lz] = 0.5;
for (int lz1 = 0; lz1 < 169; lz1++)
for (int lz2 = 0; lz2 < 169; lz2++)
{
String h1 = num_to_hand[lz1];
String h2 = num_to_hand[lz2];
// second hand is paired
if (h2.charAt(0)==h2.charAt(1))
{
possi[lz1][lz2] = 6;
if ((h2.charAt(0) == h1.charAt(0)) || (h2.charAt(0) == h1.charAt(1))) possi[lz1][lz2] = 3;
if ((h2.charAt(0) == h1.charAt(0)) && (h2.charAt(0) == h1.charAt(1))) possi[lz1][lz2] = 1;
}
else
// second hand is sooted
if (h2.indexOf("s") != -1)
{
possi[lz1][lz2] = 4;
// first hand is paired and overlaps with second
if (h1.charAt(0)==h1.charAt(1) && (h1.charAt(0)==h2.charAt(1) || h1.charAt(0)==h2.charAt(0)) ) possi[lz1][lz2] = 2;
// first hand is sooted and overlaps with second
if ((h1.indexOf("s") != -1) && (h1.charAt(0)==h2.charAt(0) || h1.charAt(0)==h2.charAt(1) || h1.charAt(1)==h2.charAt(0) || h1.charAt(1)==h2.charAt(1))) possi[lz1][lz2] = 3;
// first hand is unsooted and overlaps once with second
if ((h1.indexOf("o") != -1) && (h1.charAt(0)==h2.charAt(0) || h1.charAt(0)==h2.charAt(1) || h1.charAt(1)==h2.charAt(0) || h1.charAt(1)==h2.charAt(1))) possi[lz1][lz2] = 3;
// first hand is unsooted and overlaps twice with second
if ((h1.indexOf("o") != -1) && ((h1.charAt(0)==h2.charAt(0) || h1.charAt(1)==h2.charAt(0)) && (h1.charAt(0)==h2.charAt(0) && h1.charAt(1)==h2.charAt(1)))) possi[lz1][lz2] = 2;
}
// second hand is unsooted
if (h2.indexOf("o") != -1)
{
possi[lz1][lz2] = 12;
// first hand is paired and overlaps with second
if (h1.charAt(0)==h1.charAt(1) && (h1.charAt(0)==h2.charAt(1) || h1.charAt(0)==h2.charAt(0)) ) possi[lz1][lz2] = 6;
// first hand is sooted and overlaps once with second
if ((h1.indexOf("s") != -1) && ((h1.charAt(0)==h2.charAt(0) || h1.charAt(1)==h2.charAt(0)) && (h1.charAt(0)==h2.charAt(0) && h1.charAt(1)==h2.charAt(1)))) possi[lz1][lz2] = 6; else
// first hand is sooted and overlaps twice with second
if ((h1.indexOf("o") != -1) && ((h1.charAt(0)==h2.charAt(0) || h1.charAt(1)==h2.charAt(0)) && (h1.charAt(0)==h2.charAt(0) && h1.charAt(1)==h2.charAt(1)))) possi[lz1][lz2] = 7; else
// first hand is unsooted and overlaps once with second
if ((h1.indexOf("s") != -1) && (h1.charAt(0)==h2.charAt(0) || h1.charAt(0)==h2.charAt(1) || h1.charAt(1)==h2.charAt(0) || h1.charAt(1)==h2.charAt(1))) possi[lz1][lz2] = 9; else
// first hand is unsooted and overlaps twice with second
if ((h1.indexOf("o") != -1) && (h1.charAt(0)==h2.charAt(0) || h1.charAt(0)==h2.charAt(1) || h1.charAt(1)==h2.charAt(0) || h1.charAt(1)==h2.charAt(1))) possi[lz1][lz2] = 9;
}
}
}
public static double match_range(String r1, String r2)
{
int sumpossi = 0;
double sumev = 0;
for (int lz1=0; lz1<169; lz1++)
for (int lz2=0; lz2<169; lz2++)
{
if ( ( (r1.indexOf(num_to_hand[lz1])!=-1) && (r2.indexOf(num_to_hand[lz2])!=-1) ) )
{
sumpossi+=possi[lz1][lz2];
sumev+=eq[lz1][lz2] * possi[lz1][lz2];
//System.out.println(num_to_hand[lz1]+" "+num_to_hand[lz2]+" : "+possi[lz1][lz2]+" "+eq[lz1][lz2]);
}
}
if (sumpossi==0) return 0;
else
return (sumev*1.0 / sumpossi);
}
public static void main (String args[])
{
make_eq_array();
// make sure to leave a space after each hand
System.out.println(match_range("AA KK QQ JJ TT AKs AKo ", "AKs AKo "));
System.out.println(match_range("AKo AKs AQo AQs AJo AJs ", "JTs T9s 98s 87s 76s "));
}
}