Sample ICM Implementation
Maybe you wondered sometimes: How difficult is it to implement ICM?
As you can see below, its just a few lines of code. This Java-snippet should produce exactly the same results as your favourite ICM Tool. The ICM implementation used for this sites ICM calculator is actually quite similar.
/**
* \param payouts Payout structure, e.g.: new double[]{0.5,0.3,0.2}
* \param stacks Player stacks
* \param player Index of selected player in the stack-array
* \returns ICM equity for selected player
*/
public static
double
getEquity
(
double
[]
payouts,
double
[]
stacks,
int
player
)
{
double
total =
0
;
for
(
int
i =
0
; i < stacks.length; i++
)
total += stacks
[
i
]
;
return
getEquity
(
payouts, stacks.clone
()
, total, player,
0
)
;
}
//Recursive method doing the actual calculation.
private static
double
getEquity
(
double
[]
payouts,
double
[]
stacks,
double
total,
int
player,
int
depth
)
{
double
eq = stacks
[
player
]
/ total * payouts
[
depth
]
;
if
(
depth +
1
< payouts.length
)
for
(
int
i =
0
; i < stacks.length; i++
)
if
(
i != player && stacks
[
i
]
>
0.0
) {
double
c = stacks
[
i
]
;
stacks
[
i
]
=
0.0
;
eq += getEquity
(
payouts, stacks, total - c, player, depth +
1
)
* c / total;
stacks
[
i
]
= c;
}
return
eq;
}
|
|
Code converted with Java2html (java2html.de).
As long as all players have >0 chips, the implementation above is correct. This will be fine for most use-cases. For a fully fledged ICM implementation, we would have
to deal with some special cases that bloat up the code considerably.
e.g.: How to correctly deal with multiple players having zero chips.