darrrrUC
darrrrUC

Reputation: 311

Keeping track of score in a list along with names

Hi again dear StackOverflow! I'm new to learning C# and I'm stuck on a "add score" program.

The basic idea of what I'm trying to do is:

For example:

players = 1
name = bilbo

round1 begins:
score1 = 10
score2 = 10
score3 = 10

round2 begins:
score1 = 20
score2 = 20
score3 = 20

round2 begins:
score1 = 30
score2 = 30
score3 = 30

game ends:
Congratulations bilbo your score was as follows:
round1:
10,10,10
round2:
20,20,20
round3:
30,30,30
Total score: 180

My problem is I do not know how to keep track of each round. The way it works for me now it that it overwrites the score so the only score displayed is the last one. In this case the output would look like

total score: 90 // aka the last 3 scores

How can I keep track on each round and connect that score to the correct player?

I know that there is very little to none validation, but that is not the issue at hand for me :)

Great weekend to all and thanks in before hand for any help given.

Code so far:

class Program
{
  static void Main(string[] args)
  {
    List<Player> players = new List<Player>();
    Console.WriteLine("How many players?");
    int howManyPlayers = Convert.ToInt32(Console.ReadLine());

    for (int i = 0; i < howManyPlayers; i++)
    {
      Console.WriteLine("Name the player");
      Player playa = new Player() { Name = Console.ReadLine()};
      players.Add(playa);
    }

    for (int i = 0; i < 3; i++)
    {
      foreach (var item in players)
      {
        Console.WriteLine("Enter points1 for round {}", i);
        int round1Input = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter points2 for round {}", i);
        int runda2Input = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter points3 for round {}", i);
        int runda3Input = Convert.ToInt32(Console.ReadLine());
        item.totalScore(item.arrow1 = round1Input, 
        item.arrow2 = runda2Input,   item.arrow3 = runda3Input);
      }
    }

    printAllPlayers(players);
  }

  static void printAllPlayers(List<Player> players)
  {
    Console.WriteLine("Printing {0} players ...", players.Count);
    foreach (Player player in players)
    {
      Console.WriteLine("Player: {0} \nScore: {1}", player.Name, player.Score);
    }
  }
}

class Player
{
  public string Name { get; set; }
  public int Score { get; set; }
  public int arrow1 { get; set; }
  public int arrow2 { get; set; }
  public int arrow3 { get; set; }

  public void totalScore(int arrow1, int arrow2, int arrow3)
  {
    Score = arrow1 + arrow2 + arrow3;
  }
}

Upvotes: 2

Views: 3478

Answers (3)

JW Lim
JW Lim

Reputation: 1814

I tried to keep it simple and mostly followed your initial implementation. Let me know if something's unclear.

static void Main(string[] args)
{  
    ...

    for (int i = 0; i < 3; i++) //this loop is for the 3 rounds
    {
        foreach (var player in players) //this one is for all the players
        {
            Console.WriteLine("Player {0}", player.Name); //output the player name

            List<int> roundScores;
            //this is not the best way to do it, but it's easier to understand
            if (j=0) roundScores = player.round1Scores;
            else if (j=1) roundScores = player.round2Scores;
            else if (j=2) roundScores = player.round3Scores;

            Console.WriteLine("Enter points1 for round {}", i + 1);
            roundScores.Add(Convert.ToInt32(Console.ReadLine()));
            Console.WriteLine("Enter points2 for round {}", i + 1);
            roundScores.Add(Convert.ToInt32(Console.ReadLine()));
            Console.WriteLine("Enter points3 for round {}", i + 1);
            roundScores.Add(Convert.ToInt32(Console.ReadLine()));
        }
    }
    printAllPlayers(players);
}

static void printAllPlayers(List<Player> players)
{
    Console.WriteLine("Printing {0} players ...", players.Count);
    foreach (Player player in players)
    {
        player.calcTotalScore(); //calculate the total score for this player
        Console.WriteLine("Player: {0} \nScore: {1}", player.Name, player.Score);
    }
}


class Player
{
    public string Name { get; set; }
    public int Score { get; set; }
    public List<int> round1Scores { get; set; } // I used a List<int> here but you can choose to use an array int[] too
    public List<int> round2Scores { get; set; }
    public List<int> round3Scores { get; set; }

    public void calcTotalScore()
    {
        Score = 0; //initialize the total score.

        for (int i = 0; i < 3; i++)
        {
            Score += round1Scores[i];
            Score += round2Scores[i];
            Score += round3Scores[i];
        }
    }
}

Upvotes: 1

richaux
richaux

Reputation: 2672

As per John W's suggestion:

    static void Main(string[] args)
    {

        List<Player> players = new List<Player>();
        Console.WriteLine("How many players?");
        int howManyPlayers = Convert.ToInt32(Console.ReadLine());

        for (int i = 0; i < howManyPlayers; i++)
        {
            Console.WriteLine("Name the player");
            Player playa = new Player() { Name = Console.ReadLine() };
            players.Add(playa);
        }


        for (int i = 0; i < 3; i++)
        {
            foreach (var item in players)
            {
                // to avoid this repeating code, you could create a Round class
                // populate the Arrow*x* properties in a loop
                // and add the populated Round to the Player.
                Console.WriteLine("Player {0} - Enter points 1 for round {1}", item.Name, i + 1);
                int round1Input = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Player {0} - Enter points 2 for round {1}", item.Name, i + 1);
                int runda2Input = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Player {0} - Enter points 3 for round {1}", item.Name, i + 1);
                int runda3Input = Convert.ToInt32(Console.ReadLine());
                item.Rounds.Add(new Round(round1Input, runda2Input, runda3Input));
            }
        }

        printAllPlayers(players);
    }


    static void printAllPlayers(List<Player> players)
    {
        Console.WriteLine("Printing {0} players ...", players.Count);
        foreach (Player player in players)
        {
            Console.WriteLine("Player: {0} \nScore: {1}", player.Name, player.TotalScore);
        }
        Console.ReadLine();
    }

}

class Round
{
    public Round()
    {
    }

    public Round(int Arrow1, int Arrow2, int Arrow3)
    {
        arrow1 = Arrow1;
        arrow2 = Arrow2;
        arrow3 = Arrow3;
    }

    public int arrow1 { get; set; }
    public int arrow2 { get; set; }
    public int arrow3 { get; set; }
    public int Score
    {
        get
        {
            return arrow1 + arrow2 + arrow3;
        }

    }
}

class Player
{
    public Player()
    {
        Rounds = new List<Round>();
    }

    public string Name { get; set; }
    public List<Round> Rounds { get; set; }

    public int TotalScore
    {
        get
        {
            int score;
            score = 0;
            // iterate through the player's Rounds, summing the Scores
            foreach (var r in Rounds)
            {
                score += r.Score;
            }
            return score;
        }
    }

Upvotes: 3

gudatcomputers
gudatcomputers

Reputation: 2882

Sounds like you need a Game object that has a property that is a List<Round> and a Round might have a Dictionary<string,int> where string is the player name and int is the score . Round could also have a RoundNumber property

Upvotes: 1

Related Questions