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".
|