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

February 1999 Programmer's Challenge

Chinese Checkers

Mail solutions to: progchallenge@mactech.com
Due Date: 11:59pm ET, Monday, 1 February 1999

For this month’s Challenge, I’ve reached once again into my stash of board games, this time coming up with one I haven’t played in a very long time: Chinese Checkers. The prototype for the code you should write is:

#if defined(__cplusplus)
extern "C" {
#endif

typedef struct Position { long row; long col; } Position; void InitChineseCheckers( long numPlayers, /* 2..6 */ long gameSize, /* base of home triangle, 3..63, you have size*(size+1)/2 pieces */ long playerPosition[6], /* 0..5, entries 0..numPlayers-1 are valid */ long yourIndex /* 0..numPlayers-1, your position is playerPosition[yourIndex] */ ); void YourMove( Position *from, /* originating position */ Position *to /* destination position */ ); void OpponentMove( long opponent, /* index in playerPosition[] of the player making move */ Position from, /* originating position */ Position to /* destination position */ ); void TermChineseCheckers(void); #if defined(__cplusplus) } #endif

Chinese checkers is played on a six-pointed star shaped board by 2 to 6 players. The board can be viewed as six equilateral triangles attached to a hexagon. Each Position on the board is connected to six adjacent positions (ignoring boundary conditions), one each to the left, right, upper left, upper right, lower left, and lower right. At the beginning of a game, each player’s pieces occupy one of the six triangular areas, referred to as that player’s home position. The triangular area opposite the player’s home position is called that player’s goal position. The triangular areas that are not part of the home or goal positions are called neutral zones for that player. The objective of the game is to move your pieces from your home to your goal before any other player does the same.

Standard Chinese Checker boards use a board where each player has 10 pieces, initially placed in a home triangle with 4 positions at its base. We’ll refer to the base of the home triangle as the gameSize. I’ve also seen games with 6 pieces per player (a gameSize of 3). In our game, the gameSize can be anywhere from 3 to 64, resulting in anywhere from 6 to 2080 pieces per player.

To communicate game play between my test code and your solution, we need a numbering system for the board. The positions on the board are described using row and column values. The board will have 4*gameSize+1 rows, numbered from 0 to 4*gameSize. The star shape dictates that row r has the following number of positions:

Rows

# of columns

0

to

gameSize-1

r+1

gameSize

to

2*gameSize-1

4*gameSize+1-r

2*gameSize

to

3*gameSize

r+1

3*gameSize+1

to

4*gameSize

4*gameSize+1-r

The columns in row r with c columns are numbered from -(c-1)/2 to +(c-1)/2, inclusive, for even numbered rows, and from —(c/2)+1 to (c/2), inclusive, for odd numbered rows. For a position (r,c) in an even numbered row, the adjacent positions are (r,c-1), (r,c+1), (r+1,c), (r+1,c+1), (r-1,c), and (r-1,c+1). For the same position in an odd-numbered row, the adjacent positions are (r,c-1), (r,c+1), (r+1,c), (r+1,c-1), (r-1,c), and (r-1,c-1). (Provided, of course, that those positions exist on the board).

           0:                           0
           1:                         0   1 
           2:                      -1   0   1 
           3:                    -1   0   1   2
           4:  -6  -5  -4  -3  -2  -1   0   1   2   3   4   5   6 
           5:    -5  -4  -3  -2  -1   0   1   2   3   4   5   6
           6:      -5  -4  -3  -2  -1   0   1   2   3   4   5  
           7:        -4  -3  -2  -1   0   1   2   3   4   5
           8:          -4  -3  -2  -1   0   1   2   3   4  
           9:        -4  -3  -2  -1   0   1   2   3   4   5
          10:      -5  -4  -3  -2  -1   0   1   2   3   4   5  
          11:    -5  -4  -3  -2  -1   0   1   2   3   4   5   6
          12:  -6  -5  -4  -3  -2  -1   0   1   2   3   4   5   6 
          13:                    -1   0   1   2
          14:                      -1   0   1 
          15:                         0   1 
          16:                           0

A move in Chinese Checkers can be either a non-jump move or a sequence of contiguous jumps. Relocating a piece from one position to an unoccupied adjacent position is a non-jump move. Jumping over an adjacent occupied position into an unoccupied position, collinear with the first two, is considered a jump. A single jump move can consist of jumping a single piece over an occupied position, followed by a jump from the new location over an adjacent piece to still another location, repeated as many times as the board situation allows. No pieces are removed from the board at any time. A move may not begin or end in a neutral zone triangles, although a multi-jump move may include an intermediate position in the neutral zone. Some Chinese Checker rules allow a player to pass; this is not allowed in our game.

Your InitChineseCheckers routine will be called at the beginning of each game with the game parameters, the number of players in the game (numPlayers), the size of the board (gameSize, described above), the board position for each player (playerPosition[]), and your index as a player in the game (yourIndex). The playerPosition is numbered clockwise starting from the (0,0) position.

After every player has been initialized, the player at playerPosition[0] will be asked to play by calling his YourMove routine. That player should return the Position of the piece he is moving in *from, and the position he is moving to in *to. For a non-jump move, the two positions must be adjacent. For a jump move, there must be a valid sequence of jumps between *from and *to. If you cannot make a legal move, set *from to the location of one of your pieces and *to to that same location. Illegal moves will result in disqualification.

After a player moves, the other players will be notified of the move by calling their OpponentMove routines, providing the index of the moving player in opponent, and the positions moved from and to in those parameters, respectively.

The game is over when one player has moved all of his pieces from the home area to the goal area, provided all players have had the same number of turns to move. At the end of the game, TermChineseCheckers will be called, where you can deallocate any memory allocated during the game.

The evaluation will consist of solutions playing against one another in combinations of between 2 and 6 players per game. Each solution will play the same number of games against each other solution, with the same number of opportunities to play first, with fair seating arrangements that will be determined after I see how many solutions are submitted. The winner will be the solution that wins the most games in the least amount of time. Specifically, the winner will be the solution with the greatest score, where the score is two times the number of games won, plus the number of games drawn, minus a time penalty of 1 game per second of execution time. Execution time will include the time used by all of your routines: InitChineseCheckers, YourMove, OpponentMove, and TermChineseCheckers.

This will be a native PowerPC Challenge, using the latest CodeWarrior environment. Solutions may be coded in C, C++, or Pascal.


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.