In short, I do chess in the console, I need to have a char that has two values, or rather it could be both white and black, depends on the choice of what to put in {0} , any character.

I need to know whether it is possible to implement it at all, at least somehow, my knowledge in C # is at the novice level, so maybe I just don’t understand or don’t see a way to implement it.

 class Char { static void Main() { char One = 'П'; Console.WriteLine("{0}", One); } } 

I have already implemented everything except for some movements of the figures, it turned out somehow.

enter image description here

  • There is one good char that has two meanings. Called boolean. ) - Nick Volynkin
  • Yes, but boolean will not give me a symbol, unfortunately .. - SDst
  • one
    If the initial set of characters is limited, then probably not the entire char occupied, but most likely only the first 127 characters, but if chess is, then even less so. The high bit, for example, can be taken under the color. Then numbers up to 127 will be black, and from 128 to 255 will be white. This is one of several options. - Zealint
  • one
    You should add more specifics to the question: char is a type, variables of this type can take a very wide range of values. In addition, you create your own Char . What prevents to add to your class a field that stores the value to be output and a field that stores color? In addition: if you wanted to change the color that the console will display, you cannot do with a single line. Color should be set for each output separately. - Grundy
  • 3
    @SDst, then of course you can provide your solution so that it is clear what kind of problem you actually solved ;-) - Grundy

3 answers 3

It's time to remember that C # is an object-oriented language and add a couple of your own types to describe the shapes on the field.

First, let's set the colors of the figures by listing

 enum FigureColor { White, Black } 

Now define the shape. Since the figure can not change color during the game - we will make the property responsible for the color read-only.

 public class ChessFigure { //буквенное обозначение public char Letter { get; set; } //цвет фигуры public FigureColor Color { get; private set; } //конструктор public ChessFigure(FigureColor color, char letter) { Color = color; Letter = letter; } } 

Now the creation and output of a shape in the console can be done

 ChessFigure one = new ChessFigure(FigureColor.Black, 'П'); if(one.Color == FigureColor.Black) Console.ForegroundColor = ConsoleColor.Red; if(one.Color == FigureColor.White) Console.ForegroundColor = ConsoleColor.White; Console.Write(one.Letter); Console.ResetColor(); 

This is only an idea that can and should be developed further. A similar technique can be used for chessboard cells.

Try to immediately separate the logic of the program from its visualization, it will help to avoid difficulties with adding new functions and changing existing ones.

    Apparently, you need something like this. I think it is easy to adapt to your task.

     using System; class Program { const bool BLACK = true; const bool WHITE = false; public static void display(char c, bool bw) { Console.ForegroundColor = bw ? ConsoleColor.Black : ConsoleColor.White; Console.BackgroundColor = bw ? ConsoleColor.White : ConsoleColor.Black; Console.Write(c); } public static void Main(string[] args) { const string line1 = "RNBQKBRN"; const string line2 = "pppppppp"; const string line3 = " "; string[] lines = { line1, line2, line3, line3, line3, line3, line2, line1 }; bool first = WHITE; for (int i = 0; i < 8; ++i) { bool bw = first; first = !first; for (int j = 0; j < 8; ++j) { display(lines[i][j], bw); bw = !bw; } Console.WriteLine(); } Console.WriteLine(); Console.ReadKey(); } } 

      Could be so.

       class Char { static bool Ones; static string Enter; static void Main() { Console.Clear(); Console.OutputEncoding = Encoding.UTF8; Enter = Console.ReadLine(); switch (Enter) { case "White": Ones = true; break; case "Red": Ones = false; break; default: Main(); break; } Console.Write("┆ "); if (Ones == true) White(); if (Ones == false) Black(); Console.ResetColor(); Console.Write(" ┆"); Console.ReadKey(); } static void White() { Console.ForegroundColor = ConsoleColor.White; Console.Write("П"); } static void Black() { Console.ForegroundColor = ConsoleColor.Red; Console.Write("П"); } }