コード例 #1
0
        // Parses a game state from a string. On success, returns 1. On failure,
        // returns 0.
        private int ParseGameState(String s)
        {
            Planets.Clear();
            Fleets.Clear();
            ClearDupeCache();
            //PORT: Make WindowsCompatible
            String[] lines    = s.Replace("\r\n", "\n").Split('\n');
            int      planetid = 0;

            for (int i = 0; i < lines.Length; ++i)
            {
                String line         = lines[i];
                int    commentBegin = line.IndexOf('#');
                if (commentBegin >= 0)
                {
                    line = line.Substring(0, commentBegin);
                }
                if (line.Trim().Length == 0)
                {
                    continue;
                }
                String[] tokens = line.Split(' ');
                if (tokens.Length == 0)
                {
                    continue;
                }
                if (tokens[0].Equals("P"))
                {
                    if (tokens.Length != 6)
                    {
                        return(0);
                    }
                    double x          = Double.parseDouble(tokens[1]);
                    double y          = Double.parseDouble(tokens[2]);
                    int    owner      = Integer.parseInt(tokens[3]);
                    int    numShips   = Integer.parseInt(tokens[4]);
                    int    growthRate = Integer.parseInt(tokens[5]);
                    Planet p          = new Planet(planetid++, owner, numShips, growthRate, x, y);
                    Planets.Add(p);
                    if (gamePlayback.Length > 0)
                    {
                        gamePlayback.Append(":");
                    }
                    gamePlayback.Append("" + x + "," + y + "," + owner + "," + numShips + "," + growthRate);
                }
                else if (tokens[0].Equals("F"))
                {
                    if (tokens.Length != 7)
                    {
                        return(0);
                    }
                    int   owner           = Integer.parseInt(tokens[1]);
                    int   numShips        = Integer.parseInt(tokens[2]);
                    int   source          = Integer.parseInt(tokens[3]);
                    int   destination     = Integer.parseInt(tokens[4]);
                    int   totalTripLength = Integer.parseInt(tokens[5]);
                    int   turnsRemaining  = Integer.parseInt(tokens[6]);
                    Fleet f = new Fleet(owner,
                                        numShips,
                                        source,
                                        destination,
                                        totalTripLength,
                                        turnsRemaining);
                    Fleets.Add(f);
                }
                else
                {
                    return(0);
                }
            }
            gamePlayback.Append("|");
            return(1);
        }