0

I am trying to create a draft order for a game I'm working on but I cannot get it to function how I need it to. I need 5 rounds with each round number properly and each pick should be numbered 1-10 for each round. Here is the code I'm working with:

List<Draft> _draftorder = new List<Draft>();

foreach (Team t in teams)
{
    for (int i = 1; i <= 5; i++)
    {
        _draftorder.Add(new Draft()
        {
            city = t.city,
            round = i++,
            pick = i++,
        });
    }
}

Any help is appreciated!

10
  • 3
    each i++ increases the value stored in i, so you are adding 1 to it three times within the for loop Commented Jun 20, 2018 at 21:17
  • So Round and Pick should always be the same per Draft object? Commented Jun 20, 2018 at 21:20
  • So I need a second for loop for the "picks"? Commented Jun 20, 2018 at 21:20
  • @RobertCole it would be much easier if you gave us a sample output, instead of us trying to guess Commented Jun 20, 2018 at 21:20
  • 1
    Why would your Draft list not include the team? Commented Jun 20, 2018 at 21:22

2 Answers 2

1

I find the logic is easier to follow when the code matches the stepwise workflow of the requirements, and the variable names match the concepts in the requirements.

List<Draft> _draftorder = new List<Draft>();

for (int currentRound=1; currentRound<=10; currentRound++)
{
    int pickOrder = 1;
    foreach (Team t in teams)
    {
        _draftorder.Add(new Draft()
        {
            city = t.city,
            round = currentRound,
            pick = pickOrder++
        });
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

He changed his requirement: Now I need to find a way to randomly assign a different pick number to each team instead of it counting up to 5.
Not the way I read it. I understand it to mean each round has its own namespace with respect to pick, between 1 and 10 each round. I'm assuming there are ten teams.
@JohnWu yeah I just re-read the question. I swear it was different at some point. Idk
0

You need to add an additional for loop and put the foreach loop inside the first for loop:

List<Draft> _draftorder = new List<Draft>();

// Allow 5 rounds
for (int i = 1; i <= 5; i++)
{
    // Allow every team to pick some drafts
    foreach (Team t in teams)
    {
        // Limit them to a specific amount of picks per round
        for (int j = 1; j <= 10; j++)
        {
            _draftorder.Add(new Draft()
            {
                city = t.city,
                round = i,
                pick = j,
            });
        }
    }
}

4 Comments

This helped but its not doing exactly what I need. I was able to change round = i++ to get 5 picks for each team. I also changed j <= 10 to 5 so that teams don't get too many picks. Now I need to find a way to randomly assign a different pick number to each team instead of it counting up to 5.
@RobertCole Stick to the original question, which did not mention anything about "random" picks.
Actually, I said that I need picks numbered 1-10. Each team must have the same number pick per round. If I left that detail out then my bad. But I did say each round should be numbered 1-10.
@RobertCole, I updated my answer based on your comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.