Example #1
0
    static string BuildTable(GamesXmlFactory factory)
    {
        StringBuilder builder = new StringBuilder ();

        builder.AppendLine ("\tpublic class GameList");
        builder.AppendLine ("\t{");
        builder.AppendLine ("\t\tstatic Type[] LogicPuzzlesInternal = new Type []");
        builder.AppendLine ("\t\t{");

        foreach (GameXmlDefinition definition in factory.Definitions)
        {
            builder.AppendLine (String.Format ("\t\t\ttypeof ({0}),", RemoveSpaces (definition.Name)));
        }

        builder.AppendLine ("\t\t};");

        builder.AppendLine ("\t\tpublic static Type [] LogicPuzzles");
        builder.AppendLine ("\t\t{");
        builder.AppendLine ("\t\t\tget {");
        builder.AppendLine ("\t\t\t\treturn LogicPuzzlesInternal;");
        builder.AppendLine ("\t\t\t}");
        builder.AppendLine ("\t\t}");

        builder.AppendLine ("\t}");

        return builder.ToString ();
    }
Example #2
0
        public void DrawingElements()
        {
            factory = new GamesXmlFactory ();
            factory.Read ("test_games.xml");
            definitions = factory.Definitions;

            definition = definitions [0];
            Assert.AreEqual (2, definition.DrawingObjects.Length);

            // Check image drawing object
            Assert.AreEqual (typeof (gbrainy.Core.Main.Xml.ImageDrawingObject), definition.DrawingObjects [0].GetType ());

            ImageDrawingObject image = definition.DrawingObjects [0] as ImageDrawingObject;

            Assert.AreEqual ("clock.svg", image.Filename);
            Assert.AreEqual (0.30, image.X);
            Assert.AreEqual (0.40, image.Y);
            Assert.AreEqual (0.50, image.Width);
            Assert.AreEqual (0.60, image.Height);

            // Check text drawing object
            Assert.AreEqual (typeof (gbrainy.Core.Main.Xml.TextDrawingObject), definition.DrawingObjects [1].GetType ());

            TextDrawingObject text = definition.DrawingObjects [1] as TextDrawingObject;
            Assert.AreEqual ("Sample text for unit tests", text.Text);
            Assert.AreEqual (0.5, text.X);
            Assert.AreEqual (0.4, text.Y);
            Assert.AreEqual (true, text.Centered);
            Assert.AreEqual (TextDrawingObject.Sizes.Large, text.Size);
        }
Example #3
0
        public void BasicGameDefinition()
        {
            factory = new GamesXmlFactory ();
            factory.Read ("test_games.xml");
            definitions = factory.Definitions;

            definition = definitions [0];
            Assert.AreEqual ("Clock Rotation", definition.Name);
            Assert.AreEqual (0, definition.Variants.Count);
            Assert.AreEqual (GameTypes.LogicPuzzle, definition.Type);
            Assert.AreEqual (GameDifficulty.Medium | GameDifficulty.Master, definition.Difficulty);
            Assert.AreEqual ("Rationale text", definition.Rationale.String);
            Assert.AreEqual ("How many degrees rotates the minute hand of a clock?", definition.Question.String);
            Assert.AreEqual ("How many degrees rotates the minute hand of a clocks?", definition.Question.PluralString);
            Assert.AreEqual ("[rslt]", definition.AnswerText);
        }
Example #4
0
    // Reads a games.xml, uses GameTemplate.cs file, process both, and generates a GamesXml.cs with all the games
    static void Main()
    {
        Dictionary <string, string> tokens = new Dictionary <string, string> ();
        GamesXmlFactory factory;

        TextWriter tw = new StreamWriter ("GamesXml.cs");
        factory = new GamesXmlFactory ();
        factory.Read ("../data/games.xml");
        Console.WriteLine ("Games read {0}", factory.Definitions.Count);

        tw.WriteLine ("using System;");
        tw.WriteLine ("using gbrainy.Core.Main;");
        tw.WriteLine ("using gbrainy.Core.Libraries;");
        tw.WriteLine ("using Mono.Unix;");
        tw.WriteLine ("using System.Collections.Generic;");
        tw.WriteLine ("using gbrainy.Core.Main.Xml;");
        tw.WriteLine ("");
        tw.WriteLine ("namespace gbrainy.Games");
        tw.WriteLine ("{");

        foreach (GameXmlDefinition definition in factory.Definitions)
        {
            tokens.Clear ();

            // Class definition
            tokens.Add ("@CLASSNAME@", RemoveSpaces (definition.Name));
            tokens.Add ("@NAME@", definition.Name);
            //tokens.Add ("@QUESTION@", GetStringFromDefinition (definition.Question));
            tokens.Add ("@TIP@", definition.Tip);
            //tokens.Add ("@RATIONALE@", GetStringFromDefinition (definition.Rationale));

            tokens.Add ("@VARIANTS_DEFINITION@", GetVariantsDefinitions (definition));
            //tokens.Add ("@VARIANTS_VARIABLES@", GetVariantsVariables (definition));

            string[] vars = GetVariantsVariables (definition);
            tokens.Add ("@VARIABLES_DEFINITION@", vars [VarIdxDefinitions]);
            tokens.Add ("@VARIABLES_ASSIGMENT@", vars [VarIdxAssigments]);

            string line;
            Stream read = File.OpenRead ("GameTemplate.cs");
            StreamReader sr = new StreamReader (read);

            bool write_line;
            while (true)
            {
                write_line = true;
                line = sr.ReadLine ();
                if (line == null)
                    break;

                foreach (string token in tokens.Keys)
                {
                    if (line.IndexOf (token) == -1)
                        continue;

                    line = line.Replace (token, tokens[token]);

                    if (String.IsNullOrEmpty (line) == true)
                        write_line = false;
                }

                if (write_line)
                    tw.WriteLine (line);
            }
            read.Close ();
        }

        tw.WriteLine (BuildTable (factory));

        tw.WriteLine ("}");
        tw.Close ();
    }
Example #5
0
        public void GameDefinitionWithVariants()
        {
            GameXmlDefinitionVariant variant;
            factory = new GamesXmlFactory ();
            factory.Read ("test_games.xml");
            definitions = factory.Definitions;
            definition = definitions [1]; // Age Game

            Assert.AreEqual ("Age", definition.Name);
            Assert.AreEqual (2, definition.Variants.Count);
            Assert.AreEqual ("father_son.svg", ((definition.DrawingObjects[0]) as ImageDrawingObject).Filename);

            // Variant: John is 46 years old.
            variant = definition.Variants [0];
            Assert.AreEqual (true, variant.Question.String.Contains ("John is 46 years old"));
            Assert.AreEqual ("[son]", variant.AnswerText);
            Assert.AreEqual (true, variant.Variables.Contains ("int father = 46;"));

            // Variant: John's age is nowadays 2 times his son's age.
            variant = definition.Variants [1];
            Assert.AreEqual (true, variant.Question.String.Contains ("John's age is nowadays 2 times his son's age."));
            Assert.AreEqual ("24", variant.AnswerText);
            Assert.AreEqual (true, variant.Variables.Contains ("int ago = years [idx];"));
        }
Example #6
0
        // XML are stored using the Variant as a pointer to the game + the internal variant
        public void LoadGamesFromXml(string file)
        {
            // Load defined XML games
            GamesXmlFactory xml_games;

            xml_games = new GamesXmlFactory ();
            xml_games.Read (file);

            Type type = typeof (GameXml);
            int cnt = 0;

            foreach (GameXmlDefinition game in xml_games.Definitions)
            {
                // If the game has variants the game definition is used as reference
                // but only the variants are playable. The first variant is used as game (IsGame = true)
                available_games.Add (new GameLocator (type, cnt++, game.Type, true));

                switch (game.Type) {
                case GameTypes.LogicPuzzle:
                    cnt_logic++;
                    break;
                case GameTypes.Calculation:
                    cnt_calculation++;
                    break;
                default:
                    break;
                }

                for (int i = 1; i < game.Variants.Count; i++)
                {
                    available_games.Add (new GameLocator (type, cnt++, game.Type, false));
                }
            }
        }