Пятнашки в консоли.Как менять значения массива? - C#
Формулировка задачи:
Нужна помощь.
Пишу пятнашки в консоли.
Поле 5х5.
Сделал поле-Проверил, что бы числа не повторялись-Наименьшему числу присвоил символ(это будет пустое поле).
Попробовал даже сделать метод для замены значений, но он не работает.Как-то не правильно передаю аргументы.
Проблема - как менять местами значения при нажатии клавиш управления?
Подскажите,пожалуйста.
Листинг программы
- using System;
- using System.Text;
- namespace Fifteens
- {
- class Field
- {
- public string[,] field = new string[5, 5];
- Random ran = new Random();
- public bool CheckingForRepeat(string elem)
- {
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j < 5; j++)
- {
- if (field[i, j] == elem)
- return true;
- }
- }
- return false;
- }
- public string[,] GetField()
- {
- string forCheck;
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j < 5; j++)
- {
- forCheck = Convert.ToString(ran.Next(0, 25));
- while (CheckingForRepeat(forCheck) == true)
- { forCheck = Convert.ToString(ran.Next(0, 25)); }
- field[i, j] = forCheck;
- }
- }
- Console.WriteLine();
- return field;
- }
- public void ShowField()
- {
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j < 5; j++)
- {
- if (field[i, j] == "0")
- {
- field[i, j] = "\\_/";
- Console.ForegroundColor = ConsoleColor.Red;
- Console.ResetColor();
- }
- Console.Write("{0,4}", field[i, j]);
- }
- Console.WriteLine();
- }
- }
- public void SwapArrayElements(ref string[,] a, ref string[,] b)
- {
- string[,] temp = a;
- a = b;
- b = temp;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Field field = new Field();
- field.GetField();
- field.ShowField();
- while (true)
- {
- ConsoleKeyInfo keyinfo = Console.ReadKey();
- switch (keyinfo.Key)
- {
- case ConsoleKey.UpArrow:
- break;
- case ConsoleKey.DownArrow:
- break;
- case ConsoleKey.LeftArrow:
- break;
- case ConsoleKey.RightArrow:
- break;
- }
- }
- }
- }
Решение задачи: «Пятнашки в консоли.Как менять значения массива?»
textual
Листинг программы
- using System;
- using System.Drawing;
- using System.Text;
- namespace Fifteens
- {
- class Field
- {
- public string[,] field = new string[5, 5];
- Random ran = new Random();
- Point current; //координаты пустого поля
- public bool CheckingForRepeat(string elem)
- {
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j < 5; j++)
- {
- if (field[i, j] == elem)
- return true;
- }
- }
- return false;
- }
- public string[,] GetField()
- {
- string forCheck;
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j < 5; j++)
- {
- forCheck = Convert.ToString(ran.Next(0, 25));
- while (CheckingForRepeat(forCheck) == true)
- { forCheck = Convert.ToString(ran.Next(0, 25)); }
- field[i, j] = forCheck;
- if (field[i, j] == "0") current=new Point(i,j);
- }
- }
- Console.WriteLine();
- return field;
- }
- public void ShowField()
- {
- Console.Clear();
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j < 5; j++)
- {
- if ((i == current.X) && (j==current.Y))
- {
- field[i, j] = "\\_/";
- Console.ForegroundColor = ConsoleColor.Red;
- Console.ResetColor();
- }
- Console.Write("{0,4}", field[i, j]);
- }
- Console.WriteLine();
- }
- }
- public void SwapArrayElements(ref string a, ref string b)
- {
- string temp = a;
- a = b;
- b = temp;
- }
- public void MoveRight()
- {
- if (current.Y < field.GetLength(1)-1)
- {
- SwapArrayElements(ref field[current.X, current.Y], ref field[current.X, current.Y + 1]);
- current.Y++;
- }
- }
- public void MoveLeft()
- {
- if (current.Y > 0)
- {
- SwapArrayElements(ref field[current.X, current.Y], ref field[current.X, current.Y - 1]);
- current.Y--;
- }
- }
- public void MoveUp()
- {
- if (current.X > 0)
- {
- SwapArrayElements(ref field[current.X-1, current.Y], ref field[current.X, current.Y]);
- current.X--;
- }
- }
- public void MoveDown()
- {
- if (current.X < field.GetLength(0)-1)
- {
- SwapArrayElements(ref field[current.X + 1, current.Y], ref field[current.X, current.Y]);
- current.X++;
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Field field = new Field();
- field.GetField();
- field.ShowField();
- while (true)
- {
- ConsoleKeyInfo keyinfo = Console.ReadKey();
- switch (keyinfo.Key)
- {
- case ConsoleKey.UpArrow:
- field.MoveUp();
- break;
- case ConsoleKey.DownArrow:
- field.MoveDown();
- break;
- case ConsoleKey.LeftArrow:
- field.MoveLeft();
- break;
- case ConsoleKey.RightArrow:
- field.MoveRight();
- break;
- }
- field.ShowField();
- }
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д