Помогите разбить программу на функции - C#

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

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

Здравствуйте помоги пожалуйста разбить "программу" на функции. Вот оригинал
Листинг программы
  1. static void Main(string[] args)
  2. {
  3.  
  4. Console.Title = "Dan-5";
  5. Console.CursorVisible = false;
  6. Random r = new Random();
  7. int a = r.Next(Console.BufferWidth - 1);
  8. int b = r.Next(24);
  9. Console.SetCursorPosition(a, b);
  10. Console.ForegroundColor = ConsoleColor.Yellow;
  11. Console.Write((char)1);
  12. int x = 1;
  13. int y = 1;
  14. Console.SetCursorPosition(x, y);
  15. Console.ForegroundColor = ConsoleColor.Yellow;
  16. Console.Write((char)2);
  17. while (true)
  18. {
  19. switch (Console.ReadKey().Key)
  20. {
  21. case ConsoleKey.UpArrow:
  22. {
  23. y--;
  24. y = y < 0 ? 0 : y;
  25. }
  26. break;
  27. case ConsoleKey.DownArrow:
  28. {
  29. y++;
  30. }
  31. break;
  32. case ConsoleKey.LeftArrow:
  33. {
  34. x--;
  35. x = x <= 0 ? 0 : x;
  36. }
  37. break;
  38. case ConsoleKey.RightArrow:
  39. {
  40. x++;
  41. x = x >= Console.BufferWidth ?
  42. Console.BufferWidth - 1 : x;
  43. }
  44. break;
  45. case ConsoleKey.Escape:
  46. {
  47. return;
  48. }
  49. #region 2
  50. // Управление вторым человечком
  51. case ConsoleKey.A:
  52. {
  53. a--;
  54. }
  55. break;
  56. case ConsoleKey.S:
  57. {
  58. a++;
  59. }
  60. break;
  61. case ConsoleKey.Z:
  62. {
  63. b++;
  64. }
  65. break;
  66. case ConsoleKey.W:
  67. {
  68. b--;
  69. }
  70. break;
  71. #endregion
  72. default: break;
  73. }
  74. Console.Clear();
  75. Console.SetCursorPosition(a, b);
  76. Console.Write((char)1);
  77. Console.SetCursorPosition(x, y);
  78. Console.Write((char)2);
  79. if ((a == x) && (b == y))
  80. {
  81. Console.Clear();
  82. int l = 20;
  83. int t = 5;
  84. int w = 50;
  85. int h = 20;
  86. Console.SetCursorPosition(l, t);
  87. Console.BackgroundColor = ConsoleColor.Red;
  88. for (int i = l; i <= w; i++)
  89. for (int j = t; j <= h; j++)
  90. {
  91. Console.SetCursorPosition(i, j);
  92. Console.Write(" ");
  93. }
  94. Console.SetCursorPosition(w / 2 + 3, h / 2 + 3);
  95. Console.WriteLine("GAME OWER !");
  96. break;
  97. }
  98. }
  99. Console.ReadKey();
  100. }
А вот мои потуги но все они четны ((( пытался использовать return но что-то не получается (
Листинг программы
  1. static int a, b, x, y;
  2. static void Main(string[] args)
  3. {
  4. Console.Title = "Dan-5";
  5. Begin();
  6. Gameplay();
  7. Cons();
  8. GameOver();
  9. Console.ReadKey();
  10. }
  11. static int Begin()
  12. {
  13. Console.CursorVisible = false;
  14. Random r = new Random();
  15. a = r.Next(Console.BufferWidth - 1);
  16. b = r.Next(24);
  17. Console.SetCursorPosition(a, b);
  18. Console.ForegroundColor = ConsoleColor.Yellow;
  19. Console.Write((char)1);
  20. x = 1;
  21. y = 1;
  22. Console.SetCursorPosition(x, y);
  23. Console.ForegroundColor = ConsoleColor.Yellow;
  24. Console.Write((char)2);
  25. while (true);
  26. }
  27. static void Gameplay()
  28. {
  29. switch (Console.ReadKey().Key)
  30. {
  31. case ConsoleKey.UpArrow:
  32. {
  33. y--;
  34. y = y < 0 ? 0 : y;
  35. }
  36. break;
  37. case ConsoleKey.DownArrow:
  38. {
  39. y++;
  40. }
  41. break;
  42. case ConsoleKey.LeftArrow:
  43. {
  44. x--;
  45. x = x <= 0 ? 0 : x;
  46. }
  47. break;
  48. case ConsoleKey.RightArrow:
  49. {
  50. x++;
  51. x = x >= Console.BufferWidth ?
  52. Console.BufferWidth - 1 : x;
  53. }
  54. break;
  55. case ConsoleKey.Escape:
  56. {
  57. return;
  58. }
  59. #region 2
  60. // Управление вторым человечком
  61. case ConsoleKey.A:
  62. {
  63. a--;
  64. }
  65. break;
  66. case ConsoleKey.S:
  67. {
  68. a++;
  69. }
  70. break;
  71. case ConsoleKey.Z:
  72. {
  73. b++;
  74. }
  75. break;
  76. case ConsoleKey.W:
  77. {
  78. b--;
  79. }
  80. break;
  81. #endregion
  82. default: break;
  83. }
  84. }
  85. static void Cons()
  86. {
  87. Console.Clear();
  88. Console.SetCursorPosition(a, b);
  89. Console.Write((char)1);
  90. Console.SetCursorPosition(x, y);
  91. Console.Write((char)2);
  92. }
  93. static void GameOver()
  94. {
  95. if ((a == x) && (b == y))
  96. Console.Clear();
  97. int l = 20;
  98. int t = 5;
  99. int w = 50;
  100. int h = 20;
  101. Console.SetCursorPosition(l, t);
  102. Console.BackgroundColor = ConsoleColor.Red;
  103. for (int i = l; i <= w; i++)
  104. for (int j = t; j <= h; j++)
  105. {
  106. Console.SetCursorPosition(i, j);
  107. Console.Write(" ");
  108. }
  109. Console.SetCursorPosition(w / 2 + 3, h / 2 + 3);
  110. Console.WriteLine("GAME OWER !");
  111. }
  112.  
  113. }
  114. }

Решение задачи: «Помогите разбить программу на функции»

textual
Листинг программы
  1.  while (true); из Begin() убрать вствить в Gameplay()

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


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

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

7   голосов , оценка 4.143 из 5

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

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

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