Guest User

SimonSays code

a guest
Mar 16th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  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.  
  9. namespace lab4SimonSays
  10. {
  11. public class SimonSays
  12. {
  13.  
  14. private Color[] sequence;
  15. private Color [] colors = new Color[]{Color.Red, Color.Green, Color.Blue, Color.Yellow};
  16. private Dictionary<char, Color> stringTocolor = new Dictionary<char, Color>();
  17.  
  18. public SimonSays ()
  19. {
  20. GameProj.Form1 _MainForm = new GameProj.Form1();
  21. string data = _MainForm.CallResults();
  22.  
  23. TextBox textBox1= new TextBox();
  24. textBox1.Text = data; //'data' pulls the string in another project which contains randomly generates lines of 4 of R/G/B/Y
  25.  
  26. //add content to Dictionary
  27. stringTocolor.Add('R', Color.Red);
  28. stringTocolor.Add('G', Color.Green);
  29. stringTocolor.Add('B', Color.Blue);
  30. stringTocolor.Add('Y', Color.Yellow);
  31.  
  32. Color[] colourset = newSequence(textBox1.Text.Length);
  33.  
  34. }
  35.  
  36. StringCollection GetLinesCollectionFromTextBox(TextBox textBox)
  37. {
  38. StringCollection lines = new StringCollection();
  39.  
  40. // lineCount may be -1 if TextBox layout info is not up-to-date.
  41. int lineCount = textBox.LineCount;
  42.  
  43. for (int line = 0; line < lineCount; line++)
  44. // GetLineText takes a zero-based line index.
  45. lines.Add(textBox.GetLineText(line));
  46.  
  47. return lines;
  48. }
  49.  
  50. public Color[] newSequence(int length)
  51. {
  52. GameProj.Form1 _MainForm = new GameProj.Form1();
  53. string data = _MainForm.CallResults();
  54.  
  55. TextBox textBox1= new TextBox();
  56. textBox1.Text = data; //Make it so that it reads each line as new sequence
  57.  
  58. Color[] array = new Color[length];
  59. //check dictionary has the char key or not
  60.  
  61. for (int i = 0; i < textBox1.Text.Length; i++)
  62. {
  63. if (stringTocolor.ContainsKey(textBox1.Text[i]))
  64. {
  65. array[i] = stringTocolor[textBox1.Text[i]];
  66. }
  67. //give alert if wrong key
  68. else
  69. {
  70. MessageBox.Show("Wrong Colour input at index " + i + " of textbox string!");
  71. }
  72. }
  73. this.sequence = array;
  74. return array;
  75. }
  76.  
  77.  
  78. public void newSequence (Color [] sequence)
  79. {
  80. this.sequence=sequence;
  81.  
  82. }
  83.  
  84. public int Compare (Color[] user_colors)
  85. {
  86. if(this.sequence ==null)
  87. throw new InvalidStateException("The game engine has not generated sequence yet");
  88. for (int i=0; i<user_colors.Length; i++) {
  89. if (user_colors [i] == this.sequence [i]) {
  90. //single match
  91. } else {
  92. //no match
  93. return -1;
  94. }
  95. }
  96.  
  97. //all matched
  98. if (user_colors.Length == this.sequence.Length) {
  99. return 1;
  100. } else { //partial match
  101. return 0;
  102. }
  103. }
  104.  
  105. public Color[] Colors {
  106. get {
  107. return colors;
  108. }
  109. }
  110.  
  111.  
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment