Guest User

Untitled

a guest
Nov 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. //Game3.cs
  2. using System;
  3. using System.Collections.Generic;
  4.  
  5. namespace Pyatnyasky_v2
  6. {
  7. class Game3 : Game2
  8. {
  9. Stack<Points> undo = new Stack<Points>();
  10. Stack<Points> redo = new Stack<Points>();
  11. public Game3(int[] point) : base(point)
  12. {
  13. }
  14.  
  15. public void History(int value)
  16. {
  17. undo.Push(GetLocation(value));
  18. }
  19. public void Undo()
  20. {
  21. if (undo.Count > 0)
  22. {
  23. var point = undo.Pop();
  24. this.Move(point.x, point.y], this); //Вот здесь среда показывает синтаксическа ошибка
  25. redo.Push(point);
  26. }
  27. else throw new Exception("Unable to cancel");
  28. }
  29.  
  30. public void Redo()
  31. {
  32. if (redo.Count > 0)
  33. {
  34. var point = redo.Pop();
  35. this.Move(point.x, point.y], this); //Вот здесь среда показывает синтаксическа ошибка
  36. undo.Push(point);
  37. }
  38. }
  39.  
  40. }
  41. }
  42.  
  43.  
  44. //Game.cs
  45. using System;
  46. namespace Pyatnyasky
  47. {
  48. class Points
  49. {
  50.  
  51. public int x, y;
  52. public Points(int x, int y)
  53. {
  54. this.x = x;
  55. this.y = y;
  56. }
  57. }
  58. class Game
  59. {
  60.  
  61. public int[] point = new int[16];
  62. public int Length = 0;
  63.  
  64. public static int[] ArrayText = new int[16];
  65. public const int width = 4, height = 4;
  66. public int[,] field = new int[width, height];
  67. public Points[] FieldValue = new Points[16];
  68.  
  69. public Game(int[] point)
  70. {
  71.  
  72. int r = 0;
  73.  
  74. Length = width * height;
  75.  
  76. for (int j = 0; j < height; j++)
  77. {
  78. for (int i = 0; i < width; i++)
  79. {
  80. field[j, i] = point[r];
  81. FieldValue[point[r]] = new Points(j, i);
  82. r++;
  83.  
  84. }
  85. }
  86. }
  87.  
  88. public int this[int x, int y]
  89. {
  90. get
  91. {
  92. if (x < 0 || x >= width * height || y < 0 || y >= width * height)
  93. {
  94. throw new ArgumentOutOfRangeException("the indexes do not fit");
  95. }
  96. return field[x, y];
  97. }
  98. }
  99.  
  100. public Points GetLocation(int value)
  101. {
  102.  
  103. return FieldValue[value];
  104. }
  105. public void drawField()
  106. {
  107. Console.WriteLine("____________________________________");
  108. for (int i = 0; i < width; i++)
  109. {
  110. for (int j = 0; j < height; j++)
  111. {
  112. Console.Write(field[i, j] + "t");
  113. }
  114. Console.WriteLine();
  115. }
  116. Console.WriteLine("____________________________________");
  117.  
  118. }
  119. public void Move(int value, Game3 obj)
  120. {
  121. try
  122. {
  123. if (value > 15 || value < 0)
  124. {
  125. throw new ArgumentException();
  126. }
  127. int x = GetLocation(0).x;
  128. int y = GetLocation(0).y;
  129.  
  130. int X = GetLocation(value).x;
  131. int Y = GetLocation(value).y;
  132.  
  133. if ((X == x && (Y == y - 1 || Y == y + 1)) || (Y == y && (X == x - 1 || X == x + 1)))
  134. {
  135. field[x, y] = value;
  136. field[X, Y] = 0;
  137.  
  138. var vere = FieldValue[0];
  139. FieldValue[0] = FieldValue[value];
  140. FieldValue[value] = vere;
  141.  
  142. obj.History(value);
  143. }
  144. else
  145. {
  146. throw new Exception();
  147. }
  148. }
  149. catch (ArgumentException)
  150. {
  151. Console.WriteLine("There is no such number: ");
  152. }
  153. catch (Exception)
  154. {
  155. Console.WriteLine("Along with this number is not 0: ");
  156. }
  157.  
  158. }
  159. }
  160. }
Add Comment
Please, Sign In to add comment