Simon Says


SUBMITTED BY: Guest

DATE: March 16, 2014, 10:49 p.m.

FORMAT: Text only

SIZE: 4.2 kB

HITS: 818

  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Collections.Generic;
  7. using System.Windows.Forms;
  8. namespace lab4SimonSays
  9. {
  10. public class SimonSays
  11. {
  12. private Color[] sequence;
  13. private Color [] colors = new Color[]{Color.Red, Color.Green, Color.Blue, Color.Yellow};
  14. private Dictionary<char, Color> stringTocolor = new Dictionary<char, Color>();
  15. public SimonSays ()
  16. {
  17. GameProj.Form1 _MainForm = new GameProj.Form1();
  18. string data = _MainForm.CallResults();
  19. TextBox textBox1= new TextBox();
  20. textBox1.Text = data; //'data' pulls the string in another project which contains randomly generates lines of 4 of R/G/B/Y
  21. //add content to Dictionary
  22. stringTocolor.Add('R', Color.Red);
  23. stringTocolor.Add('G', Color.Green);
  24. stringTocolor.Add('B', Color.Blue);
  25. stringTocolor.Add('Y', Color.Yellow);
  26. Color[] colourset = newSequence(textBox1.Text.Length);
  27. }
  28. StringCollection GetLinesCollectionFromTextBox(TextBox textBox)
  29. {
  30. StringCollection lines = new StringCollection();
  31. // lineCount may be -1 if TextBox layout info is not up-to-date.
  32. int lineCount = textBox.LineCount;
  33. for (int line = 0; line < lineCount; line++)
  34. // GetLineText takes a zero-based line index.
  35. lines.Add(textBox.GetLineText(line));
  36. return lines;
  37. }
  38. public Color[] newSequence(int length)
  39. {
  40. GameProj.Form1 _MainForm = new GameProj.Form1();
  41. string data = _MainForm.CallResults();
  42. TextBox textBox1= new TextBox();
  43. textBox1.Text = data; //Make it so that it reads each line as new sequence
  44. Color[] array = new Color[length];
  45. //check dictionary has the char key or not
  46. for (int i = 0; i < textBox1.Text.Length; i++)
  47. {
  48. if (stringTocolor.ContainsKey(textBox1.Text[i]))
  49. {
  50. array[i] = stringTocolor[textBox1.Text[i]];
  51. }
  52. //give alert if wrong key
  53. else
  54. {
  55. MessageBox.Show("Wrong Colour input at index " + i + " of textbox string!");
  56. }
  57. }
  58. this.sequence = array;
  59. return array;
  60. }
  61. public void newSequence (Color [] sequence)
  62. {
  63. this.sequence=sequence;
  64. }
  65. public int Compare (Color[] user_colors)
  66. {
  67. if(this.sequence ==null)
  68. throw new InvalidStateException("The game engine has not generated sequence yet");
  69. for (int i=0; i<user_colors.Length; i++) {
  70. if (user_colors [i] == this.sequence [i]) {
  71. //single match
  72. } else {
  73. //no match
  74. return -1;
  75. }
  76. }
  77. //all matched
  78. if (user_colors.Length == this.sequence.Length) {
  79. return 1;
  80. } else { //partial match
  81. return 0;
  82. }
  83. }
  84. public Color[] Colors {
  85. get {
  86. return colors;
  87. }
  88. }
  89. }
  90. }

comments powered by Disqus