Добавить использование шаблонов проектирования в игру "Пятнашки" - C#

Узнай цену своей работы

Формулировка задачи:

Есть реализация игры пятнашки, нужно сделать откат предыдущего хода, выбор уровня сложности (рохмир игрового поля 3х3, 4х4), с использованием паттернов Синглетон, Фабричный метод, Команда, Хранитель, помогите.
Листинг программы
  1. using System;
  2. //Game.cs
  3. namespace puzzle15
  4. {
  5. class Points
  6. {
  7. public readonly int x, y;
  8. public Points(int x, int y)
  9. {
  10. this.x = x;
  11. this.y = y;
  12. }
  13. }
  14. class Game
  15. {
  16. public int Length=0;
  17. const int nw = 4, height = 4;
  18. int[,] field = new int[nw, height];
  19. Points[] FieldValue = new Points[16];
  20.  
  21. public Game(int[] point)
  22. {
  23. int r = 0;
  24. string[] file = new string[4];
  25. Length = 16;
  26. mixer(point);
  27. for (int j = 0; j < height; j++)
  28. {
  29. for (int i = 0; i < nw; i++)
  30. {
  31. field[j, i] = point[r];
  32. FieldValue[point[r]] = new Points(j, i);
  33. r++;
  34. }
  35. }
  36. }
  37. public void mixer(int[] p)
  38. {
  39. int tmp = 0;
  40. Random rnd = new Random();
  41. for (int i = 0; i < 16; i++)
  42. {
  43. bool isExist = false;
  44. do
  45. {
  46. isExist = false;
  47. tmp = rnd.Next(0, 16);
  48. for (int j = 0; j < i; j++)
  49. {
  50. if (tmp == p[j]) { isExist = true; }
  51. }
  52. }
  53. while (isExist);
  54. p[i] = tmp;
  55. }
  56. }
  57. private Points GetLocation(int value)
  58. {
  59. return FieldValue[value];
  60. }
  61.  
  62. public void drawField()
  63. {
  64. Console.WriteLine("----------------------------");
  65. for (int i = 0; i < nw; i++)
  66. {
  67. for (int j = 0; j < height; j++)
  68. {
  69. Console.Write(field[i, j] + "\t");
  70. }
  71. Console.WriteLine();
  72. }
  73. Console.WriteLine("----------------------------");
  74. }
  75. public bool repeat(double Length,int[] point)
  76. {
  77. for (int i = 0; i < Length; ++i)
  78. {
  79. for (int y = i + 1; y < Length; ++y)
  80. {
  81. if (point[i] == point[y])
  82. {
  83. Console.WriteLine(point[i] + " ==" + point[y]);
  84. throw new ArgumentException("Numbers should not be repeated");
  85. }
  86.  
  87. }
  88. }
  89. return true;
  90. }
  91. public Boolean finish()
  92. {
  93. bool temp = false;
  94. int value = 1;
  95. for (int i = 0; i < nw; ++i)
  96. {
  97. for (int j = 0; j < height; ++j)
  98. {
  99. if (field[i, j] == value)
  100. {
  101. temp = true;
  102. ++value;
  103. if (value == Length)
  104. {
  105. value = 0;
  106. }
  107. }
  108. else
  109. {
  110. return false;
  111. }
  112. }
  113. }
  114. return temp;
  115. }
  116. public void Move(int value)
  117. {
  118. try
  119. {
  120. Console.WriteLine(value);
  121. if (value > 15 || value < 0)
  122. {
  123. throw new ArgumentException();
  124. }
  125. int x = GetLocation(0).x;
  126. int y = GetLocation(0).y;
  127. int ValueX = GetLocation(value).x;
  128. int ValueY = GetLocation(value).y;
  129. if ((ValueX == x && (ValueY == y - 1 || ValueY == y + 1))||(ValueY == y && (ValueX == x - 1 || ValueX == x + 1)))
  130. {
  131. field[x, y] = value;
  132. field[ValueX, ValueY] = 0;
  133. var vere = FieldValue[0];
  134. FieldValue[0] = FieldValue[value];
  135. FieldValue[value] = vere;
  136. }
  137. }
  138. catch (ArgumentException)
  139. {
  140. Console.WriteLine("There is no such number, try again: ");
  141. }
  142. catch (Exception)
  143. {
  144. Console.WriteLine("Next to this number is not 0, try again: ");
  145. }
  146. }
  147. }
  148.  
  149. //--------------------------------------------------------------------------------------------------------------------------------------------------------
  150.  
  151. class Points2
  152. {
  153. ..............................................................................................
  154. }
  155. class Game2
  156. {
  157. public int Length = 0;
  158. const int nw = 3, height = 3;
  159. int[,] field = new int[nw, height];
  160. Points[] FieldValue = new Points[9];
  161.  
  162. public Game2(int[] point)
  163. {
  164. int r = 0;
  165. string[] file = new string[3];
  166. Length = 9;
  167. mixer(point);
  168. for (int j = 0; j < height; j++)
  169. {
  170. for (int i = 0; i < nw; i++)
  171. {
  172. field[j, i] = point[r];
  173. FieldValue[point[r]] = new Points(j, i);
  174. r++;
  175. }
  176. }
  177. }
  178. .................................................................................................
  179. }
  180. }
  181.  
  182. //Program.cs
  183. using System;
  184. using System.Collections.Generic;
  185.  
  186. namespace puzzle15
  187. ...............................................................................................
  188. //вот код начало хранителя
  189. class GameHistory
  190. {
  191. public Stack<Game> History { get; private set; }
  192. public GameHistory()
  193. {
  194. History = new Stack<Game>();
  195. }
  196. }
  197.  
  198. }

Решение задачи: «Добавить использование шаблонов проектирования в игру "Пятнашки"»

textual
Листинг программы
  1. //Program.cs
  2. using System;
  3.  
  4. namespace _15puzzle
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int i;
  11.             int[] p = new int[100];
  12.  
  13.             for (i = 0; i < 16; i++)
  14.             {
  15.                 p[i] = i + 1;
  16.             }
  17.  
  18.             p[15] = 0;
  19.  
  20.  
  21.             Console.WriteLine("15 puzzle");
  22.      
  23.  
  24.          
  25.  
  26.             Game MyGame = new Game(p);
  27.             int score = 0;
  28.          
  29.             MyGame.mixer(p);
  30.  
  31.             for (; ; )
  32.             {
  33.                 MyGame.drawField();
  34.                
  35.                 Console.WriteLine("Select number: ");
  36.  
  37.                 int a=Convert.ToInt32(Console.ReadLine());
  38.  
  39.                MyGame.Move(a);
  40.  
  41.                 if (MyGame.finish())
  42.                 {
  43.                     MyGame.drawField();
  44.                     Console.WriteLine("Winner!");
  45.                     Console.WriteLine("Game end");
  46.                     break;
  47.                 }
  48.                 score++;
  49.  
  50.                 Console.WriteLine("Count: " + score);
  51.  
  52.              
  53.             }
  54.         }
  55.  
  56.        
  57.     }
  58. }
  59.  
  60. //Game.cs
  61.  
  62. using System;
  63. using System.IO;
  64.  
  65. namespace _15puzzle
  66. {
  67.     class Game
  68.     {
  69.    
  70.         int[] point=new int[100];
  71.         public int Length=0;
  72.  
  73.         public static int[] ArrayText=new int [100];
  74.         const int nw = 4, height = 4;
  75.         int[,] field = new int[nw, height];
  76.         Points[] FieldValue = new Points[100];
  77.          
  78.  
  79.         public Game(int[] point)
  80.         {
  81.             int ruru = 0;
  82.             string[] file = new string[4];
  83.            Length = TextSCV();
  84.          
  85.             mixer(ArrayText);
  86.  
  87.             for (int j = 0; j < height; j++)
  88.             {
  89.                 for (int i = 0; i < nw; i++)
  90.                 {
  91.                     field[j, i] = ArrayText[ruru];
  92.                     FieldValue[ArrayText[ruru]] = new Points(j, i);
  93.                     ruru++;
  94.  
  95.                 }
  96.             }
  97.          
  98.         }
  99.  
  100.         public void mixer(int[] p)
  101.         {
  102.  
  103.             int tmp = 0;
  104.  
  105.             Random rnd = new Random();
  106.  
  107.             for (int i = 0; i < 16; i++)
  108.             {
  109.                 bool isExist = false;
  110.                 do
  111.                 {
  112.                     isExist = false;
  113.                     tmp = rnd.Next(0, 16);
  114.                     for (int j = 0; j < i; j++)
  115.                     {
  116.                         if (tmp == p[j]) { isExist = true; }
  117.                     }
  118.                 }
  119.                 while (isExist);
  120.                 p[i] = tmp;
  121.             }
  122.         }
  123.  
  124.         public int this[int x, int y]
  125.         {      
  126.             get
  127.             {
  128.                 return field[x, y];
  129.             }
  130.             set
  131.             {
  132.                 field[x, y] = value;
  133.             }
  134.         }
  135.  
  136.  
  137.         private Points GetLocation(int value)
  138.         {
  139.  
  140.             return FieldValue[value];
  141.         }
  142.  
  143.  
  144.         public void drawField()
  145.         {
  146.  
  147.             Console.Write("~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + "\n");
  148.             for (int i = 0; i < nw; i++)
  149.             {
  150.                 for (int j = 0; j < height; j++)
  151.                 {
  152.                     Console.Write(field[i, j] + "\t");
  153.  
  154.                 }
  155.                 Console.WriteLine();
  156.  
  157.             }
  158.  
  159.             Console.Write("~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + "\n");
  160.  
  161.         }
  162.  
  163.         public bool repeat(double Length,int[] ArrayText)
  164.         {
  165.  
  166.             for (int i = 0; i < Length; ++i)
  167.             {
  168.                 for (int y = i + 1; y < Length; ++y)
  169.                 {
  170.                     if (ArrayText[i] == ArrayText[y])
  171.                     {
  172.                         Console.WriteLine(ArrayText[i] + " ==" + ArrayText[y]);
  173.                         throw new ArgumentException("Number not repet! ");
  174.                     }
  175.  
  176.  
  177.                 }
  178.             }
  179.             return true;
  180.         }
  181.  
  182.         public Boolean finish()
  183.         {
  184.             bool temp = false;
  185.             int value = 1;
  186.             for (int i = 0; i < nw; ++i)
  187.             {
  188.                 for (int j = 0; j < height; ++j)
  189.                 {
  190.                     if (field[i, j] == value)
  191.                     {
  192.  
  193.                         temp = true;
  194.                         ++value;
  195.                         if (value == Length)
  196.                         {
  197.                             value = 0;
  198.                         }
  199.                     }
  200.                     else
  201.                     {
  202.                         return false;
  203.                     }
  204.  
  205.                 }
  206.  
  207.             }      
  208.             return temp;
  209.  
  210.         }
  211.  
  212.         public void Move(int value)
  213.         {
  214.  
  215.             try
  216.             {
  217.             Console.WriteLine(value);
  218.             if (value > 15 || value < 0)
  219.             {
  220.                 throw new ArgumentException();
  221.             }
  222.  
  223.             int x = GetLocation(0).x;
  224.             int y = GetLocation(0).y;
  225.  
  226.             int ValueX = GetLocation(value).x;
  227.             int ValueY = GetLocation(value).y;
  228.  
  229.                 if ((ValueX == x && (ValueY == y - 1 || ValueY == y + 1))||(ValueY == y && (ValueX == x - 1 || ValueX == x + 1)))
  230.                 {
  231.  
  232.                     field[x, y] = value;
  233.                     field[ValueX, ValueY] = 0;
  234.  
  235.                     var vere = FieldValue[0];
  236.                     FieldValue[0] = FieldValue[value];
  237.                     FieldValue[value] = vere;
  238.                 }
  239.  
  240.                 else
  241.                 {
  242.                     throw new Exception();
  243.                     //Console.WriteLine("Некуда двигать. ");
  244.                 }
  245.             }
  246.  
  247.             catch (ArgumentException)
  248.             {
  249.                 Console.WriteLine("Numer is not: ");
  250.             }
  251.             catch (Exception)
  252.             {
  253.                 Console.WriteLine("Number: ");
  254.             }
  255.            
  256.         }
  257.  
  258.         public static int TextSCV()
  259.         {
  260.             int ruru = 0;
  261.            
  262.                 string[] text = File.ReadAllLines(@"text.csv");
  263.                 Char place = ' ';
  264.              
  265.                 for (int i = 0; i < text.Length; ++i)
  266.                 {
  267.                     string[] row = text[i].Split(place);
  268.                     foreach (var substring in row)
  269.                     {
  270.                         ArrayText[ruru] = Convert.ToInt32(substring);
  271.                         //Console.WriteLine(ArrayText[ruru]);
  272.                         ++ruru;
  273.                     }
  274.                 }
  275.                
  276.            
  277.             return ruru;
  278.         }
  279.     }
  280. }
  281.  
  282. //Point.cs
  283. namespace _15puzzle
  284. {
  285.     class Points
  286.     {
  287.         public readonly int x, y;
  288.         public Points(int x, int y)
  289.         {
  290.             this.x = x;
  291.             this.y = y;
  292.         }
  293.     }
  294. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

14   голосов , оценка 3.714 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы