It was necessary to solve the problem of bypassing the knight of the N * N board and display the solution. The algorithm is simple, I implemented it, but with the conclusion there were problems. The practitioner requires that the output be "through the interface." Unfortunately, I can’t say anything more specifically. I do not understand what is required. Suddenly, someone knows what it means, and will be able to explain, although I understand that there is little chance. Here is the console output function that does not satisfy it.

private static void Print(int boardSize, int[,] board) { for (int x = 0; x < boardSize; x++) { for (int y = 0; y < boardSize; y++) { Console.Write("{0:D2} ", board[x, y]); } Console.WriteLine(); } } 

All I understood about the interfaces and this task is that it is required to create a separate class, to inherit it. Here is the only thing that I understood

 interface Printer { void Print(string toPrint); } class ConsolePrinter : Printer { public static void Print(string format, object arg) { Console.Write(format, arg); } } 

How now it (if it is of course correct) to transfer to the first function? Please do not zaminusovyvat, just write in the comments that I didn’t explain, try to tell. If the question is not logical at all, also do not minus pliz, but just warn

  • Can a practitioner confuse you and mean gui? - user5074
  • No, it is not GUIshnoe, but somehow through the interface - Alexander Kavokin
  • one
    - it is accepted to call interfaces with the letter I => IPrinter - in your case, the interface implementation looks somehow wrong - the Print method takes one argument in the interface, and for some reason, in the implementation 2 , Specter

3 answers 3

You are right, you can inherit from the interface class, and this is in general its main function! Since no functions in the interface are implemented! Judging by your task, you need to write an interface in which your Print function will be declared, and then create your own class, inherit it from this interface and write the concrete implementation of the Print function in it (class)! I think it should look something like this:

  interface Printer { void Print(int boardSize, int[,] board); } class ConsolePrinter : Printer { public void Print(int boardSize, int[,] board) { for (int x = 0; x < boardSize; x++) { for (int y = 0; y < boardSize; y++) { Console.Write("{0:D2} ", board[x, y]); } Console.WriteLine(); } } } static void Main(string[] args) { Printer printer = new ConsolePrinter(); // вызываем функцию Print printer.Print(int boardSize, int[,] board); } 
  • one
    Why create an object if the Print method is still static ? - Specter
  • Sorry, did not notice that it is static (copy-paste is to blame) - Rams666
  • thank you, now there is no way to check, tomorrow I will see - Alexander Kavokin

The static method in your class has nothing to do with the interface and this code will not be missed by the compiler, since the class did not implement the interface, but it must! The method should be NOT static, but instance and call like this:

 Printer printer = new ConsolePrinter(); printer.Print(int boardSize, int[,] board); 

+1 about naming -> I would call the interface IPrintable

  • IPrintable => what printit itself, some kind of wrong meaning is obtained - Specter
  • About myself there is nothing in the name. It means that if a class implements this interface, then it gets the possibility of "print" and that's it. The same convention in BOS: IDisposable, ISerializable .... - wind
  • yes, yes, there are ...able examples of the sea, but they all mean action in relation to oneself, cognitive dissonance arises when a class implementing the IPrintable interface prints a two-dimensional array that has nothing to do with this class - Specter
  • In this I agree, but there is absolutely no talk about architecture here, as well as the meaning in this interface. Theoretically, this interface should implement exactly the class that calculates arrays and will be able to print itself by implementing the necessary method. Also, it’s not a fact that calculations are not made in ConsolePrinter. - wind
  • If you think about it, then the class that implements the IPrintable interface can also implement the I_расчёт_обхода_конём_доски_able interface, then you are right - Specter
  1. Declare an interface for output
  2. You pass an interface instance to the class implementing the algorithm through the constructor, and save it to the field of this class.
  3. The output to the console from the class implementing the algorithm is done by accessing this field with the call of the method (s) declared in the interface
  4. Create a class that implements the output interface. For example, through the console.
  5. Create an instance of this class, pass it to the class that implements the algorithm.