Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Draw an ASCII Checkerboard

Summary

Inspired by the recent popularity of ASCII art challenges, this challenge's purpose is to draw an ASCII checkerboard, like one on which Chess can be played.

Write a program that takes a positive integer n as an argument, in stdin, or as user input, and output an checkerboard with nxn squares, along with a border that is 1 character thick.

Each square should be 2x2 characters. The squares should follow the normal alternating white-black (white first, as in top-left corner) pattern of a checkerboard. White squares should be made out of space () characters, and black squares should be made out of pound (#) characters.

The border should be made out of dashes (-) with a plus (+) on the border or perpendicular point of a square.

Input

Positive integer in representing the number of squares (dimensions in squares) to draw in the checkerboard, with each square being 2x2 characters.

Example Results

n=2

+--+--+
| |##|
| |##|
+--+--+
|##| |
|##| |
+--+--+

n=3

+--+--+--+
| |##| |
| |##| |
+--+--+--+
|##| |##|
|##| |##|
+--+--+--+
| |##| |
| |##| |
+--+--+--+

n=4

+--+--+--+--+
| |##| |##|
| |##| |##|
+--+--+--+--+
|##| |##| |
|##| |##| |
+--+--+--+--+
| |##| |##|
| |##| |##|
+--+--+--+--+
|##| |##| |
|##| |##| |
+--+--+--+--+

... and so on.


Notes

  • Trailing spaces and new lines are acceptable.
  • You may write either an entire program or a function.
  • No leading spaces.
  • Your program should display correct results for n=15.
  • For less-known esoteric languages and similar, provide a link to the language.
  • n=0 should produce +. (optional, but highly recommended and encouraged.)
  • Shortest code in bytes wins, as this is code golf.

Answer*

Draft saved
Draft discarded
Cancel
1
  • \$\begingroup\$ 100 bytes \$\endgroup\$ Commented Jul 4, 2020 at 1:09

AltStyle によって変換されたページ (->オリジナル) /