using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Reflection; using System.CodeDom.Compiler; using System.CodeDom; using System.IO; using System.Xml; using Microsoft.Xna; using Microsoft.Xna.Framework; using DarkWynter.Stream; using DarkWynter.Engine.Globals; using DarkWynter.Engine.Utilities; using DarkWynter.Game; using DarkWynter.Game.Globals; using DarkWynter.Game.GameObjects; using DarkWynter.Game.Controllers; using DarkWynter.App.Surveys; using DarkWynter.App.ChallengeSet; using System.Threading; using CSTextEditor; namespace DarkWynter.App { /// /// GameForm controls the game entry point as well as the compiler entry /// points for the Game2Learn system /// public partial class GameForm : Form { MainForm compilerExternalForm; List challengeList; CompilerResults compilerResults; string studentCode; string cleanedStudentCode; public static string customErrorMessage; int unsuccessfulCompiles = 0; # region Pre and post test instructions string preTestIntro = @"Hello and welcome to the Game2Learn quest - WhileLoops. Built on the DarkWynter engine, this game is designed to teach you how to work with while loops. First off, however, we need to ask you a couple of questions before we get into the game. Please click on the pre-test button to get started! "; string preTestIntroButton = "Start the Pre-Test"; string postTestIntro = @"Thank you for playing the Game2Learn quest - WhileLoops. We hope that the experience was informative for you and that you had fun playing the game. Before we let you go, however, we need to ask you a couple more questions. Please click on the post-test button to get started! "; string postTestIntroButton = "Start the Post-Test"; #endregion SurveyForm surveyForm = new SurveyForm(); public static List surveyList; /// Initializes a new DarkWynter host form public GameForm() { InitializeComponent(); compilerPanel.Hide(); dialogueGroupPanel.Hide(); getTMLoc.Hide(); saveButton.Hide(); runButton.Hide(); compileButton.Hide(); fileToolStrip.Visible = true; amountBox.Text = "5"; radiusBox.Text = "2"; positionBox1.Text = "21"; positionBox2.Text = "0"; positionBox3.Text = "45"; #region Challenges // Create Challenge List challengeList = new List(); for (int i = 0; i < XML.ChallengeNodes.Count; i++) { if (int.Parse(XML.ChallengeNodes[i].Attributes["id"].Value) == 0) { challengeList.Add(new Challenge0(XML.ChallengeNodes[i])); } if (int.Parse(XML.ChallengeNodes[i].Attributes["id"].Value) == 1) { challengeList.Add(new Challenge1(XML.ChallengeNodes[i])); } if (int.Parse(XML.ChallengeNodes[i].Attributes["id"].Value) == 2) { challengeList.Add(new Challenge2(XML.ChallengeNodes[i])); } if (int.Parse(XML.ChallengeNodes[i].Attributes["id"].Value) == 3) { challengeList.Add(new Challenge3(XML.ChallengeNodes[i])); } } // Init first problem Statics_Game.GameSettings.currentChallengeNumber = 0; // Load the first problem codeTextBox.Text = challengeList[Statics_Game.GameSettings.currentChallengeNumber].GetScaffolding(); // Load the first instructions dialogueTextBox.Text += challengeList[Statics_Game.GameSettings.currentChallengeNumber].challengeDialogue[challengeList[Statics_Game.GameSettings.currentChallengeNumber].dialogueCounter] + "\r\n"; #endregion #region Surveys // Initialize the text for the pretest InstructionBox.Text = preTestIntro; testButton.Text = preTestIntroButton; // Create the Pretest survey surveyList = new List(); // Pretest Survey surveyList.Add(new Survey("_xml/QuestSettings/WLPreSurvey.xml")); #endregion } #region Quit, Enter, Load (Mostly Not Used) /// Executed when the user clicks the quit button /// Quit button that has been clicked /// Not used private void quitClicked(object sender, FormClosingEventArgs e) { string name = Logging.CreateFileName(); Logging.writeG2LLog(name); //Logging.writeG2LSurvey(name); Application.Exit(); } private void quitClickedButton(object sender, EventArgs e) { // ... triggers FormClosingEvent quitClicked() Application.Exit(); } private void enterClicked(object sender, EventArgs e) { //DarkWynterGame.gameControllers[0]. } private void GameForm_Load(object sender, EventArgs e) { } # endregion #region Compile and Run the code /// /// Compiles the code in the codeTextBox.Text field /// /// /// private void CompileCode() { Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Button Message] Student hit compiler button [End Button Message]"); Logging.G2LLogList.Add("\r\n[Student Code] - \r\n"); Logging.G2LLogList.Add(cleanedStudentCode + "\r\n[End Student Code]\r\n"); compilerResults = Compiler.CompileCode(studentCode); // If _does_not_ compile if (compilerResults.Errors.Count > 0) { // Display compilation errors. StringBuilder sb = new StringBuilder(); sb.AppendFormat("Errors building {0} into {1}\n\n", studentCode, compilerResults.PathToAssembly); foreach (CompilerError ce in compilerResults.Errors) { sb.Append(ce.ToString()); sb.Append("\n"); } unsuccessfulCompiles++; outputTextBox.Text = sb.ToString(); Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Error Message] There were errors in the code [End Error Message] "); Logging.G2LLogList.Add(sb.ToString()); Logging.G2LLogList.Add(" [Record Message] This is the " + unsuccessfulCompiles + " attempt at this problem. [End Record Message] "); } if (compilerResults.Errors.Count == 0) { outputTextBox.Text = "Compile Successful\n"; Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Success Message] There were no errors in the code [End Success Message] "); } } /// /// Runs the Code, after compilation, in the codeTextBoc.Text field /// private void RunCode() { // If it hasn't been compiled, just have them compile it if (compilerResults == null) { outputTextBox.Text = "You must compile before you can run!"; Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Error Message] Student tried to run without compiling first. [End Error Message]"); return; } // If compiles, validate output if (compilerResults.Errors.Count == 0) { StringWriter stringWriter = new StringWriter(); Console.SetOut(stringWriter); // Execute user code Compiler.Execute(compilerResults.CompiledAssembly, "Test", "MyFunction", null); // List output in compiler feedback box try { outputTextBox.Text += stringWriter.ToString(); Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Compiler output] - "); Logging.G2LLogList.Add(stringWriter.ToString() + "[End Compiler output]\r\n"); // Execute instructor evaluation and feedback function if (challengeList[Statics_Game.GameSettings.currentChallengeNumber].ValidateStudentCode(studentCode, stringWriter.ToString())) { if (Statics_Game.GameSettings.isProblemFinished == false) { // If correct, call the vis code challengeList[Statics_Game.GameSettings.currentChallengeNumber].RunVizualization(); Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Success Message] Bridge " + Statics_Game.GameSettings.currentChallengeNumber + " built successfully. [End Success Message]"); // Ensure Current Prob does not go out of bounds if (Statics_Game.GameSettings.currentChallengeNumber < challengeList.Count - 1) { // Finish the vis and don't run it again Statics_Game.GameSettings.isProblemFinished = true; } } } // write the custom error message to the screen and to the log else outputTextBox.Text = customErrorMessage; Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Custom Error Message] " + customErrorMessage + " [End Custom Error Message]"); } catch { string runOnWhile = @"You have managed to exceed the memory in your code which means your while loop is probably not coded correctly. Please check your brackets and make sure you are ending your while loop correctly."; outputTextBox.Text = runOnWhile; Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Compiler output] - "); Logging.G2LLogList.Add(runOnWhile + "[End Compiler output]\r\n"); } // Move the scroller down ScrolltoText(1); stringWriter.Close(); } } #endregion #region Buttons, buttons, and more buttons /// /// Open the form, deactivates Engine controls, resize the gameWindow, handle resetText() trigger /// /// /// private void openToolStripMenuItem_Click(object sender, EventArgs e) { Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Button Message] Compiler panels opened [End Button Message]"); Logging.G2LLogList.Add("\r\n[Game Message] Student is on problem number " + Statics_Game.GameSettings.currentChallengeNumber); Logging.G2LLogList.Add("Student is on island number " + Statics_Game.GameSettings.currentChallengeNumber + " [End Game Message] \r\n"); Engine.DarkWynterEngine.Deactivate(); compilerPanel.Show(); dialogueGroupPanel.Show(); // getTMLoc.Show(); //getTMLoc.Enabled = true; if (Statics_Engine.PlayerSettings.doResetText == true) { resetText(); } int GameWindowWidth = 620; int GameWindowHeight = 500; this.darkWynterGameControl.Location = new System.Drawing.Point(400, 55); this.darkWynterGameControl.Size = new System.Drawing.Size(GameWindowWidth, GameWindowHeight); // Set the viewport Microsoft.Xna.Framework.Graphics.Viewport viewport = new Microsoft.Xna.Framework.Graphics.Viewport(); viewport.X = this.darkWynterGameControl.Location.X; viewport.Y = this.darkWynterGameControl.Location.Y; viewport.Width = this.darkWynterGameControl.Size.Width; viewport.Height = this.darkWynterGameControl.Size.Height; viewport.MinDepth = Statics_Stream.RenderSettings.graphics.GraphicsDevice.Viewport.MinDepth; viewport.MaxDepth = Statics_Stream.RenderSettings.graphics.GraphicsDevice.Viewport.MaxDepth; Statics_Stream.RenderSettings.graphics.GraphicsDevice.Viewport = viewport; Statics_Game.GameSettings.isCompilerOpen = true; //Statics_Game.GameSettings.hudNeedUpdate = true; } /// /// Compiler Button /// /// /// private void compileButton_Click(object sender, EventArgs e) { // Compile student code studentCode = codeTextBox.Text; //studentCode = compilerExternalForm.textEditorControl1.Text; //Invoke(new MethodInvoker(delegate //{ // studentCode = compilerExternalForm.textEditorControl1.Text; //})); cleanedStudentCode = studentCode.Trim(); cleanedStudentCode = studentCode.Replace('\n', ' '); CompileCode(); } /// /// Runs the compiled code /// /// /// private void runButton_Click(object sender, EventArgs e) { RunCode(); } /// /// Reactivates Engine controls, resets gameWindow /// /// /// private void closeCompilerToolStripMenuItem1_Click(object sender, EventArgs e) { Logging.G2LLogList.Add(DateTime.Now.ToString() + " - [Button Message] Compiler panels closed [End Button Message] "); Engine.DarkWynterEngine.Activate(); compilerPanel.Hide(); dialogueGroupPanel.Hide(); compileButton.Hide(); runButton.Hide(); // getTMLoc.Hide(); // getTMLoc.Enabled = false; this.darkWynterGameControl.Location = new System.Drawing.Point(0, 25); this.darkWynterGameControl.Size = new System.Drawing.Size(1024, 743); // Set the viewport Microsoft.Xna.Framework.Graphics.Viewport viewport = new Microsoft.Xna.Framework.Graphics.Viewport(); viewport.X = this.darkWynterGameControl.Location.X; viewport.Y = this.darkWynterGameControl.Location.Y; viewport.Width = this.darkWynterGameControl.Size.Width; viewport.Height = this.darkWynterGameControl.Size.Height; viewport.MinDepth = Statics_Stream.RenderSettings.graphics.GraphicsDevice.Viewport.MinDepth; viewport.MaxDepth = Statics_Stream.RenderSettings.graphics.GraphicsDevice.Viewport.MaxDepth; Statics_Stream.RenderSettings.graphics.GraphicsDevice.Viewport = viewport; Statics_Game.GameSettings.hudStopResize = true; Statics_Game.GameSettings.isCompilerOpen = false; } /// /// Keeps the dialogue rolling /// /// /// private void continueDialogue_Click(object sender, EventArgs e) { challengeList[Statics_Game.GameSettings.currentChallengeNumber].dialogueCounter++; // If second problem set if (challengeList[Statics_Game.GameSettings.currentChallengeNumber].dialogueCounter < challengeList[Statics_Game.GameSettings.currentChallengeNumber].dialogueMax) { dialogueTextBox.Text += challengeList[Statics_Game.GameSettings.currentChallengeNumber] .challengeDialogue[challengeList[Statics_Game.GameSettings.currentChallengeNumber].dialogueCounter] + "\r\n"; if (challengeList[Statics_Game.GameSettings.currentChallengeNumber].dialogueCounter == 10) { compileButton.Show(); runButton.Show(); } } // Move the scroller down ScrolltoText(2); } /// /// Will save out to a text file when I finish it /// /// /// private void saveButton_Click(object sender, EventArgs e) { // save the code out String name = "problem" + Statics_Game.GameSettings.currentChallengeNumber + ".txt"; string logFile = "../../../__Game/Compiler/Logs/" + name; StreamWriter myFile = null; FileStream fileStream = File.Open(logFile, FileMode.CreateNew, FileAccess.Write); myFile = new StreamWriter(fileStream); myFile.WriteLine(DateTime.Now); myFile.Write(codeTextBox.Text); //myFile.Write(compilerExternalForm.textEditorControl1.Text); myFile.Close(); } /// /// Starts the pretest /// /// /// public void pretestButton_Click(object sender, EventArgs e) { InstructionBox.Hide(); testButton.Hide(); SurveyPanel.Hide(); surveyForm.Location = new System.Drawing.Point(300,300); surveyForm.Show(); } /// /// MS sucks. /// /// /// private void postTestOpen_Click(object sender, EventArgs e) { if (Statics_Engine.SystemSettings.gameState == Enums_Engine.EngineState.FINALIZE_G2LSTUFF) { SurveyForm surveyForm = new SurveyForm(); // Posttest Survey surveyList.Add(new Survey("_xml/QuestSettings/WLPostSurvey.xml")); surveyForm.Location = new System.Drawing.Point(300, 300); surveyForm.Show(); } } #endregion #region Helper methods // autoscoll - do not change please // boxNumber keeps track of which one we are changing private void ScrolltoText(int boxNumber) { if (boxNumber == 2) { dialogueTextBox.SelectionStart = dialogueTextBox.Text.Length; dialogueTextBox.SelectionLength = dialogueTextBox.Text.Length; dialogueTextBox.SelectionColor = System.Drawing.Color.Red; dialogueTextBox.SelectionFont = new Font("Courier New", 20, FontStyle.Bold); dialogueTextBox.ScrollToCaret(); dialogueTextBox.Select(); } if (boxNumber == 1) { outputTextBox.SelectionStart = outputTextBox.Text.Length; outputTextBox.ScrollToCaret(); outputTextBox.Select(); } } private void resetText() { // Reset dialog counter in problem challengeList[Statics_Game.GameSettings.currentChallengeNumber].dialogueCounter = 0; // Clear boxes dialogueTextBox.Text = ""; outputTextBox.Text = ""; codeTextBox.Text = ""; // Load the first problem codeTextBox.Text = challengeList[Statics_Game.GameSettings.currentChallengeNumber].GetScaffolding(); // External Form //compilerExternalForm.textEditorControl1.Text = //challengeList[Statics_Game.GameSettings.currentChallengeNumber].GetScaffolding(); // Load the first instructions dialogueTextBox.Text += challengeList[Statics_Game.GameSettings.currentChallengeNumber] .challengeDialogue[challengeList[Statics_Game.GameSettings.currentChallengeNumber].dialogueCounter] + "\r\n"; } #endregion #region ApplicationMain Methods private void buttonTheDarkWynterProject_Click(object sender, EventArgs e) { System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.EnableRaisingEvents = false; // Launch Website proc.StartInfo.FileName = "iexplore"; proc.StartInfo.Arguments = "http://www.darkwynter.com/"; // Launch Word File //proc.StartInfo.FileName = "winword"; //proc.StartInfo.Arguments = "C:\\Dotnetstuff\\TestWordDoc.doc"; proc.Start(); } private void hideAll() { //levelEditorPanel.Hide(); //contentLoader.Hide(); //cutSceneMain.Hide(); } private void cutSceneButton_Click(object sender, EventArgs e) { hideAll(); //cutSceneMain.MdiParent = this; //cutSceneMain.Dock = DockStyle.Fill; //cutSceneMain.MinimizeBox = false; //cutSceneMain.MaximizeBox = false; //cutSceneMain.FormBorderStyle = FormBorderStyle.None; //cutSceneMain.Show(); } private void levelEditorButton_Click(object sender, EventArgs e) { hideAll(); //levelEditorMain.MdiParent = this; //levelEditorMain.Dock = DockStyle.Fill; //levelEditorMain.MinimizeBox = false; //levelEditorMain.MaximizeBox = false; //levelEditorMain.FormBorderStyle = FormBorderStyle.None; //levelEditorMain.Show(); } private void contentLoaderButton_Click(object sender, EventArgs e) { hideAll(); //contentLoaderMain.MdiParent = this; //contentLoader.Dock = DockStyle.Left; //contentLoaderMain.MinimizeBox = false; //contentLoaderMain.MaximizeBox = false; //contentLoaderMain.FormBorderStyle = FormBorderStyle.None; //contentLoader.Show(); //this.Controls.Add(contentLoader); } private void Enter_Click(object sender, EventArgs e) { // Terrain Mod Panel float amount = float.Parse(amountBox.Text); Vector3 position = new Vector3(float.Parse(positionBox1.Text), float.Parse(positionBox2.Text), float.Parse(positionBox3.Text)); Vector3 position2 = new Vector3(float.Parse(end1.Text), float.Parse(end2.Text), float.Parse(end3.Text)); int radius = int.Parse(radiusBox.Text); TerrainModRequest request = new TerrainModRequest(amount, position, position2, radius, Enums_Engine.TerrainModType.EQUALTO); GameController.NonControllerBasedTerrainMod(request); } #endregion private void button_CSCompiler_Click(object sender, EventArgs e) { // Form thread Thread t = new Thread(new ThreadStart(compilerForm)); t.SetApartmentState(ApartmentState.STA); t.Start(); } //[MTAThread] private void compilerForm() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); compilerExternalForm = new MainForm(); compilerExternalForm.textEditorControl1.Text = challengeList[Statics_Game.GameSettings.currentChallengeNumber].GetScaffolding(); Application.Run(compilerExternalForm); //compilerForm.MinimizeBox = false; //compilerForm.MaximizeBox = false; //compilerForm.FormBorderStyle = FormBorderStyle.Sizable; //compilerForm.Show(); } ///// ///// The main entry point for the application. ///// //[STAThread] //static void RunCompilerForm() //{ // Application.EnableVisualStyles(); // Application.SetCompatibleTextRenderingDefault(false); // Application.Run(new MainForm()); //} } }