C#在游戏中的每个玩家的单独列表中收集点数

时间:2017-08-23 20:22:58

标签: c# list

我正在制作一个游戏,你首先选择有多少玩家,然后你每投掷3个飞镖,你会得到0-20之间的随机分数,你会一直持续到一个玩家超过301分。玩家得到的分数列在一个列表中,如果超过301分,该程序将如何看待。我的问题是,现在应该收集玩家积分的列表会收集所有玩家组合的积分,因此程序在所有玩家达到301积分后停止。我希望程序知道它是哪个玩家并将所有玩家分数放在单独的列表中,以便它可以知道玩家是否达到301分。我对C#比较新,很抱歉,如果我遗漏了一些明显的东西,我似乎无法弄明白。感谢

using System;
using System.Collections.Generic;
using System.Linq;

namespace dart
{
    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 (total_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 :(得分:3)

你可以拥有这样一个玩家类:

class Player
{
      public int Points {get; set;}
      //...some other properties
}

现在Payers可以是List<Player>

List<Player> Players = new List<Player>();

您可以添加您的玩家。 然后你会将每个玩家的点数添加到只有那个玩家:

var player = Players[i];
Player.Point += YourRandomPoint;

点数总和为Players.Sum(p=> p.Points)

所以,如果你想看到所有玩家一起达到301分,你可以得到你的循环:

while(Players.Sum(p=> p.Points<301))
{
    // continue the game
}

或者如果你想看看是否有一个拥有301+积分的玩家,正如@ MistyK在评论中所建议的那样你可以按照以下方式进行循环:

while(!Players.Any(p=> p.Points>=301))
{
    // continue the game
}