Answer the question
In order to leave comments, you need to log in
How to remake the code (see inside)?
Hello! I can't figure out how to change this code:
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();
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Let's try together to figure out how to solve such issues.
In such cases, the first thing to do is to properly get to the root of the problem.
Your task is to remove the use of the System.Drawing namespace .
What do I need to do?
1. Remove the using header at the top
using System.Drawing;
2. Remove the reference to the assembly
Now we try to compile the project, we get errors:
error CS0246: Could not find type or namespace name 'Point' (possibly missing using directive or assembly reference)
What can replace the Point class from System.Drawing?
class Point
{
}
if (field[i, j] == "0") current = new Point(i, j);
1>error CS1061: 'Point' does not contain an 'X' definition, and no accessible extension method 'X' could be found that takes the type 'Point' as its first argument (perhaps missing a using directive or an assembly reference).
if ((i == current.X) && (j == current.Y))
1>error CS1061: 'Point' does not contain a 'Y' definition, and could not find an available extension method 'Y' that takes the type 'Point' as its first argument (perhaps missing a using directive or an assembly reference).
1>error CS1061: 'Point' does not contain a 'Y' definition, and could not find an available extension method 'Y' that takes the type 'Point' as its first argument (perhaps missing a using directive or an assembly reference).
class Point
{
public int X { get; set; }
public int Y { get; set; }
public Point(int x, int y)
{
X = x;
Y = y;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question