1. Computing

Programming Challenge 32 - Looping loops

Deadline February 28 2010


Back in the early 90s there was a computer game called Loopz that was very addictive. You were given pieces and had to form loops. Pieces were composite shapes made up of corners and horizontal or vertical pieces. You can watch a video of someone playing the NES (I think) version on Youtube.

This challenge is a cut down version of Loopz. On a playing area of 18 x 7 squares (same as in loopz) read a text file full of pieces (from the same location as your exe) and place them to form loops. There's one 9 char piece per line and you should read the file piece by piece (or all at once) and place the piece in the order they were read in.

When a loop is detected (see below) you remove the pieces that make up the loop, add the score and continue reading pieces. You can skip up to 3 pieces before this run is over. As in the original Loopz there is an erase piece that lets you remove any segment.

Detecting Loopz

How you implement loop detection is up to you but here's my idea. Each square in a loop segment has two neighbors. I.e. a top left corner should have a horizontal neighbor to the East and a vertical one to the South. A horizontal straight line piece has West and East horizontal neighbors etc. When you place a piece, head clockwise following the valid neighbors until you reach a break or where you started.

The pieces

Each piece is a 9 character string and affects between 1 and 5 squares. It's made up of a period ., pipe char | or minus - and 23459 representing top left 2, top right 3, bottom left 4 and bottom right 5. 9 is the erase char. 1 looks like | so isn't used. A piece made up of a top left 1 plus two horizontal segments and two vertical segments would like like this:


and in the input file is is a 9 char string. 2--|..|.. All pieces are in this notation and are valid. You will never get |--...... for example or any invalid piece shape. The periods represent empty space.

Erase Char

Occasionally, about 1 in 25 pieces will be 9........ and by playing this on any existing segment (a segment is any length of valid connected pieces), you remove all the squares in that segment.

Game Play and Scoring

Starting with an empty 18 (horizontal) by 7 (vertical board), process the input file input.txt and play every piece in order or skip up to 3. Continue adding pieces as they are or rotating them. Each rotation is 90 degrees clockwise. The piece 2--|--|-- if rotated once would become --3..|..| then rotate again ..|..|--4 then once more |..|..5--.

Every loop completed earns square(count(pieces in the loop)) points. The smallest loop is 4 (just 4 corners) so earns 16 points. All pieces that make up the loop are removed.


Please output a text file output.txt in the same loc as your exe. It should show the piece then either the word Skip (three allowed) or the coordinates (horiz, vertical) and 0-3 Rs on one line then the board after your piece is played. Also show the board after a loop is completed and removed. Use the same characters for the board but a * for each non . in the piece played. Highest score wins but your program must take no more than 5 minutes to run.

Example Output

This is a sample file. (To be done).


This is for glory only. About.com does not permit prizes to be given.

Please submit your source code and the output file to the cplus@aboutguide.com?subject=Programming Challenge 32 email address with the subject line Programming Challenge 32.

It must compile with Open Watcom, Microsoft Visual C++ 2005/2008 Express Edition/Microsoft Visual Studio 2003/2005/2008 or Borland Turbo C++ Explorer, Microsoft Visual C# 2005/2008 Express Edition or GCC/G++. If it doesn't compile, it can't be run so is automatically disqualified.

Please include your name, age (optional), blog/website url (optional) and country. Your email address will not be kept, used or displayed except to acknowledge your challenge entry. You can submit as many entries as you like before the deadline which is February 28 2010.

The top ten entries will be listed, judged purely on points. A condition of entry is that you allow your source code to be published on this website, with full credits to you as the author.

Have fun!

More Programming Challenges

  1. About.com
  2. Computing
  3. C / C++ / C#
  4. Programming Challenge List
  5. Programming Challenge 32 - Looping Loops

©2014 About.com. All rights reserved.