At the risk of describing a "solution" that uses some features of C# that many newcomers to the language may have not encountered, or understood how to use, I'm going to offer some code here:
using System.Collections.Generic;
using System.Linq;
public struct MatchData
{
public int ID;
public int player1Score;
public int player2Score;
public MatchData(int id, int s1, int s2)
{
ID = id;
player1Score = s1;
player2Score = s2;
}
}
private Dictionary<int,> CalculatePossibleMatches(int p1Start, int p1MaxPointsToAdd, int p2Start, int p2MaxPointsToAdd)
{
IEnumerable<int> player1Scores = Enumerable.Range(p1Start, p1MaxPointsToAdd + 1);
IEnumerable<int> player2Scores = Enumerable.Range(p2Start, p2MaxPointsToAdd + 1);
var dctMatchIDToScores = new Dictionary<int,>();
int currentMatchID = 0;
foreach (int score1 in player1Scores)
{
foreach (int score2 in player2Scores)
{
dctMatchIDToScores.Add(++currentMatchID, new MatchData(currentMatchID, score1, score2));
}
}
return dctMatchIDToScores;
}
Now: test by executing this code in some Method, or EventHandler:
Dictionary<int,> Matches = CalculatePossibleMatches(10, 4, 11, 1);
foreach (var kvp in Matches)
{
MatchData scores = kvp.Value;
Console.WriteLine("Match: #{0} Player 1: {1} Player2: {2}", kvp.Key, scores.player1Score,
scores.player2Score);
}
Observe the result in the 'Output window (WinForms), or Console. Put a break-point in the test code before the 'foreach loop and examine the value of the Dictionary object returned from 'CalculatePossibleMatches.
Comments:
1. we use Linq's strongly typed IEnumerable here to hold a Range of possible scores, given the input data. The Enumerable.Range method returns a sequence; that sequence must be stored in a variable of Type IEnumerable ... don't ask me why, ask Microsoft :)
2. one 'foreach loop inside another 'foreach loop does the right thing to access every possible pair of scores.
3. a new instance of the 'MatchData struct is created, the data for each Match is inserted into it via its constructor, and a new entry is made in the Dictionary with the Key set to the Match ID number, and the Value set to the instance of MatchData.
Good questions to ask:
1. what are the benefits of building a Dictionary, and using a 'Struct ?
a. we can then use Linq in very powerful ways to query that Dictionary, and produce summary results of any type, or select items based on one, or many, criteria.
b. imho, the techniques shown here clearly express the "intent" of the code, that is the "nature" of problem-solving required.