Skip to content

Instantly share code, notes, and snippets.

@aadsm
Last active January 20, 2024 02:58
Show Gist options
  • Save aadsm/6e3f583e6203242702572e0d4a094f92 to your computer and use it in GitHub Desktop.
Save aadsm/6e3f583e6203242702572e0d4a094f92 to your computer and use it in GitHub Desktop.
Chessboard
//******************************************************************
// Chessboard program
// This program prints a chessboard pattern that is built up from
// basic strings of white and black characters.
//******************************************************************
#include <iostream>
#include <string>
using namespace std;
const string BLACK = "%%%%%"; // Define a line of a black square //
const string BLACK2 = "% %";
const string WHITE = " "; // Define a line of a white square
const string WHITE2 = "%%%%%";
// Create a white-black row by concatenating the basic strings
const string WHITE_ROW = WHITE + BLACK + WHITE + BLACK + WHITE + BLACK + WHITE \
+ BLACK;
const string WHITE_ROW_BORDER = WHITE2 + BLACK + WHITE2 + BLACK + WHITE2 \
+ BLACK + WHITE2 + BLACK;
// Create a black-white row by concatenating the basic strings
const string BLACK_ROW = BLACK2 + WHITE + BLACK2 + WHITE + BLACK2 + WHITE \
+ BLACK2 + WHITE;
const string BLACK_ROW_BORDER = BLACK + WHITE + BLACK + WHITE + BLACK + WHITE \
+ BLACK + WHITE;
void printRow(string row, string rowBorder, int height)
{
cout << rowBorder << endl;
for (int i = 0; i < height; i++)
{
cout << row << endl;
}
cout << rowBorder << endl;
}
void printWhiteBlackRow(int height)
{
printRow(WHITE_ROW, WHITE_ROW_BORDER, height);
}
void printBlackWhiteRow(int height)
{
printRow(BLACK_ROW, BLACK_ROW_BORDER, height);
}
int main()
{
const int rowHeight = 8;
const int rowsTotal = 8;
// five wide eitght height white-black rows
for (int i = 0; i < rowsTotal; i++)
{
if (i % 2 == 0)
{
// even row
printWhiteBlackRow(rowHeight);
}
else
{
// odd row
printBlackWhiteRow(rowHeight);
}
}
// system("PAUSE"); //to pause console output ?
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment