N
N
nikvay2020-07-31 16:10:10
C++ / C#
nikvay, 2020-07-31 16:10:10

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();
            }
        }
    }
}

What exactly needs to be redone: we need to implement the game "fifteen" without using System.Drawing. The source where I got the code already answered this problem, but I, due to the fact that I'm not experienced yet, can't figure out exactly how to do it. Please explain how I can do this or show the finished result that I could analyze. I ask for education.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Foggy Finder, 2020-07-31
@nikvay

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)

So the question can be rephrased as follows:
What can replace the Point class from System.Drawing?

Agree that the more specific the question, the easier it is to answer.
So, just one mistake means that the correction will not be much work. One Point class is missing . You can go look at the source and copy the class from there, but we'll go the other way.
Does the compiler write that a class is needed? ok let's create a class
class Point
{

}

We try to collect, there are even more errors. But you don’t need to be scared, carefully read the messages and lines that cause errors
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).

Everything seems to be clear. We are trying to use a non-existent constructor and non-existent properties.
We add everything you need. Does the compiler swear at two properties and expect to see a constructor with two parameters? Yeah, the dependence is visible - each parameter has its own property with the names X and Y , respectively.
How to determine their type? See what we are trying to convey there? i , j - integers - int . So we put it on.
class Point
{
    public int X { get; set; }
    public int Y { get; set; }

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }
}

Are we trying to compile? There are no errors, you can proceed to the health check stage.

G
GavriKos, 2020-07-31
@GavriKos

already answered this problem

so why not specify it there?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question