Example #1
0
 // Schedules all prisoners who match the given predicate for release
 public static int Release(Prison prison, Predicate<Prisoner> predicate) {
     int[] idsToRemove = FindPrisoners(prison, predicate);
     foreach (int id in idsToRemove) {
         ReleasePrisoner(prison, id);
     }
     return idsToRemove.Length;
 }
Example #2
0
        public Prison Load(Stream stream) {
            var reader = new StreamReader(stream, Encoding.ASCII);
            var nodes = new Stack<Node>();
            Node currentNode = new Prison();
            int lineNum = 0;

            string line;
            while ((line = reader.ReadLine()) != null) {
                lineNum++;
                Tokenize(line);

                // skip blank lines
                if (tokens.Count == 0) continue;

                // start a new node
                if ("BEGIN".Equals(tokens[0])) {
                    nodes.Push(currentNode);

                    string label = tokens[1];
                    currentNode = currentNode.CreateNode(label);

                    if (tokens.Count > 2) {
                        // inline node
                        if (tokens.Count%2 != 1) {
                            throw new FormatException(
                                "Unexpected number of tokens in an inline node definition on line " + lineNum);
                        }
                        if (!"END".Equals(tokens[tokens.Count - 1])) {
                            throw new FormatException("Unexpected end of inline node definition on line " + lineNum);
                        }
                        for (int i = 2; i < tokens.Count - 1; i += 2) {
                            string key = tokens[i];
                            string value = tokens[i + 1];
                            currentNode.ReadKey(key, value);
                        }
                        Node upperNode = nodes.Pop();
                        upperNode.FinishedReadingNode(currentNode);
                        currentNode = upperNode;
                    } else {
                        currentNode.DoNotInline = true;
                    }
                } else if ("END".Equals(tokens[0])) {
                    // end of multi-line section
                    Node upperNode = nodes.Pop();
                    upperNode.FinishedReadingNode(currentNode);
                    currentNode = upperNode;
                } else {
                    // inside a multi-line section
                    string key = tokens[0];
                    string value = tokens[1];
                    currentNode.ReadKey(key, value);
                }
            }
            if (nodes.Count != 0) {
                throw new FormatException("Unexpected end of file!");
            }
            return (Prison)currentNode;
        }
Example #3
0
 // Eliminates a single prisoner, by ID. Erases all traces of their existence.
 public static void EliminatePrisoner(Prison prison, int id) {
     prison.Objects.Prisoners.Remove(id);
     prison.Contraband.Child.Prisoners.Remove(id);
     prison.Informants.Child.Prisoners.RemoveAll(informant => informant.PrisonerId == id);
     prison.Misconduct.Reports.Reports.Remove(id);
     prison.Penalties.Child.PenaltyList.Remove(id);
     foreach (ReformProgram program in prison.Reform.Programs.Programs) {
         program.Students.Students.Remove(id);
     }
     prison.Tunnels.Diggers.Prisoners.RemoveAll(p => p.PrisonerId == id);
     prison.Victory.Child.Log.RemoveAll(entry => entry.PrisonerId == id);
 }
Example #4
0
 void miFileOpen_Click(object sender, EventArgs e) {
     if (openDialog.ShowDialog() == DialogResult.OK) {
         fileName = openDialog.FileName;
         using (FileStream fs = File.OpenRead(openDialog.FileName)) {
             Text = String.Format("Loading {0} | {1}", Path.GetFileName(fileName), AppName);
             try {
                 prison = new Parser().Load(fs);
             } catch (Exception ex) {
                 string msg = String.Format("An error occured while loading:{0}{1}{0}{2}",
                                            Environment.NewLine, ex.GetType().Name, ex.Message);
                 MessageBox.Show(msg, String.Format("Error loading {0}", Path.GetFileName(fileName)),
                                 MessageBoxButtons.OK, MessageBoxIcon.Error);
                 Close();
             }
             if (prison.Version != Parser.SupportedVersion) {
                 MessageBox.Show(String.Format(Resources.FileVersionWarning, Parser.SupportedVersion,
                                               prison.Version));
             }
             LoadPrisonToGui();
             Enabled = true;
             Text = String.Format("{0} | {1}", Path.GetFileName(fileName), AppName);
         }
     } else {
         if (prison == null) {
             Close();
         }
     }
 }
Example #5
0
 // Schedules a single prisoner for release, by ID.
 public static void ReleasePrisoner(Prison prison, int id) {
     PrisonerBio bio = prison.Objects.Prisoners[id].Bio;
     bio.Served = bio.Sentence;
 }
Example #6
0
 public static int CountPrisoners(Prison prison, Predicate<Prisoner> predicate) {
     return prison.Objects.Prisoners.Values
                  .Count(prisoner => predicate(prisoner));
 }
Example #7
0
 // Returns a list of IDs of all prisoners who match the given predicate
 public static int[] FindPrisoners(Prison prison, Predicate<Prisoner> predicate) {
     return prison.Objects.Prisoners.Values
                  .Where(prisoner => predicate(prisoner))
                  .Select(prisoner => prisoner.Id)
                  .ToArray();
 }
Example #8
0
 public void WritePrison(Prison prison) {
     writer.Write('\n');
     WriteNodeData(prison);
 }