Need help with many classes

rabe01

New member
Joined
Mar 4, 2020
Messages
2
Programming Experience
3-5
hi!

so I'm trying to do a project where you can create a player and then the player will be put in a team and save it in a text file.
so right now I'm able to create a player and place it inside the list of class player and save it in a text file.

but now I'm kinda stuck. Do I want another class who takes care of all the teams? and then the class will get the list from players? (like this public List<Player> Players { get; set; } ?)
do anyone knows how to do it?

ask me if something is unclear :)
 
Your first post on the forums. Welcome

Of course we know how to do it. But we aren't going to write it out for you. If you show what you've tried, thought of and wrote so far, we can give you pointers and direction.
 
no don't write it for me, I wanna learn this :) so right now I can create a player ( I have to type in name, age, position, shirt number and then which team I want the player to go to, player.Add(new Player(name, age, pos, num, team)); so the player will be put in private static List<Player> player = new List<Player>(); and the player will show up inside my txt file players.txt and when I start up the program I can print all my players out again. But now to my actual problem. What now? I have tried to do another class called team, but do I need another txt file for just the teams? do I need a list called teams? and then I manually put in all the teams? Right now I have my teams like this inside my player class: static public readonly string[] teams = { "liverpool", "manchester city", "leicester", "chelsea", "mancheter united", "tottenham", "arsenal", "west ham" };
 
Since I got the day off and I wasn't busy, I was just having a play with your little idea. I imagines it was a football game or something to that effect :

C#:
    public class SetTeam : Players
    {
        public enum Team
        {
            Team1 = 1, Team2 = 2
        }
        public enum Team1Players
        {
            Player1 = 1, Player2 = 2, Player3 = 3, Player4 = 4, Player5 = 5
        }
        public enum Team2Players
        {
            Player1 = 1, Player2 = 2, Player3 = 3, Player4 = 4, Player5 = 5
        }
        private Dictionary<Team, List<Players>> MyTeam = new Dictionary<Team, List<Players>>();
        public SetTeam(Dictionary<Team, List<Players>> myTeam)
        {
            MyTeam = myTeam;
        }
    }
    public class Players
    {
        private protected List<Player> List_OfTeam1Players = new List<Player>();
        private protected List<Player> List_OfTeam2Players = new List<Player>();
        public List<Player>[] Add_Players(List<Enum>[] Teams)
        {
            if (Teams[0].Count > 0 && Teams[0].Count <= 5 || Teams[1].Count > 0 && Teams[1].Count <= 5)
            {                
                Dictionary<Enum, List<Player>> setTeams = new Dictionary<Enum, List<Player>>();
                Teams[0].ToList().ForEach((Enum player) =>
                {
                    dynamic dynamic_Player = new ExpandoObject();
                    {
                        dynamic_Player.Location = 20;
                        dynamic_Player.PlayerNumber = Convert.ChangeType(player, player.GetTypeCode());
                        dynamic_Player.Name = player.ToString();
                        List_OfTeam1Players.Add(new Player(dynamic_Player));
                    }
                });
                Teams[1].ToList().ForEach((Enum player) =>
                {
                    dynamic dynamic_Player = new ExpandoObject();
                    {
                        dynamic_Player.Location = 20;
                        dynamic_Player.PlayerNumber = Convert.ChangeType(player, player.GetTypeCode());
                        dynamic_Player.Name = player.ToString();
                        List_OfTeam1Players.Add(new Player(dynamic_Player));
                    }
                });
                return new List<Player>[]
                {
                    List_OfTeam1Players,
                    List_OfTeam2Players
                };
            }
            return new List<Player>[]
            {
                List_OfTeam1Players,
                List_OfTeam2Players
            };
        }
    }
    public class Player
    {
        public Player(dynamic this_Player)
        {
            Player_Location = this_Player.Location;
            Player_Number = this_Player.PlayerNumber;
            Player_Name = this_Player.Name;
        }
        public string Player_Name { get; set; }
        public int Player_Number { get; set; }
        public double Player_Location { get; set; }
    }
You can call it by putting this into a button or whoever other method you want to use to call the code :
C#:
            SetTeam game = new SetTeam(new Dictionary<SetTeam.Team, List<Players>>());

            List<Enum> TeamOne_Players = new List<Enum>  { SetTeam.Team1Players.Player1, SetTeam.Team1Players.Player2, SetTeam.Team1Players.Player3,
                SetTeam.Team1Players.Player4, SetTeam.Team1Players.Player5 };
            List<Enum> TeamTwo_Players = new List<Enum> { SetTeam.Team2Players.Player1, SetTeam.Team2Players.Player2, SetTeam.Team2Players.Player3,
                SetTeam.Team2Players.Player4, SetTeam.Team2Players.Player5 };
            List<Enum>[] teams = { TeamOne_Players, TeamTwo_Players };
            foreach (var player in from List<Player> Of_Player
                                   in game.Add_Players(teams)
                                   from Player player in Of_Player
                                   select player)
            {
                Debug.WriteLine($"{player.Player_Name.PadRight(1)} (player number {player.Player_Number}) is at grid location {player.Player_Location} on the map. ");
            }
Outputs :
Player1 (player number 1) is at grid location 20 on the map.
Player2 (player number 2) is at grid location 20 on the map.
Player3 (player number 3) is at grid location 20 on the map.
Player4 (player number 4) is at grid location 20 on the map.
Player5 (player number 5) is at grid location 20 on the map.
Player1 (player number 1) is at grid location 20 on the map.
Player2 (player number 2) is at grid location 20 on the map.
Player3 (player number 3) is at grid location 20 on the map.
Player4 (player number 4) is at grid location 20 on the map.
Player5 (player number 5) is at grid location 20 on the map.

So if you have a 5 a side game, the first five are output from the first team, and then second five are from the second team. You can change that here : if (Teams[0].Count > 0 && Teams[0].Count <= 5 || Teams[1].Count > 0 && Teams[1].Count <= 5) and by adding more players to your Enums. Its rough, and lot of room for editing, and improving, including removing some inline variables etc. But it will get you started. I do suggest debugging it and stepping through the code so you can see what it's doing, and how it works. If you got stuck, or need to know something, ask me, or if you spot any mistakes, feel free to point any out.

Hope it helps (y)
 
Back
Top Bottom