This was a typical introduction to programming question. Create the pattern of a chess board that is 8 x 8.  Use X and O to represent the squares. This is what your output should look like.

XOXOXOXO
OXOXOXOX
XOXOXOXO
OXOXOXOX
XOXOXOXO
OXOXOXOX
XOXOXOXO
OXOXOXOX

Below is a simple code l wrote to get this output. This is just for fun.

using System;

namespace ChessDashboard
{
class Program
{
static void Main(string[] args)
{
int i,j,remainder;
for(i=1;i <= 8;i++)
{
if (i % 2 == 0) { remainder = 1; }
else {remainder = 0;}
for (j = 1; j <=8; j++)
{
if (j % 2 == remainder )
{ Console.Write(“O”); }
else { Console.Write(“X”); }
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

What l’m doing is pretty straight forward, l have two for loops. The outside for loop corresponds to getting the 8 rows expected and the inside for loop corresponds to getting the 8 columns each alternating with X or 0. I used the mode operator to determine if the current i is divisible by 2 or not which helps me decide how to start outputting the current row with an X or O. When l complete outputing 8 columns, l move to the next line with writeline(). The readline() at the end is just to allow the output visible untill the user presses any key.

 

4 thoughts on “Create a simple C# code to output an 8X8 chest board

  1. Hi,
    just add a small change in ur code like this…..
    class chessboard
    {
    ///
    /// chess board
    ///
    static void Main()
    {

    Console.WriteLine(“Printing the chess board patterns”);
    Console.WriteLine(“———————————-“);
    string[,] chess = new string[8, 8];
    string finalvalofcolumn = string.Empty;

    for (int i = 0; i < 8; i++)
    {
    for (int j = 0; j < 8; j++)
    {

    chess[i, j] = ((i + j) % 2 == 0) ? chess[i, j] = "x" : chess[i, j] = "0";

    }
    }

    for (int i = 0; i < 8; i++)
    {
    for (int j = 0; j < 8; j++)
    {
    Console.Write(chess[i, j], "");
    }
    Console.WriteLine("\n");
    }
    Console.ReadKey();

    }
    }

  2. below are 3 different ways to print a chessboard in console. 3rd one is the most optimized of the three ways provided below.

    public static void PrintChessBoard()
    {
    char dark = ‘X’;
    char light = ‘O’;
    string cell = “{0} “;
    char[] cells = { light, dark };
    StringBuilder board = new StringBuilder();
    for(int i = 1; i <= 8; i++)
    {
    for(int j = 1; j <= 8; j++)
    {
    /****************************Method-1******************************/
    //if (i % 2 == 0)
    //{
    // Console.Write(cell, j % 2 == 0 ? light : dark);
    // board.Append(string.Format(cell, j % 2 == 0 ? light : dark));
    //}
    //else
    //{
    // Console.Write(cell, j % 2 == 0 ? dark : light);
    // board.Append(string.Format(cell, j % 2 == 0 ? dark : light));
    //}

    /*************Method-2(Optimization from above method)*************/
    //Method-2(Optimization from above method)
    //Console.Write(cell, (i+j) % 2 == 0 ? light : dark);
    //board.Append(string.Format(cell, (i + j) % 2 == 0 ? light : dark));

    /*******************Method-3(Most Optimized)***********************/
    Console.Write(cell, cells[(i + j) % 2]);
    board.Append(string.Format(cell, cells[(i + j) % 2]));
    }
    Console.WriteLine();
    board.Append(Environment.NewLine);
    }

    Console.WriteLine();
    Console.Write(board);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *