PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Jeapordy/MainWindow.xaml.cs

https://github.com/CautionRH/MasonicJeopardy
C# | 196 lines | 178 code | 15 blank | 3 comment | 39 complexity | a47af616f4e7d02dcc52e155fb3f71f5 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Windows.Threading;
  16. using System.Xml;
  17. using System.Xml.Linq;
  18. using Microsoft.Win32;
  19. namespace Jeopordy
  20. {
  21. /// <summary>
  22. /// Interaction logic for MainWindow.xaml
  23. /// </summary>
  24. public partial class MainWindow : Window
  25. {
  26. public delegate void VoidDelegate();
  27. List<Category> Categories;
  28. Dictionary<string, QuestionAndAnswer> questionMapping;
  29. string _finalQuestion = "";
  30. string _finalAnswer = "";
  31. string _finalTopic = "";
  32. System.Timers.Timer timesUp;
  33. string ApplicationDir;
  34. public MainWindow()
  35. {
  36. InitializeComponent();
  37. Categories = new List<Category>();
  38. questionMapping = new Dictionary<string, QuestionAndAnswer>();
  39. ReadGameSheet();
  40. SetGameBoard();
  41. ApplicationDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
  42. KeyDown += MainWindow_KeyDown;
  43. AudioPlayer.Source = new Uri(ApplicationDir + @"\Music\Jeopardy Board SMS.mp3");
  44. AudioPlayer.Play();
  45. }
  46. void MainWindow_KeyDown(object sender, KeyEventArgs e)
  47. {
  48. if (e.Key == Key.Escape)
  49. {
  50. Environment.Exit(0);
  51. }
  52. if (e.Key == Key.F)
  53. {
  54. SetUpFinalJeopardy();
  55. }
  56. }
  57. public void ReadGameSheet()
  58. {
  59. OpenFileDialog ofd = new OpenFileDialog();
  60. ofd.DefaultExt = ".xml";
  61. if(ofd.ShowDialog() == true)
  62. {
  63. XDocument doc = XDocument.Load(ofd.FileName);
  64. foreach (var element in doc.Root.DescendantNodes().OfType<XElement>())
  65. {
  66. if (element.Name == "category")
  67. {
  68. Category cat = new Category();
  69. cat.CategoryName = (string)element.Attribute("desc");
  70. foreach (var child in element.DescendantNodes().OfType<XElement>())
  71. {
  72. if (child.Name == "question")
  73. {
  74. QuestionAndAnswer qa = new QuestionAndAnswer();
  75. qa.Answer = (string)child.Attribute("answer");
  76. qa.Question = (string)child.Attribute("text");
  77. qa.Value = int.Parse((string)child.Attribute("value"));
  78. qa.DailyDouble = (string)child.Attribute("daily_double");
  79. cat.AddQuestion(qa);
  80. }
  81. }
  82. Categories.Add(cat);
  83. }
  84. if (element.Name == "final")
  85. {
  86. _finalQuestion = (string)element.Attribute("text");
  87. _finalAnswer = (string)element.Attribute("answer");
  88. _finalTopic = (string)element.Attribute("topic");
  89. }
  90. }
  91. }
  92. }
  93. public void SetGameBoard()
  94. {
  95. for (int i = 0; i < 5; i++)
  96. {
  97. if(i == 0)
  98. CategoryText1.Text = Categories[i].CategoryName;
  99. else if (i == 1)
  100. CategoryText2.Text = Categories[i].CategoryName;
  101. else if (i == 2)
  102. CategoryText3.Text = Categories[i].CategoryName;
  103. else if (i == 3)
  104. CategoryText4.Text = Categories[i].CategoryName;
  105. else if (i == 4)
  106. CategoryText5.Text = Categories[i].CategoryName;
  107. for (int j = 0; j < 5; j++)
  108. {
  109. QuestionAndAnswer qa = Categories[i].questions[j];
  110. string map = "QuestionC" + (i+1) + "R" + (j+1);
  111. questionMapping[map] = qa;
  112. }
  113. }
  114. }
  115. private void Question_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  116. {
  117. TextBlock textblock = (TextBlock)sender;
  118. string question = questionMapping[textblock.Name].Question;
  119. string answer = questionMapping[textblock.Name].Answer;
  120. string dailydouble = questionMapping[textblock.Name].DailyDouble;
  121. if (textblock.Text == question)
  122. {
  123. textblock.Text = questionMapping[textblock.Name].Answer;
  124. timesUp.Stop();
  125. }
  126. else if (textblock.Text == answer)
  127. {
  128. textblock.Text = "";
  129. }
  130. else
  131. {
  132. if (dailydouble == "true")
  133. {
  134. textblock.Text = "DAILY DOUBLE";
  135. questionMapping[textblock.Name].DailyDouble = "false";
  136. AudioPlayer.Source = new Uri(ApplicationDir + @"\Music\Daily double.mp3");
  137. AudioPlayer.Play();
  138. }
  139. else
  140. {
  141. textblock.Text = question;
  142. timesUp = new System.Timers.Timer();
  143. timesUp.AutoReset = false;
  144. timesUp.Interval = 10000;
  145. timesUp.Elapsed += timesUp_Elapsed;
  146. timesUp.Enabled = true;
  147. }
  148. }
  149. }
  150. public void SetUpFinalJeopardy()
  151. {
  152. Grid.SetZIndex(BlockQuestions, 99);
  153. Grid.SetZIndex(FinalJeopardyQuestion, 100);
  154. FinalJeopardyQuestion.Visibility = System.Windows.Visibility.Visible;
  155. FinalJeopardyQuestion.Text = _finalTopic;
  156. }
  157. void timesUp_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  158. {
  159. VoidDelegate myDelegate = new VoidDelegate(delegate
  160. {
  161. AudioPlayer.Source = new Uri(ApplicationDir + @"\Music\Times up.mp3");
  162. AudioPlayer.Play();
  163. });
  164. this.Dispatcher.Invoke(DispatcherPriority.Normal, myDelegate);
  165. }
  166. private void FinalJeopardyQuestion_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  167. {
  168. if (FinalJeopardyQuestion.Text == _finalTopic)
  169. {
  170. FinalJeopardyQuestion.Text = _finalQuestion;
  171. Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
  172. Thread.Sleep(3000);
  173. AudioPlayer.Source = new Uri(ApplicationDir + @"\Music\dumdidum.mp3");
  174. AudioPlayer.Play();
  175. }else if (FinalJeopardyQuestion.Text == _finalQuestion)
  176. {
  177. FinalJeopardyQuestion.Text = _finalAnswer;
  178. }
  179. }
  180. }
  181. }