示例#1
0
 /// <summary>
 /// The main entry point for the application.
 /// </summary>
 static void Main(string[] args)
 {
     using (Game game = new Game())
     {
         game.Run();
     }
 }
示例#2
0
        public void Parse(Game game, ArrayList teams /*, Scenario2DPainter scenario2DPainter */)
        {
            String line;
            Team team = null;   //current team

            if (File.Exists(filePath))
            {
                StreamReader file = null;
                try
                {
                    file = new StreamReader(filePath);
                    while ((line = file.ReadLine()) != null)
                    {
                        line = line.TrimStart(' '); //remove whitespaces from the start of the line
                        if (line.Equals("") || line.StartsWith("//"))    //ignore empty and comment lines
                            continue;
                        else if (line.StartsWith("T:")) //new team
                        {
                            String teamName = line.Substring(2).TrimStart(' ');
                            team = new Team(game, teamName, Color.White);

                            //add team to teams
                            teams.Add(team);
                        }
                        else if (line.StartsWith("C:"))
                        {
                            String color = line.Substring(2).TrimStart(' ');

                            byte r = byte.Parse(color.Substring(0, 3));
                            byte g = byte.Parse(color.Substring(3, 3));
                            byte b = byte.Parse(color.Substring(6, 3));

                            team.Color = new Color(r, g, b);
                        }
                        else if (line.StartsWith("P:")) //positioning
                        {
                            String teamStrategy = line.Substring(2).TrimStart(' ');
                            team.SetStrategy(teamStrategy);
                        }
                        else if (team != null)   //read players
                        {
                            String playerName = "", position = "", side = "";
                            int number, defense, goalkeeping, offense, shot, speed, stamina;
                            int extraWhiteSpaces = 0;

                            Player newPlayer = null;
                            String[] tokens = line.Split(' ');

                            int i = 0;  //in the end of the cycle it will be the index of the player number
                            double num;
                            //get player name
                            while (!double.TryParse(tokens[i], out num))    //while not a number
                            {
                                if (tokens[i].Equals(""))  //extra white space between player's names
                                {
                                    extraWhiteSpaces++;
                                    i++;
                                    continue;
                                }
                                if (i == 0) playerName += tokens[i];
                                else playerName += " " + tokens[i];
                                i++;
                            }
                            int j = i;
                            i -= extraWhiteSpaces;
                            while (j < tokens.Length)   //extra white space between the rest of the info
                            {
                                if (tokens[j].Equals(""))
                                {
                                    extraWhiteSpaces++;
                                }
                                j++;
                            }
                            //get player properties
                            if (tokens.Length - i == 9 + extraWhiteSpaces) //correct amount of info
                            {
                                //limit the skill level allowed
                                int maxSkill = 100;
                                try
                                {
                                    i += extraWhiteSpaces;
                                    number = int.Parse(tokens[i++]);
                                    position = tokens[i++];
                                    side = tokens[i++];
                                    defense = (int.Parse(tokens[i++]) > maxSkill) ? maxSkill : int.Parse(tokens[i - 1]);
                                    goalkeeping = (int.Parse(tokens[i++]) > maxSkill) ? maxSkill : int.Parse(tokens[i - 1]);
                                    offense = (int.Parse(tokens[i++]) > maxSkill) ? maxSkill : int.Parse(tokens[i - 1]);
                                    shot = (int.Parse(tokens[i++]) > maxSkill) ? maxSkill : int.Parse(tokens[i - 1]);
                                    speed = (int.Parse(tokens[i++]) > maxSkill) ? maxSkill : int.Parse(tokens[i - 1]);
                                    stamina = (int.Parse(tokens[i++]) > maxSkill) ? maxSkill : int.Parse(tokens[i - 1]);

                                    TacticalPosition position2 = TacticalPosition.goalkeeper;
                                    Side side2 = Side.center;

                                    if (position.Equals("defender")) position2 = TacticalPosition.defender;
                                    else if (position.Equals("dfmidfielder")) position2 = TacticalPosition.dfmidfielder;
                                    else if (position.Equals("midfielder")) position2 = TacticalPosition.midfielder;
                                    else if (position.Equals("ofmidfielder")) position2 = TacticalPosition.ofmidfielder;
                                    else if (position.Equals("striker")) position2 = TacticalPosition.striker;

                                    if (side.Equals("left")) side2 = Side.left;
                                    else if (side.Equals("right")) side2 = Side.right;

                                    //create new player
                                    newPlayer = new Player(game, team, playerName, number, position2, side2, defense, goalkeeping, offense, shot, speed, stamina /*scenario2DPainter*/, 0f, 0f);

                                    //insert player in team
                                    team.Players.Add(newPlayer);
                                }
                                catch (FormatException)
                                {
                                    Console.WriteLine("Wrong line format for player " + playerName + "!");
                                }
                            }
                            else Console.WriteLine("Incorrect amount of info in line: " + line);

                        }
                        Console.WriteLine(line);
                    }
                }
                finally
                {
                    if (file != null)
                        file.Close();
                }
            }
            else Console.WriteLine("Teams file not found!");
        }