Site hosted by Angelfire.com: Build your free website today!
Game Code for ROM

This idea was inspired by Robert "Sembiance" Schultz after seeing his excellent game code (the version I saw had two good games - a card game, and a slot machine). My idea was to make a game that could be played by two or more players in the MUD.

Checkers

I wanted to make a board game such as chess with the board as an object, but I didn't want to start adding variables to the OBJ_DATA structure. The solution was a simple one, just use the bits in the obj->value array to represent pieces.
The problem with this method is that it is extremely limiting on the number of pieces you can have. For this reason I decided that chess would be impossible, but with some creative use of the arrays, checkers was perfect.
Although there are 64 squares on a chess board, checkers only uses 32 of them. This means that we can have one bit per square in each of the five obj->value array elements. This gives us a five bit number for each usable square on the board to represent the location of the pieces. Here's one possible scheme:

Using this scheme we actually only need three of the five array elements available! The only problem now is to piece the bits of the three elements into a usable number for each square to display the board with. I used this code (if the ascii art doesn't show up properly on your browser, it's just single lines to make boxes around the text):

void look_checkers( CHAR_DATA *ch, OBJ_DATA *obj )
{
    int i, j, k, a, b, c, d;
    char buf[MAX_STRING_LENGTH];

send_to_char( "The checkers board is set up as follows: nr", ch );

send_to_char( " 1 2 3 4 5 6 7 8nr", ch ); send_to_char( " ÚÄÄÄÂÄÄÄÂÄÄÄÂÄÄÄÂÄÄÄÂÄÄÄÂÄÄÄÂÄÄÄ¿nr", ch );

j = 1; k = 1; for ( i=1;i<=4;i++ ) { a = ( IS_SET( obj->value[0], j ) ) ? 1 : 0; a += ( IS_SET( obj->value[1], j ) ) ? 2 : 0; a += ( IS_SET( obj->value[2], j ) ) ? 4 : 0; j *= 2; b = ( IS_SET( obj->value[0], j ) ) ? 1 : 0; b += ( IS_SET( obj->value[1], j ) ) ? 2 : 0; b += ( IS_SET( obj->value[2], j ) ) ? 4 : 0; j *= 2; c = ( IS_SET( obj->value[0], j ) ) ? 1 : 0; c += ( IS_SET( obj->value[1], j ) ) ? 2 : 0; c += ( IS_SET( obj->value[2], j ) ) ? 4 : 0; j *= 2; d = ( IS_SET( obj->value[0], j ) ) ? 1 : 0; d += ( IS_SET( obj->value[1], j ) ) ? 2 : 0; d += ( IS_SET( obj->value[2], j ) ) ? 4 : 0; j *= 2;

sprintf( buf, "%d ³ %s ³ ³ %s ³ ³ %s ³ ³ %s ³ ³nr", k, ( a >= 4 ) ? "`lB`w" : ( a >= 3 ) ? "`lb`w" : ( a >= 2 ) ? "`WW`w" : ( a >= 1 ) ? "`Ww`w" : ( a >= 0 ) ? " " : "1", ( b >= 4 ) ? "`lB`w" : ( b >= 3 ) ? "`lb`w" : ( b >= 2 ) ? "`WW`w" : ( b >= 1 ) ? "`Ww`w" : ( b >= 0 ) ? " " : "1", ( c >= 4 ) ? "`lB`w" : ( c >= 3 ) ? "`lb`w" : ( c >= 2 ) ? "`WW`w" : ( c >= 1 ) ? "`Ww`w" : ( c >= 0 ) ? " " : "1", ( d >= 4 ) ? "`lB`w" : ( d >= 3 ) ? "`lb`w" : ( d >= 2 ) ? "`WW`w" : ( d >= 1 ) ? "`Ww`w" : ( d >= 0 ) ? " " : "1" );

send_to_char( buf, ch ); send_to_char( " ÃÄÄÄÅÄÄÄÅÄÄÄÅÄÄÄÅÄÄÄÅÄÄÄÅÄÄÄÅÄÄÄ´nr", ch ); k++;

a = ( IS_SET( obj->value[0], j ) ) ? 1 : 0; a += ( IS_SET( obj->value[1], j ) ) ? 2 : 0; a += ( IS_SET( obj->value[2], j ) ) ? 4 : 0; j *= 2; b = ( IS_SET( obj->value[0], j ) ) ? 1 : 0; b += ( IS_SET( obj->value[1], j ) ) ? 2 : 0; b += ( IS_SET( obj->value[2], j ) ) ? 4 : 0; j *= 2; c = ( IS_SET( obj->value[0], j ) ) ? 1 : 0; c += ( IS_SET( obj->value[1], j ) ) ? 2 : 0; c += ( IS_SET( obj->value[2], j ) ) ? 4 : 0; j *= 2; d = ( IS_SET( obj->value[0], j ) ) ? 1 : 0; d += ( IS_SET( obj->value[1], j ) ) ? 2 : 0; d += ( IS_SET( obj->value[2], j ) ) ? 4 : 0; j *= 2;

sprintf( buf, "%d ³ ³ %s ³ ³ %s ³ ³ %s ³ ³ %s ³nr", k, ( a >= 4 ) ? "`lB`w" : ( a >= 3 ) ? "`lb`w" : ( a >= 2 ) ? "`WW`w" : ( a >= 1 ) ? "`Ww`w" : ( a >= 0 ) ? " " : "1", ( b >= 4 ) ? "`lB`w" : ( b >= 3 ) ? "`lb`w" : ( b >= 2 ) ? "`WW`w" : ( b >= 1 ) ? "`Ww`w" : ( b >= 0 ) ? " " : "1", ( c >= 4 ) ? "`lB`w" : ( c >= 3 ) ? "`lb`w" : ( c >= 2 ) ? "`WW`w" : ( c >= 1 ) ? "`Ww`w" : ( c >= 0 ) ? " " : "1", ( d >= 4 ) ? "`lB`w" : ( d >= 3 ) ? "`lb`w" : ( d >= 2 ) ? "`WW`w" : ( d >= 1 ) ? "`Ww`w" : ( d >= 0 ) ? " " : "1" );

send_to_char( buf, ch ); if ( i < 4 ) send_to_char( " ÃÄÄÄÅÄÄÄÅÄÄÄÅÄÄÄÅÄÄÄÅÄÄÄÅÄÄÄÅÄÄÄ´nr", ch ); else send_to_char( " ÀÄÄÄÁÄÄÄÁÄÄÄÁÄÄÄÁÄÄÄÁÄÄÄÁÄÄÄÁÄÄÄÙnr", ch ); k++; } }

My color codes are a single '`' character followed by one letter describing the color. They are described in detail in another document. The code will properly display a checkers board using ascii art and assemble the bits of the array elements into usable numbers. The only thing left to do is simply create a control function to enable a player to move the pieces. This is a simple matter of using the SET_BIT, REMOVE_BIT and IS_SET defines from stock ROM to see what's in a square already and move a piece. The only technical detail is that to take a piece (eg a black piece), you must first move your piece (a white piece) onto the black piece, then again to move over it. This will result in the black piece being removed from the array as it is overwritten by the white piece.

Mail me at dasran@claramail.com

"MERC/Derivative Programmers" webring [Next] [Index] [Prev] [Joining]