MacTech Network:   MacTech Forums  |  MacForge.net  |  Computer Memory  |  Register Domains  |  Cables  |  iPod Deals  | Mac Deals  | Mac Book Shelf


  MacTech Magazine

The journal of Macintosh technology

 
 

Magazine In Print
  About MacTech  
  Home Page  
  Subscribe  
  Archives DVD  
  Submit News  
  MacTech Forums  
  Get a copy of MacTech RISK FREE  
MacTech Only Search:
Community Search:
MacTech Central
  by Category  
  by Company  
  by Product  
MacTech News
  MacTech News  
  Previous News  
  MacTech RSS  
Article Archives
  Show Indices  
  by Volume  
  by Author  
  Source Code FTP  
Inside MacTech
  Writer's Kit  
  Editorial Staff  
  Editorial Calendar  
  Back Issues  
  Advertising  
Contact Us
  Customer Service  
  MacTech Store  
  Legal/Disclaimers  
  Webmaster Feedback  
 Get Netflix

 July 2001 Programmer's Challenge

Down-N-Out

Mail solutions to: progchallenge@mactech.com
Due Date: 11:59pm ET, Sunday, 1 July 2001

George Warner earns two Challenge points for suggesting another interesting board game, this time the solitaire game known as Down-N-Out. The Down-N-Out board is a 10x30 rectangular array of cells, initially populated randomly with 100 cells of each of three colors. The object is to score as many points as possible by removing cells from the board. A cell can be removed if it is adjacent (horizontally or vertically, but not diagonally) with a cell of the same color. When a cell is removed, all cells connected to it by transitive adjacency are removed. That is, all adjacent cells are removed, and all cells adjacent to those cells, etc. The number of points earned for each move is equal to the square of the number of cells removed (e.g., 2 cells = 4 points, 3 cells = 9 points, etc.). There is an obvious advantage to planning moves that maximize the number of connected cells removed simultaneously. After each move, the board is compacted by sliding all cells downward to fill any empty cells, and then by sliding all columns to the center to fill any empty columns. The game continues as long as cells can be removed.

For those of you interested in trying the game, there is a shareware version available at <http://www.peciva.com/software/downout.shtml>.

The prototype for the code you should write is:

typedef char CellColor;  /* 0==empty, 1..numColors are valid colors */

void InitDownNOut(
  short boardSizeRows,  
/* number of rows in the game */
  short boardSizeCols,  
/* number of columns in the game */
  short numColors,      
/* number of colors in the game */
  const CellColor board[], 
/* board[row*boardSizeCols + col] is color of cell at [row][col] */
  WindowPtr gameWindow  
/* window where results of your moves should be displayed */
);

void HandleUpdateEvent(EventRecord theEvent);

Boolean
/* able to play */ PlayOneDownNOutMove(

  CellColor board[],  
/* board[row*boardSizeCols + col] is color of cell at [row][col] */
  long score,         
/* points earned prior to this move */
  short *moveRow,     
/* return row of your next move */
  short *moveCol,     
/* return col of your next move */
  long *numberOfCellsRemoved 
/* self explanatory */
);

void TermDownNOut(void);

Each game begins with a call to your InitDownNOut routine, where you are given the dimensions of the game board (boardSizeRows and boardSizeCols), the number of colors in the game (numColors), and a pointer (gameWindow) to a WindowRecord where you must display the game state as it progresses. Finally, you will be given the initial state of the game board, fully populated with equal numbers of each color cell, subject to rounding limitations. InitDownNOut should allocate any dynamic memory needed by your solution, and that memory should be returned at the end of the game when your TermDownNOut routine is called.

Your PlayOneDownNOutMove routine will be called repeatedly, once for each move you make. You will be given your current point score as calculated by the test code and the state of the game board. You should determine the most advantageous move and return it in moveRow and moveCol. You should update the game board, eliminating cells removed by your move and compacting the board vertically and then horizontally. You should calculate the number of cells removed and return it in numberOfCellsRemoved.

The last time we ran a Challenge that involved maintaining a display, contestants asked how the window would be redrawn in response to an update event. This time, I’m asking you to write a routine to do that. Your HandleUpdateEvent routine will be called by the test code whenever an update event is received for your gameWindow.

During the call to InitDownNOut, and after each of your moves, you should display the updated game state in the gameWindow. The details of the display are up to you, as long as the display correctly and completely represents the state of the board.

The winner will be the best scoring entry, as determined by the sum of the point score of each game, minus a penalty of 1% for each millisecond of execution time used for that game. The Challenge prize will be divided between the overall winner and the best scoring entry from a contestant that has not won the Challenge recently.

This will be a native PowerPC Challenge, using the CodeWarrior Pro 6 environment. Solutions may be coded in C or C++. I’ve deleted Pascal from the list of permissible languages, both because it isn’t supported by CW6 (without heroics) and because no one has submitted a Pascal solution in a long time.


Test code for this Challenge will be available soon.


You can get a head start on the Challenge by reading the Programmer's Challenge mailing list. It will be posted to the list on or before the 12th of the preceding month. To join, send an email to listserv@listmail.xplain.com with the subject "subscribe challenge-A". You can also join the discussion list by sending a message with the subject "subscribe challenge-D".





Generate a short URL for this page:


Click on the cover to
see this month's issue!

TRIAL SUBSCRIPTION
Get a RISK-FREE subscription to the only technical Mac magazine!

Today's Deal


Apple Special

Order
Snow Leopard,
Mac Box Set, Family Pack,
and Snow Leopard Server
at a discount.
 


MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797

Register Low Cost (ok dirt cheap!) Domain Names in the MacTech Domain Store. As low as $1.99!
Save on long distance * Upgrade your Computer. See local info about Westlake Village
appleexpo.com | bathjot.com | bathroomjot.com | bettersupplies.com | comclothing.com | computerlunatics.com | dotcomclothing.com | explainit.com | exposinternational.com | homeismycastle.com | hoodcards.com | intlexpo.com | keyinfocard.com | kosheru.com | learnmorsels.com | localdealcards.com | lvschools.com | macjobsearch.com | mactechjobs.com | mactechmonitor.com | mactechsupplies.com | macwishbook.com | movie-depot.com | netprofessional.com | nibblelearning.com | notesintheshower.com | officejot.com | onlinebigbox.com | palmosdepot.com | peopleslineitemveto.com | showerjot.com | snapestore.com | snapishop.com | snapistore.com | snaptradingpost.com | stimulusmap.com | stimulusroadmap.com | triunfoguides.com | video-depot.com
Staff Site Links



All contents are Copyright 1984-2008 by Xplain Corporation. All rights reserved.

MacTech is a registered trademark of Xplain Corporation. Xplain, Video Depot, Movie Depot, Palm OS Depot, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, NetProLive, JavaTech, WebTech, BeTech, LinuxTech, Apple Expo, MacTech Central and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.