Помогите разбить программу на функции - C#
Формулировка задачи:
Здравствуйте помоги пожалуйста разбить "программу" на функции.
Вот оригинал
А вот мои потуги но все они четны ((( пытался использовать return но что-то не получается (
Листинг программы
- static void Main(string[] args)
- {
- Console.Title = "Dan-5";
- Console.CursorVisible = false;
- Random r = new Random();
- int a = r.Next(Console.BufferWidth - 1);
- int b = r.Next(24);
- Console.SetCursorPosition(a, b);
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write((char)1);
- int x = 1;
- int y = 1;
- Console.SetCursorPosition(x, y);
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write((char)2);
- while (true)
- {
- switch (Console.ReadKey().Key)
- {
- case ConsoleKey.UpArrow:
- {
- y--;
- y = y < 0 ? 0 : y;
- }
- break;
- case ConsoleKey.DownArrow:
- {
- y++;
- }
- break;
- case ConsoleKey.LeftArrow:
- {
- x--;
- x = x <= 0 ? 0 : x;
- }
- break;
- case ConsoleKey.RightArrow:
- {
- x++;
- x = x >= Console.BufferWidth ?
- Console.BufferWidth - 1 : x;
- }
- break;
- case ConsoleKey.Escape:
- {
- return;
- }
- #region 2
- // Управление вторым человечком
- case ConsoleKey.A:
- {
- a--;
- }
- break;
- case ConsoleKey.S:
- {
- a++;
- }
- break;
- case ConsoleKey.Z:
- {
- b++;
- }
- break;
- case ConsoleKey.W:
- {
- b--;
- }
- break;
- #endregion
- default: break;
- }
- Console.Clear();
- Console.SetCursorPosition(a, b);
- Console.Write((char)1);
- Console.SetCursorPosition(x, y);
- Console.Write((char)2);
- if ((a == x) && (b == y))
- {
- Console.Clear();
- int l = 20;
- int t = 5;
- int w = 50;
- int h = 20;
- Console.SetCursorPosition(l, t);
- Console.BackgroundColor = ConsoleColor.Red;
- for (int i = l; i <= w; i++)
- for (int j = t; j <= h; j++)
- {
- Console.SetCursorPosition(i, j);
- Console.Write(" ");
- }
- Console.SetCursorPosition(w / 2 + 3, h / 2 + 3);
- Console.WriteLine("GAME OWER !");
- break;
- }
- }
- Console.ReadKey();
- }
Листинг программы
- static int a, b, x, y;
- static void Main(string[] args)
- {
- Console.Title = "Dan-5";
- Begin();
- Gameplay();
- Cons();
- GameOver();
- Console.ReadKey();
- }
- static int Begin()
- {
- Console.CursorVisible = false;
- Random r = new Random();
- a = r.Next(Console.BufferWidth - 1);
- b = r.Next(24);
- Console.SetCursorPosition(a, b);
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write((char)1);
- x = 1;
- y = 1;
- Console.SetCursorPosition(x, y);
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write((char)2);
- while (true);
- }
- static void Gameplay()
- {
- switch (Console.ReadKey().Key)
- {
- case ConsoleKey.UpArrow:
- {
- y--;
- y = y < 0 ? 0 : y;
- }
- break;
- case ConsoleKey.DownArrow:
- {
- y++;
- }
- break;
- case ConsoleKey.LeftArrow:
- {
- x--;
- x = x <= 0 ? 0 : x;
- }
- break;
- case ConsoleKey.RightArrow:
- {
- x++;
- x = x >= Console.BufferWidth ?
- Console.BufferWidth - 1 : x;
- }
- break;
- case ConsoleKey.Escape:
- {
- return;
- }
- #region 2
- // Управление вторым человечком
- case ConsoleKey.A:
- {
- a--;
- }
- break;
- case ConsoleKey.S:
- {
- a++;
- }
- break;
- case ConsoleKey.Z:
- {
- b++;
- }
- break;
- case ConsoleKey.W:
- {
- b--;
- }
- break;
- #endregion
- default: break;
- }
- }
- static void Cons()
- {
- Console.Clear();
- Console.SetCursorPosition(a, b);
- Console.Write((char)1);
- Console.SetCursorPosition(x, y);
- Console.Write((char)2);
- }
- static void GameOver()
- {
- if ((a == x) && (b == y))
- Console.Clear();
- int l = 20;
- int t = 5;
- int w = 50;
- int h = 20;
- Console.SetCursorPosition(l, t);
- Console.BackgroundColor = ConsoleColor.Red;
- for (int i = l; i <= w; i++)
- for (int j = t; j <= h; j++)
- {
- Console.SetCursorPosition(i, j);
- Console.Write(" ");
- }
- Console.SetCursorPosition(w / 2 + 3, h / 2 + 3);
- Console.WriteLine("GAME OWER !");
- }
- }
- }
Решение задачи: «Помогите разбить программу на функции»
textual
Листинг программы
- while (true); из Begin() убрать вствить в Gameplay()
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д