C# How to display the 2dim array to right side of the numbers

Viewed 66

Problem

I want to display a chessboard, but the fields are displayed on the left instead of the right side of the numbers.

the description of the chessboard is:

Create a method with signature void DisplayChessboard(ChessPiece[,] chessboard).

This method displays the chessboard, including the row-numbers and column-letters (see screenshot below). Hint: if row + column is even, then use a light background color (e.g. Gray), otherwise use a dark background color (e.g. DarkYellow). For now, print 3 spaces for each cell. → Call this method DisplayChessboard from the Start method.

in the picture below is how i want it to look like. In my head it should work fine but it doesn't when i run it. how its supposed to look like

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace assignment1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Program myProgram = new Program();
            myProgram.Start();
        }

        private void Start()
        {
            ChessPiece[,] chessboard = new ChessPiece[8, 8]; // create 2dim array which is the chessboard
            InitChessboard(chessboard);
            DisplayChessboard(chessboard);
        }

        private void InitChessboard(ChessPiece[,] chessboard) // Fills chessboard with value null
        {
            for (int row = 0; row < chessboard.GetLength(0); row++)
            {
                for (int col = 0; col < chessboard.GetLength(1); col++)
                {
                    chessboard[row, col] = null;
                }
            }
        }

        private void DisplayChessboard(ChessPiece[,] chessboard) // Displays the chessboard
        {
            int[] numbers = { 8, 7, 6, 5, 4, 3, 2, 1 };
            char[] letters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };

            for (int row = 0; row < chessboard.GetLength(0); row++)
            {
                Console.WriteLine(numbers[row]); // displays the numbers
                for (int col = 0; col < chessboard.GetLength(1); col++)// fills chessboard with colors
                {
                    if ((row + col) % 2 == 0)
                    {
                        Console.BackgroundColor = ConsoleColor.Gray;
                    }
                    else
                    {
                        Console.BackgroundColor = ConsoleColor.DarkYellow;
                    }
                    Console.Write("   ");
                    Console.ResetColor();
                }
            }
            for (int i = 0; i < letters.Length; i++) // displays the letters
            {
                Console.Write($"   {letters[i]}");
            }
        }
    }
}
2 Answers

Two issues here:

  • After writing the numbers do not issue a newline. Use Write() instead of WriteLine().

  • After each row (and also before the last loop where the letters are displayed), do use a single WriteLine() in order to print them under the board.

Here is the code that worked for me:

private void DisplayChessboard(ChessPiece[,] chessboard) // Displays the chessboard
{
    int[] numbers = { 8, 7, 6, 5, 4, 3, 2, 1 };
    char[] letters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };

    for (int row = 0; row < chessboard.GetLength(0); row++)
    {
        Console.Write(numbers[row]); // displays the numbers
        for (int col = 0; col < chessboard.GetLength(1); col++)// fills chessboard with colors
        {
            if ((row + col) % 2 == 0)
            {
                Console.BackgroundColor = ConsoleColor.Gray;
            }
            else
            {
                Console.BackgroundColor = ConsoleColor.DarkYellow;
            }
            Console.Write("   ");
            Console.ResetColor();
        }
        Console.WriteLine();
    }
    for (int i = 0; i < letters.Length; i++) // displays the letters
    {
        Console.Write($"{letters[i],3}");
    }
    Console.WriteLine();
}

PS. I removed one space before each letter display. I did that by adding a column width parameter to the output string. I used the {value,width} format.

Console.WriteLine - Writes the specified data, followed by the current line terminator, to the standard output stream. Try Write instead...

Related