Code Friday/Challenge 2: Code Golf: Checkerboard

From A-State Computer Science Wiki
Jump to: navigation, search


You can learn a lot about how a programming language and its compiler interact by "pushing the limits" with challenges like "Code Golf". Code Golf is best described as attempting to write a program to perform a specified task while using the least amount of code possible. The resulting program is unlikely to be easy to read, but learning how to do tasks in the shortest possible way can teach you a lot about how a programming language works, and it is fun!


A Checkerboard

Using the fewest characters possible, write a valid C++ program, complying with the C++-98 standard, that will print an 8x8 checkerboard pattern consisting of the individual characters 'B' and 'R' to the screen, oriented as shown below.

Programs will be evaluated for correctness, and then scored on raw byte count (as reported by the wc utility on Linux/UNIX, for example).

Example Output Shown in the context of a command prompt so that the final newline (required) can be observed.

user:~$ ./a.out


You may use any "tricks" or language features you like to solve this problem, provided you adhere to the following rules:

  • Your program must compile and run on a Linux (Ubuntu) system with the gcc 4.8.1 compiler using the command and compiler flags shown below:
g++ -Wall -Wextra -std=c++98 -pedantic programName.cpp
Where programName.cpp is replcaced by your actual program name. No other flags or options may be added.
  • Your program must not produce any compiler warnings when compiled.
  • Your program must end with the newline character as the last character in the file.
  • Your program must not omit the return 0; required by the C++98 standard (even though the compiler will not flag it as an error).
  • Your program must produce the output 'R' and 'B' characters one at a time (no strings or arrays allowed).
  • No input is allowed from any source.
  • Your program must run to successful completion (no crashes, segmentation faults, exceptions, or infinite loops allowed).
  • Any non-standard headers included by your program will count toward your program's length.

Parting Thoughts

Write the code in a readable format first, then carefully remove extra characters like whitespace. Remember, every character counts! Any trick you can use to reduce the number of characters in your source code is fair game (except those tricks dis-allowed by the limitations above).

Have fun!

Testing Script

In order to keep everyone on the same page with respect to scoring, you can use this standardized test script to score your solution. Since there are some things that the compiler might not always point out (the newline at the end of the file, or the presence of return 0;), this script also specifically checks those. It certainly doesn't check for all possible unusual circumstances, but it should give you a good idea how your solution scores.

The script is in the Bash scripting language. To run this script, copy/paste into a file on a Linux/UNIX server that is capable of running Bash, then set the permissions to allow the file to execute using the command:
chmod +x

Run the script with the name of your source code as the first (and only) command line argument -- so if your program is named my_checkerboard.cpp, you would type:
./ my_checkerboard.cpp

# Check and score the "Checkerboard" {code|friday} Code Golf Challenge
# Lower byte count is better!



SOURCE=$(cat $1)

LASTCHAR=$(tail -c 1 $1)

if [ "$LASTCHAR" != "" ]
        echo "No newline at end of file.  +1 byte penalty."

RETURNZERO="return 0;"

if [[ "$SOURCE" != *$RETURNZERO* ]]
        echo "No 'return 0;' in main().  +9 byte penalty."

rm a.out &>/dev/null
g++ -Wall -Wextra -std=c++98 -pedantic "$1" &> checkerboard_compile.txt 
COMPILEROUTPUT=$(cat checkerboard_compile.txt)
rm checkerboard_compile.txt

if [ "$COMPILEROUTPUT" != "" ]
        printf "Program did not compile cleanly:\n----------\n$COMPILEROUTPUT\n----------"
        exit 1 

./a.out > checkerboard_result.txt
printf "$EXPECTED" > checkerboard_test.txt
diff checkerboard_test.txt checkerboard_result.txt >checkerboard_diff.txt

if [ $comp_value -eq 1 ]
    echo "Program output did not match expectation."
    cat checkerboard_diff.txt
    bytes=$(wc -c $1 | cut -f1 -d' ')
    printf "Output OK!\nOverall byte score: $bytes"
    rm checkerboard_test.txt checkerboard_result.txt checkerboard_diff.txt
rm a.out &>/dev/null