- Home /
Voting System Using List (C#)
Good morrow to you all.
I've been making this card game, and encountered a problem that I myself can't solve.
So, there is a deck of cards (list of strings, the game doesn't require anything more complex), and a list of players.
Every player can vote a player once, and in a function I want to calculate how many votes each player gets.
for (int i = 0; i < playerAmount; i++)
{
if (votedPlayers.Contains(players[i]))
{
voteCounts[i]++;
}
}
votedPlayers contains a string for what player the voter voted for.
voteCounts contains an int for how many votes a player gets.
This really isn't the proper way to do it since it does not work, so I am asking if the humble community of Unity would know how to help me.
Best Regards,
Kirei
$$anonymous$$aybe just create a Player class that have an Id and VoteGained? Everytime playerA vote for playerB, you just add 1 to VoteGained in playerB. In the main file, you'll keep a list of Player.
How do the player vote by the way?
Answer by Elisvaldo · Jun 05, 2015 at 08:22 AM
Personally I would create a dictionary to store the voting data.
Assuming the following declarations:
List<string> votedPlayers;
Dictionary<string, int> voteCounts;
Then the following should do the trick:
foreach(string player in votedPlayers){
voteCounts[player] += 1;
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
c# List not fully clearing 0 Answers
Get Count of a List inside a List / Class 1 Answer
How can I assign a UI text and a sprite to variables? 0 Answers
Generic List.Count always gives 0 2 Answers