I am trying to setup an odds calculator for a best of 7 series, given independent odds for each event.
The following code works, but I would like to add recursion to simplify the end.
public class Game
{
public int No { get; set; }
public List<decimal> Odds;
public Game(int no, decimal odd1, decimal odd2)
{
No = no;
Odds = new List<decimal>();
Odds.Add(odd1);
Odds.Add(odd2);
}
}
void Main()
{
var games = new List<Game>();
var homeodd = .6m;
var awayodd = .4m;
var winningOdds = 0m;
//Add 7 games each with a different odd of winning game
games.Add(new Game(1,homeodd,awayodd));
games.Add(new Game(2,homeodd,awayodd));
games.Add(new Game(3,awayodd,homeodd));
games.Add(new Game(4,awayodd,homeodd));
games.Add(new Game(5,awayodd,homeodd));
games.Add(new Game(6,homeodd,awayodd));
games.Add(new Game(7,homeodd,awayodd));
//game one has 2 possible outcomes, 0 = win, 1 = loss =>same for all 7 games
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
for (int l = 0; l < 2; l++)
{
for (int m = 0; m < 2; m++)
{
for (int n = 0; n < 2; n++)
{
for (int o = 0; o < 2; o++)
{
if ((i+j+k+l+m+n+o)<4) //if we have loss less than 4 games, we have won the series and we want to add the odds of that possibility to the total odds
{
winningOdds += games[0].Odds[i] * games[1].Odds[j] * games[2].Odds[k] * games[3].Odds[l] * games[4].Odds[m] * games[5].Odds[n] * games[6].Odds[o];
}
}
}
}
}
}
}
}
Console.WriteLine(winningOdds);
}
for
statements. what is inside the if statement will only run when all the variables are=0
. is that intended? \$\endgroup\$