Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace lab1_2048
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13.  
  14. while (true)
  15. {
  16. PlayingField GameField = new PlayingField();
  17.  
  18. while (!PlayingField.EndOfThisGame)
  19. {
  20. Underground.logic(Underground.keyboard(GameField.Field), GameField.Field, PlayingField.LineWidth);
  21. }
  22.  
  23. Console.Write("Вы {0},хотите сыграть ещё?(да/нет)", PlayingField.WordOfEnd);
  24. if (Console.ReadLine() == "да")
  25. PlayingField.EndOfThisGame = false;
  26. else
  27. break;
  28.  
  29. }
  30. Console.Clear();
  31. }
  32. }
  33.  
  34. class PlayingField //добавить в конструктор метод вывода поля
  35. {
  36. public const int LineWidth = 4;
  37. static public string WordOfEnd ;
  38. static public bool EndOfThisGame = false;
  39. public int[,] Field = new int[LineWidth, LineWidth];
  40.  
  41.  
  42. public PlayingField()
  43. {
  44. Random rand = new Random();
  45. Field[rand.Next(0, 2), rand.Next(0, 4)] = rand.Next(0, 10) == 9 ? 4 : 2;
  46. Field[rand.Next(2, 4), rand.Next(0, 4)] = rand.Next(0, 10) == 9 ? 4 : 2;
  47. WordOfEnd = "проиграли";
  48. }
  49. }
  50.  
  51. class Underground
  52. {
  53. static public void ViewField(int[,] Field)
  54. {
  55. string copy0 = "|---------------|";
  56. string copy1 = "\n\t\t\t\t";
  57. Console.Write("\n\n\n\n\n\n"+copy1); Console.BackgroundColor = ConsoleColor.Red; Console.Write(copy0); Console.ResetColor();
  58. for (int i = 0; i < PlayingField.LineWidth; i++)
  59. {
  60. Console.Write(copy1); Console.BackgroundColor = ConsoleColor.Red;
  61. for (int j = 0; j < PlayingField.LineWidth; j++)
  62. {
  63. string word;
  64. if (Field[i, j] == 0)
  65. word = " ";
  66. else
  67. word =Convert.ToString( Field[i, j]);
  68. if(Field[i,j]<9){
  69. Console.Write("| {0} " , word);
  70. }
  71. else if (Field[i, j] > 9 && Field[i, j] < 100) {
  72. Console.Write("| {0}", word);
  73. }
  74. else
  75. Console.Write("|{0}", word);
  76. }
  77. Console.Write("|"); Console.ResetColor();
  78. Console.Write(copy1); Console.BackgroundColor = ConsoleColor.Red; Console.Write(copy0); Console.ResetColor();
  79. }
  80. Console.Write(copy1);
  81. }
  82.  
  83. static public int keyboard( int[,] Field)//вывод игрового поля и чтение клавиши
  84. {
  85. while (true)
  86. { ViewField(Field);
  87. ConsoleKeyInfo key = Console.ReadKey();
  88. Console.Clear();
  89. if ( key.Key == ConsoleKey.W || key.Key == ConsoleKey.UpArrow)
  90. return 1;
  91. else if (key.Key == ConsoleKey.S || key.Key == ConsoleKey.DownArrow)
  92. return 2;
  93. else if (key.Key == ConsoleKey.A || key.Key == ConsoleKey.LeftArrow)
  94. return 3;
  95. else if (key.Key == ConsoleKey.D || key.Key == ConsoleKey.RightArrow)
  96. return 4;
  97.  
  98. }
  99. }
  100.  
  101. static public void logic(int num,int[,] field,int n)// доделать передачу по ссылке
  102. {
  103.  
  104. switch (num)
  105. {
  106. case 1:// up
  107. for (int i = 1; i < n; i++)
  108. {
  109. for (int j = 1; j < n; j++)
  110. {
  111.  
  112. }
  113. }
  114. break;
  115.  
  116. case 2:// down
  117. for (int i = 1; i < n; i++)
  118. {
  119. for (int j = 1; j< n; j++)
  120. {
  121.  
  122. }
  123. }
  124. break;
  125. case 3:// left
  126. for (int c = 0; c < n * 2; c++)
  127. {
  128. for (int i = 0; i < n; i++)
  129. {
  130. for (int j = 1; j < n; j++)
  131. {
  132. if ((field[i, j - 1] == field[i, j]) ^ (field[i, j - 1] == 0 && field[i, j] != 0))
  133. {
  134. field[i, j - 1] += field[i, j];
  135. field[i, j] = 0;
  136. }
  137.  
  138. }
  139. }
  140. }
  141. break;
  142. case 4:// right
  143. for (int c = 0; c < n * 2; c++)
  144. {
  145. for (int i = 0; i < n; i++)
  146. {
  147. for (int j = n-1; j >0; j--)
  148. {
  149. if ((field[i, j - 1] == field[i, j]) ^ (field[i, j - 1] != 0 && field[i, j] == 0))
  150. {
  151. field[i, j ] += field[i, j-1];
  152. field[i, j-1] = 0;
  153. }
  154.  
  155. }
  156. }
  157. }
  158. break;
  159. default:
  160. Console.WriteLine("хз как но вышла ошибка");
  161. break;
  162.  
  163. }
  164. Add(field,n);
  165.  
  166. }
  167.  
  168. static public void Add(int[,] Field,int n)//доделать
  169. {
  170. int numer = 0;
  171. Random rand = new Random();
  172. for(int i = 0; i < n; i++)
  173. {
  174. for (int j = 0; j < n; j++)
  175. {
  176. if (!(Field[i, j] == 0))
  177. numer++;
  178.  
  179. if ( Field[i,j]==2048)
  180. {
  181. PlayingField.WordOfEnd = "выйграли";
  182. PlayingField.EndOfThisGame = true;
  183. return;
  184.  
  185. }
  186. }
  187. }
  188. if (numer == 15)
  189. {
  190. PlayingField.EndOfThisGame = true;
  191. return;
  192. }
  193. Console.WriteLine(n);
  194. while (true)
  195. {
  196. int i = rand.Next(0, 4), j = i = rand.Next(0, 4);
  197. if (Field[i, j] == 0)
  198. {
  199. Field[i, j] = rand.Next(0, 10) == 9 ? 4 : 2;
  200. return;
  201. }
  202. }
  203. }
  204. }
  205. }
  206. /*тест вывода
  207. * Random rand = new Random();
  208. int[,] f = new int[4, 4];
  209. for(int o = 0; o < 4; o++)
  210. {
  211. for (int i = 0; i < 4; i++)
  212. f[o, i] = rand.Next(0, 5);
  213. }
  214. Underground.ViewField(f);
  215. Console.ReadKey();
  216. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement