C#如何从列表中获取播放器,以便打印出他们的分数

时间:2017-08-24 18:32:59

标签: c# list

我做了一个游戏,你首先说出有多少玩家,然后每个玩家投掷3个飞镖并且循环直到一个玩家得到301分,现在我想得到的玩家从名单中得到超过301分。球员和我想得到他们的分数,所以我可以打印他们的分数,并说他们在比赛结束时赢了但我无法弄清楚如何做到这一点。感谢

class Program
{
    public static void Main(string[] args)
    {
        Game My_game = new Game();
        My_game.PlayGame();
    }
}

public class Game
{
    private List<Player> player_list = new List<Player>();
    private List<int> total_list = new List<int>();

    public void PlayGame()
    {
        Random random_number = new Random();
        int throw1;
        int throw2;
        int throw3;

        string more_players = "yes";

        while (more_players == "yes")
        {
            Console.WriteLine("What is the players name?: ");
            player_list.Add(new Player(Console.ReadLine()));
            Console.WriteLine("Are there more players?");
            more_players = Console.ReadLine();
        }

        Console.WriteLine("\n\n Welcome to the dartgame! \n" +
                          "\n Game Rules: Each player throws 3 darts at a time." +
                          "\n Every throw can be worth 0-20 points." +
                          "\n Whoever gets 301 points first is the winner!");

        Console.WriteLine("\nPlayers:");

        foreach (var players in player_list)
        {
            Console.WriteLine(players);
        }

        int total_points = 0;

        while (!player_list.Any(x => x.calculate_points() >= 301))
        {
            foreach (var players in player_list)
            {
                Console.WriteLine("\n\n\n\n");
                Console.WriteLine("\n first throw for{0}!", players);
                Console.WriteLine("Press space to throw a dart!");
                Console.ReadKey();
                throw1 = random_number.Next(1, 20);
                Console.WriteLine("You got " + throw1 + " points!");
                Console.WriteLine("\n second throw for{0}!", players);
                Console.WriteLine("Press space to throw a dart!");
                Console.ReadKey();
                throw2 = random_number.Next(1, 20);
                Console.WriteLine("You got " + throw2 + " points!");
                Console.WriteLine("\n third throw for{0}!", players);
                Console.WriteLine("Press space to throw a dart!");
                Console.ReadKey();
                throw3 = random_number.Next(1, 20);
                Console.WriteLine("You got " + throw3 + " points!");
                total_points = throw1 + throw2 + throw3;
                Console.WriteLine("\nPoints for this round: " + total_points);
                total_list.Add(total_points);
                total_points = total_list.Sum(x => Convert.ToInt32(x));
                players.Add_turn(throw1, throw2, throw3);
            }

            foreach (var players in player_list)
            {
                players.print_turns();
            }
        }
    }

}

class Player
{
    private string name;
    private List<Turns> turn_list = new List<Turns>();
    public Player(string _name)
    {
        name = _name;

    }

    public void Add_turn(int turn1, int turn2, int turn3)
    {
        turn_list.Add(new Turns(turn1, turn2, turn3));
    }

    public int calculate_points()
    {
        int total = 0;
        foreach (var turns in turn_list)
        {
            total = total + turns.Get_Score();
        }
        return total;
    }

    public void print_turns()
    {
        Console.WriteLine("\n\n\n\n----------------------------------------");
        Console.WriteLine("Points for {0}", name);
        foreach (var turns in turn_list)
        {
            Console.WriteLine(turns);
        }
        Console.WriteLine("\n Total points: {0}", calculate_points());
    }


    public override string ToString()
    {
        return string.Format(" {0} ", name);
    }
}

class Turns
{
    private int turn1;
    private int turn2;
    private int turn3;

    public Turns(int _turn1, int _turn2, int _turn3)
    {
        turn1 = _turn1;
        turn2 = _turn2;
        turn3 = _turn3;
    }

    public int Get_Score()
    {
        int totalt;
        totalt = turn1 + turn2 + turn3;
        return totalt;
    }

    public override string ToString()
    {
        return string.Format("\n throw 1: {0} \n throw 2: {1} \n throw 3: {2}", turn1, turn2, turn3);
    }
}

1 个答案:

答案 0 :(得分:2)

嗯,这就是已经检查任何玩家是否符合条件的方式:

player_list.Any(x => x.calculate_points() >= 301)

所以只要一个 符合条件,就可以获得该单人游戏:

player_list.Single(x => x.calculate_points() >= 301)

或者所有匹配的玩家,如果不止一个:

player_list.Where(x => x.calculate_points() >= 301)

或者只是第一个匹配的玩家,如果有多个但你只想要一个:

player_list.First(x => x.calculate_points() >= 301)

或者也许是得分最高的球员(不考虑平局得分):

player_list.OrderByDescending(x => x.calculate_points()).First();